如何让NHibernate的考虑属性始终脏使用动态更新或插入时? [英] How to make NHibernate considered property to always be dirty when using dynamic update or insert?

查看:165
本文介绍了如何让NHibernate的考虑属性始终脏使用动态更新或插入时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻求帮助与NHibernate的一个问题,这已被窃听我有一段时间了。长话短说:

I am looking for help on an issue with NHibernate which has been bugging me for a while now. Long story short:

我正在寻找一种方式,在第一级缓存,重置财产上的每个实体一次我做一个更新或插入。

I’m looking for a way to, in the first level cache, "reset" a property on an entity each time I do an update or an insert.

我想实现是有问题的属性将始终被认为是使用动态更新或插入时由NHibernate的脏。

What I want to achieve is that the property in question will always be considered to be dirty by NHibernate when using dynamic update or insert.

这背后的故事是,我知道,如果交易成功,我想重置列会由触发器设置为Null在数据库中。在另一面,第一级缓存并不知道这一点,因此NHibernate的会认为没有更新的属性时,我将它设置为相同的值,因为我做了以前的更新/插入。美中不足的是,我的触发取决于这个值被设置。由此产生的混乱是,如果我想使用动态更新或插入,我只能够更新/无爽快,它后来(我真的不想做),一旦插入一个实体。

The backstory for this is that I know that, if the transaction was successful, the column that I want to "reset" will be set to Null in the database by a trigger. On the flip side, the first level cache does not know this, and thus NHibernate will think that the property was not updated when I set it to the same value as I did on the previous update/insert. The catch is that my trigger is dependent on this value being set. The resulting mess is that if I want to use dynamic update or insert I’m only able to update/insert an entity once without "refreshing" it afterwards (which I really don’t want to do).

提示或帮助将不胜感激,因为我真的撞墙这里

Tips or help would be much appreciated, because I’ve really hit a wall here

推荐答案

NHibernate的提供了扩展的许多地方。其中之一是会议 IInterceptor 。有许多细节文档:

NHibernate provides many places for extension. Among them is the Session IInterceptor. There is documentation with many details:

http://nhibernate.info/doc/nh/en/index.html#objectstate-interceptors

在这种情况下,我们可以创建我们的自定义之一,这将是我们观察实体(例如客户端),并必须在每次更新一个属性(例如代码)。因此,我们的实现可能是这样的:

In this case, we can create our custom one, which will be observing our entity (for example Client) and a property which must be updated every time (for example Code). So our implementation could look like this:

public class MyInterceptor : EmptyInterceptor
{
    public override int[] FindDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
    {
        var result = new List<int>();

        // we do not care about other entities here
        if(!(entity is Client))
        {
            return null; 
        }

        var length = propertyNames.Length;

        // iterate all properties
        for(var i = 0; i < length; i++)
        {
            var areEqual = currentState[i].Equals(previousState[i]);
            var isResettingProperty = propertyNames[i] == "Code";

            if (!areEqual || isResettingProperty)
            {
                result.Add(i); // the index of "Code" property will be added always
            }
        }

        return result.ToArray();
    }
}

注:这只是一个例子!应用自己的逻辑来检查肮脏的属性。

和我们有包装会话这种方式

var interceptor = new MyInterceptor()
_configuration.SetInterceptor(interceptor);



这是它。而客户端标记为动态的更新,则该属性的代码总是被设置为

And this is it. While Client is marked as dynamic-update, the property Code will always be set as dirty

<class name="Client" dynamic-update="true" ...

这篇关于如何让NHibernate的考虑属性始终脏使用动态更新或插入时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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