我有一个与此类似的实体:
public class Comment
{
public Comment(string text, DateTime creationDate, string authorEmail)
{
Text = text;
CreationDate = creationDate;
AuthorEmail = authorEmail;
}
public virtual string Text { get; private set; }
public virtual DateTime CreationDate { get; set; }
public virtual string AuthorEmail { get; private set; }
}我从Is it OK to call virtual properties from the constructor of a NHibernate entity?那里拿来的
我收到警告作为‘在构造函数中的虚拟调用’。
尽管如此,它并不会带来任何实际问题,因为虚拟成员只为NH声明为代理。但是,我想知道是否应该将构造函数方法移动到一个新的工厂类,并将新方法声明为
CreateComment(string text, DateTime creationDate, string authorEmail) 在这种情况下,最好的做法是什么?
请注意,目前我在域实体中有4-5个重载的构造函数.以上只是一个例子。
谢谢!
发布于 2013-01-15 15:43:40
我用FluentNHibernate进行了测试,您可以这样做:
public class Comment
{
private string _text;
private DateTime _creationDate;
private string _authorEmail;
public Comment(string text, DateTime creationDate, string authorEmail)
{
_text = text;
_creationDate = creationDate;
_authorEmail = authorEmail;
}
public virtual string Text
{
get { return _text; }
private set { _text = value; }
}
public virtual DateTime CreationDate
{
get { return _creationDate; }
set { _creationDate = value; }
}
public virtual string AuthorEmail
{
get { return _authorEmail; }
private set { _authorEmail = value; }
}
}发布于 2011-03-15 16:02:45
我更喜欢有一个无参数(默认)构造函数,并且构造如下:
var comment = new Comment {
Text = "Something offensive and political.",
CreationDate = DateTime.Now,
AuthorEmail = "someonestupidwithanopinion17@aol.com"
};现在问题已经不重要了。
https://stackoverflow.com/questions/5314428
复制相似问题