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

查看:15
本文介绍了从 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天全站免登陆