Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.NET ORMs和持久化非属性状态_.net_Orm_Properties_Class Design_Private Members - Fatal编程技术网

.NET ORMs和持久化非属性状态

.NET ORMs和持久化非属性状态,.net,orm,properties,class-design,private-members,.net,Orm,Properties,Class Design,Private Members,我刚刚开始使用.NETORMS,甚至还没有决定使用实体框架和NHibernate。但在这两种情况下,我都遇到了一个问题,因为他们似乎希望我以各种不自然的方式设计我的类。这是关于这个问题的几个问题之一 示例类: public class Pledge // this is an entity BTW, not a value object { private readonly int initialAmount; private bool hasBeenDoubledYet;

我刚刚开始使用.NETORMS,甚至还没有决定使用实体框架和NHibernate。但在这两种情况下,我都遇到了一个问题,因为他们似乎希望我以各种不自然的方式设计我的类。这是关于这个问题的几个问题之一


示例类:

public class Pledge // this is an entity BTW, not a value object
{
    private readonly int initialAmount;
    private bool hasBeenDoubledYet;

    public Pledge(int initialAmount)
    {
        this.initialAmount = initialAmount;
    }

    public int GetCurrentAmount()
    {
        return this.hasBeenDoubledYet ? this.initialAmount * 2 : this.initialAmount;
    }
    public void Double()
    {
        this.hasBeenDoubledYet = true;
    }
}
在这种情况下,持久性逻辑有点复杂。我们希望保留私有的
initialAmount
hasBeenDoubledYet
字段;重新实例化时,我们希望使用
initialAmount
调用构造函数,如果
已被加倍
字段为
true
,则调用
Double()
。这显然是我必须为之编写一些代码的事情

另一方面,据我所知,典型的“ORM友好”版本的代码最终可能看起来更像这样:

public class Pledge
{
    // These are properties for persistence reasons
    private int InitialAmount { get; set; }  // only set in the constructor or if you are an ORM
    private bool HasBeenDoubledYet { get; set; }

    private Pledge() { } // for persistence
    public Pledge(int initialAmount) { /* as before but with properties */ }

    public int GetCurrentAmount() { /* as before but with properties */ }
    public int Double() { /* as before but with properties */ }
}
我在中谈到了我对默认构造函数和只读字段等的保留意见,但我想这个问题实际上是关于如何让ORM处理私有字段而不是私有属性的问题——可以在EF中完成吗?在尼伯内特?我们无法为代理目的标记字段
virtual
。。。标记使用它们的方法
virtual
就足够了吗



这一切都让人感觉很不舒服:(.我希望这里的人能指出我错在哪里,无论是在我对他们的能力的理解上,还是在我对域建模和ORM角色的思考上。

我不知道EF,但NHibernate需要你想要坚持的属性是虚拟的和公共的(正如你出于代理原因所说的).

感觉很“黑”因为您误用了ORM的设计目的;您试图在实体对象内部执行业务逻辑;一些参考文献,我相信如果您执行简单的search@K伊万诺夫:我承认我在这方面是个新手,但这意味着在领域驱动设计的意义上是一个实体,所以业务逻辑确实存在工作是在数据库和域对象之间进行转换。你是说在数据库和我的域层之间应该有一个“实体对象”的中间层吗?