首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Fluent NHibrnate -构造函数-Best实践中的虚拟调用

Fluent NHibrnate -构造函数-Best实践中的虚拟调用
EN

Stack Overflow用户
提问于 2011-03-15 15:58:54
回答 2查看 701关注 0票数 1

我有一个与此类似的实体:

代码语言:javascript
复制
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声明为代理。但是,我想知道是否应该将构造函数方法移动到一个新的工厂类,并将新方法声明为

代码语言:javascript
复制
CreateComment(string text, DateTime creationDate, string authorEmail)  

在这种情况下,最好的做法是什么?

请注意,目前我在域实体中有4-5个重载的构造函数.以上只是一个例子。

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2013-01-15 15:43:40

我用FluentNHibernate进行了测试,您可以这样做:

代码语言:javascript
复制
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; }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2011-03-15 16:02:45

我更喜欢有一个无参数(默认)构造函数,并且构造如下:

代码语言:javascript
复制
var comment = new Comment {
                  Text = "Something offensive and political.",
                  CreationDate = DateTime.Now,
                  AuthorEmail = "someonestupidwithanopinion17@aol.com"
              };

现在问题已经不重要了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5314428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档