虛擬呼叫我有一個實體宣稱與此類似:流利NHibrnate - 在構造 - 最佳實踐
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個重載構造函數。以上只是一個例子。
謝謝!
怎麼樣,我需要從外面傳值的方法:(字符串文本,日期creationDate,串AUTHOREMAIL) – iniki 2011-03-15 16:05:48
@iniki:Huh?I不明白你的意思,因爲你可以簡單地說'var comment = new Comment {Text = GetText(),CreationDate = GetCreationDate(),AuthorEmail = GetAuthorEmail()};' – jason 2011-03-15 16:32:02
Jason對不起,但我想我是在這裏忽略了你的觀點:-(實際上,我現在所需的重載構造函數需要將對象成員設置爲從客戶端應用程序接收的值。不知道我怎麼能在無參數的構造函數中做到這一點。你能否詳細說明你的答案? – iniki 2011-03-16 12:20:40