nhibernate持久化对象真的需要默认构造函数吗? [英] Is the default constructor really necessary for nhibernate to persist an object?

查看:68
本文介绍了nhibernate持久化对象真的需要默认构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我不想让用户创建对象的实例,而无需向构造函数发送属性 但据我所知,该对象应该具有默认构造函数,因此可以在不发送requierd属性的情况下创建实例. 有什么办法可以防止这个问题?如果是的话,是否有副作用?

for some reason I don't wanna let user to create an instance of the object, without sending a property to the constructor but as I know the object should have default constructor and so it would be possible to create an instance with out sending requierd property. is there any way to prevent this problem? and if yes does it have any side effect?

推荐答案

只需使用受保护的默认构造函数即可:

Just use a protected default constructor:

public class Product
{
    protected Product() { }

    public Product(Category category)
    {
        this.Category = category;
    }
}

这篇关于nhibernate持久化对象真的需要默认构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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