我很难理解 NHibernate 的 petapoco 加载机制。实际上我做了一个测试来比较两者在查询时的表现。
我的类(class)如下:
UserTest.cs
具有以下属性:
private string name;
private int id;
private int customerId;
public int ID
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public int? CustomerID
{
get { return customerId; }
set
{
if (value != customerId)
{
customerId = value;
if (this.ID > 0)
{
DoSomeOtherWork();
}
}
}
}
当我执行 User.Load
时在 NHibernate 中,我观察到 DoSomeOtherWork
永远不会被调用,而在 PetaPoco 中,当我从加载 User
进行查询时如Connection.db.Fetch<UserTest>(...)
或Connection.db.Query<UserTest>(...)
,我可以看到DoSomeOtherWork
被调用。
为什么会这样?
有没有办法避免调用 DoSomeOherWork
当使用 PetaPoco 使其具有与 NHibernate 相同的行为时?我不t want to use
PetaPoco.忽略as I need to get and set the
客户ID`。
最佳答案
PetaPoco 它是一个微型 ORM(比 Nhibernate 轻得多),并在您获取记录时具体化您的 POCO 对象。除此之外没有其他魔法,所以答案是:
不,您无法避免调用属性的 setter。
关于NHibernate 与 petapoco 加载机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10222550/