确定实体字段在NHibernate中是否已更改 [英] Determine if an entity field changed in NHibernate

查看:71
本文介绍了确定实体字段在NHibernate中是否已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电话,需要确定某个字段是否已更改.但是使用该实体ID调用get会返回相同的实体,而不是先前的版本.

I have a call that needs to determine if a field has changed. But calling get using that entities id returns the same entity not the prior version.

Entity e = Dao.Get(id);
//At this point e.Field is X
e.Field = y;
Dao.Save(e);

Entity Dao.Get(Guid id)
{
  return Session.Get(id);
}

Entity Dao.Save(Entity e)
{
   Entity olde = Session.Get(e.Id);
   if (e.Field != olde.Field) <--- e.Field == olde.Field so it does not run.
     DoBigMethod(e);
   return e;
}

如何在不向实体类添加onChange方法的情况下处理这种情况.

How do I handle this situation without adding an onChange method to the Entity class.

推荐答案

您只知道实体的一个版本":当前的一个.实际上,只有 个实体的一个版本.您已经将其保存在内存中,并且已经对其进行了更改,并且忘记了以前的状态.

You only know one "version" of the entity: the current one. There is actually only one version of the entity. You have it in memory and you already changed it and forgot the previous state.

调用get可以查看以前的数据库状态是危险的.如果更改已经被刷新(例如,NHibernate在查询之前刷新),则可以获取更改.如果您打开另一个会话,则会看到来自其他事务的更改.

Call get to see the previous database state is dangerous. If changes are already flushed (NHibernate flushes before queries for instance), you get your changes. If you open another session, you see changes from other transactions.

您只对一个领域感兴趣吗?然后,您可以将旧值缓存在某个地方.

Are you only interested in one single field? Then you can cache the old value somewhere.

如果这行不通,则需要告诉我更多有关为什么需要了解此字段先前值的原因.

If this wouldn't work, you need to tell me more about the reason why you need to know the previous value of this field.

更多想法:

  • 在获取对象时,在DAO中缓存字段的先前状态.Get
  • 实现此属性,如果它更改,它将设置一个标志.
  • 考虑使此更改成为客户端调用的显式操作,而不是标志更改时调用的隐式操作.例如,如果此标志称为已激活",则实现激活"和停用"方法.此方法更改该标志并执行大量代码".该标志在世界其他地方是只读的.

这篇关于确定实体字段在NHibernate中是否已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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