可以从NHibernate实体的构造函数中调用虚拟属性吗? [英] Is it OK to call virtual properties from the constructor of a NHibernate entity?

查看:32
本文介绍了可以从NHibernate实体的构造函数中调用虚拟属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看下面的示例代码:

public class Comment
{
    private 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; }
}

我知道从构造函数中调用虚拟成员函数被认为是不好的做法,但是在NHibernate中,我需要将属性设为虚拟以支持延迟加载.在这种情况下可以吗?

i know it's considered bad practice to call virtual member functions from the constructor, however in NHibernate i need the properties to be virtual to support lazy loading. Is it considered OK in this case?

推荐答案

我很确定这是可以的,但是如果您担心的话,总是可以在较少参数的构造函数调用之后分配属性.

I'm pretty sure this is fine, but if your worried you could always just assign the properties after a parameter less constructor call.

这篇关于可以从NHibernate实体的构造函数中调用虚拟属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆