3

I'm trying to support NHibernate proxying of my entities by marking the entities' properties as virtual:

public class Video : IAbstractDomainEntity<string>
{
    public virtual string Id { get; set; }
    public virtual string Title { get; set; }
    public virtual int Duration { get; set; }
    public virtual string Author { get; set; }
    public virtual bool HighDefinition { get; set; }

    public Video()
    {
        Id = string.Empty;
        Title = string.Empty;
        Author = string.Empty;
    }
}

ReSharper states that this is bad practice due to the issue described here: Virtual member call in a constructor

I understand the problem and I also understand that I cannot mark my class as sealed because then NHibernate would not be able to generate a proxy from the entity.

Do I just live with the waning and make sure not to do anything weird with my setters?

Community
  • 1
  • 1
Sean Anderson
  • 27,963
  • 30
  • 126
  • 237
  • I would say, change the implementation to avoid this situation. E.g. do not use the auto property (but use Resharper to quickly convert it to property with backing field) and set the defaults there. Virtual property in Constructor could later simply lead to unexpected behaviour – Radim Köhler Feb 28 '14 at 17:16
  • 1
    possible duplicate of [Is it OK to call virtual properties from the constructor of a NHibernate entity?](http://stackoverflow.com/questions/465436/is-it-ok-to-call-virtual-properties-from-the-constructor-of-a-nhibernate-entity) – plmaheu Mar 02 '14 at 17:32

2 Answers2

3

The best-practice is to use properties with backing fields:

public class Video : IAbstractDomainEntity<string>
{
    private string _id;
    private string _title;
    private string _author;

    public virtual string Id
    {
        get { return _id; }
        set { _id = value; }
    }
    public virtual string Title
    {
        get { return _title; }
        set { _title = value; }
    }
    public virtual string Author
    {
        get { return _author; }
        set { _author = value; }
    }
    public virtual int Duration { get; set; }
    public virtual bool HighDefinition { get; set; }

    public Video()
    {
        _id = string.Empty;
        _title = string.Empty;
        _author = string.Empty;
    }
}

So you can avoid problems on child classes and you don't see any warning anymore

giammin
  • 18,620
  • 8
  • 71
  • 89
0

I prefer the more concise, encapsulated variant:

public class Video : IAbstractDomainEntity<string>
{
    public virtual string Id { get; protected set; }
    public virtual string Title { get; protected set; }
    public virtual string Author { get; protected set; }
    public virtual int Duration { get; protected set; }
    public virtual bool HighDefinition { get; protected set; }

    public static Video Create()
    {
        return new Video 
        {
            Id = string.Empty,
            Title = string.Empty,
            Author = string.Empty
        }
    }
}
ThomasDC
  • 484
  • 3
  • 11