我刚刚开始使用.NET ORM,甚至没有在实体框架和NHibernate之间做出决定。但在这两种情况下,我遇到了一个问题,他们似乎希望我以各种不自然的方式设计我的课程。这是关于这个问题的几个问题之一。.NET ORMs和持久非属性状态
实施例类:
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
字段;当重新实例化时,如果hasBeenDoubledYet
字段为true
,我们想调用构造函数initialAmount
,并调用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 */ }
}
我介绍了有关默认构造函数和只读我的预订字段等在another post,但我想这个问题是关于如何让ORMs处理私人领域,而不是私人属性 - 可以在EF中完成吗?在NHibernate中?我们不能标记字段virtual
代理的目的......会标记使用它们的方法virtual
就足够了吗?
这一切都感觉那么哈克:(。我希望这里有人能指出我的错了,无论是在我自己的能力把握或在我的有关域建模和奥姆斯的角色思考。
因为你是滥用ORM什么是设计感觉“哈克” for;你正试图在实体对象内部做业务逻辑;一些参考http://en.wikipedia.org/wiki/O-RM,如果你做简单的搜索,我相信还有更多 – 2011-03-19 20:46:03
@K Ivanov:我承认我在这方面是一个新手,但这意味着它是一个领域驱动设计意义上的实体,所以业务逻辑确实在那里。我认为ORM的工作是在数据库和域对象之间进行翻译。你是说在数据库和我的领域层之间应该有一个“实体对象”的中间层? – Domenic 2011-03-19 20:58:21