我试图通过将实体的属性标记为虚拟来支持我的实体的NHibernate代理:
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指出,由于此处描述的问题,这是不好的做法:Virtual member call in a constructor
我理解这个问题并且我也理解我不能将我的类标记为密封,因为NHibernate将无法从实体生成代理。
我是否只是与衰弱一起生活,并确保不要对我的制定者做任何奇怪的事情?
答案 0 :(得分:2)
最佳做法是使用具有支持字段的属性:
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;
}
}
因此,您可以避免儿童课程出现问题,而且您再也看不到任何警告