首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在nhibernate中注册多个IInterceptor

在nhibernate中注册多个IInterceptor
EN

Stack Overflow用户
提问于 2011-07-02 19:28:32
回答 2查看 2K关注 0票数 2

有没有办法在nhibernate中注册多个IInterceptor?

这在hhibernate 3.1.0.4000中不起作用

代码语言:javascript
复制
config.SetInterceptor(new ContextAwareInterceptor());
config.SetInterceptor(new ContextAwareCommandInterceptor());
config.SetInterceptor(new SqlInterceptor());
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-02 20:20:09

你不能。一个会话只能有一个拦截器。

相反,您应该研究一下Events

票数 2
EN

Stack Overflow用户

发布于 2015-10-23 03:35:37

您可以通过使用复合拦截器来完成此任务。下面是一个为我工作的实现。请注意,如果您有多个实现返回值的方法的拦截器(如GetEntity()),在某些情况下会返回第一个拦截器,而其他拦截器(如FindDirty()OnPrepareStatement())则会组合返回结果。包含测试here的完整列表。

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using MoreLinq;
using NHibernate;
using NHibernate.SqlCommand;
using NHibernate.Type;

public class CompositeInterceptor : IInterceptor
{
    private readonly IEnumerable<IInterceptor> _interceptors;

    public CompositeInterceptor(IEnumerable<IInterceptor> interceptors)
    {
        _interceptors = interceptors.ToList();
    }

    public void AfterTransactionBegin(ITransaction tx)
    {
        _interceptors.ForEach(x => x.AfterTransactionBegin(tx));
    }

    public void AfterTransactionCompletion(ITransaction tx)
    {
        _interceptors.ForEach(x => x.AfterTransactionCompletion(tx));
    }

    public void BeforeTransactionCompletion(ITransaction tx)
    {
        _interceptors.ForEach(x => x.BeforeTransactionCompletion(tx));
    }

    public int[] FindDirty(object entity, object id, object[] currentState, 
        object[] previousState, string[] propertyNames, IType[] types)
    {
        var results = _interceptors
            .Select(interceptor => interceptor.FindDirty(entity, id,
                currentState, previousState, propertyNames, types))
            .Where(result => result != null)
            .SelectMany(x => x)
            .Distinct()
            .ToArray();
        return !results.Any() ? null : results;
    }

    public object GetEntity(string entityName, object id)
    {
        return _interceptors
            .Select(interceptor => interceptor.GetEntity(entityName, id))
            .FirstOrDefault(result => result != null);
    }

    public string GetEntityName(object entity)
    {
        return _interceptors
            .Select(interceptor => interceptor.GetEntityName(entity))
            .FirstOrDefault(result => result != null);
    }

    public object Instantiate(string entityName, EntityMode entityMode, object id)
    {
        return _interceptors
            .Select(interceptor => interceptor.Instantiate(entityName, entityMode, id))
            .FirstOrDefault(result => result != null);
    }

    public bool? IsTransient(object entity)
    {
        return _interceptors
            .Select(interceptor => interceptor.IsTransient(entity))
            .FirstOrDefault(result => result != null);
    }

    public void OnCollectionRecreate(object collection, object key)
    {
        _interceptors.ForEach(x => x.OnCollectionRecreate(collection, key));
    }

    public void OnCollectionRemove(object collection, object key)
    {
        _interceptors.ForEach(x => x.OnCollectionRemove(collection, key));
    }

    public void OnCollectionUpdate(object collection, object key)
    {
        _interceptors.ForEach(x => x.OnCollectionUpdate(collection, key));
    }

    public void OnDelete(object entity, object id, object[] state, 
        string[] propertyNames, IType[] types)
    {
        _interceptors.ForEach(x => x.OnDelete(entity, id, state, propertyNames, types));
    }

    public bool OnFlushDirty(object entity, object id, object[] currentState, 
        object[] previousState, string[] propertyNames, IType[] types)
    {
        return _interceptors.Count(interceptor => interceptor.OnFlushDirty(
            entity, id, currentState, previousState, propertyNames, types)) > 0;
    }

    public bool OnLoad(object entity, object id, object[] state, 
        string[] propertyNames, IType[] types)
    {
        return _interceptors.Count(interceptor => interceptor.OnLoad(
            entity, id, state, propertyNames, types)) > 0;
    }

    public SqlString OnPrepareStatement(SqlString sql)
    {
        return _interceptors.Aggregate(sql, (current, interceptor) => 
            interceptor.OnPrepareStatement(current));
    }

    public bool OnSave(object entity, object id, object[] state, 
        string[] propertyNames, IType[] types)
    {
        return _interceptors.Count(interceptor => interceptor.OnSave(
            entity, id, state, propertyNames, types)) > 0;
    }

    public void PostFlush(ICollection entities)
    {
        _interceptors.ForEach(x => x.PostFlush(entities));
    }

    public void PreFlush(ICollection entities)
    {
        _interceptors.ForEach(x => x.PreFlush(entities));
    }

    public void SetSession(ISession session)
    {
        _interceptors.ForEach(x => x.SetSession(session));
    }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6556727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档