[英]Register multiple IInterceptor in nhibernate
Is there any way to register multiple IInterceptor in nhibernate有什么方法可以在 nhibernate 中注册多个 IInterceptor
this dose not work in hhibernate 3.1.0.4000此剂量在休眠 3.1.0.4000 中不起作用
config.SetInterceptor(new ContextAwareInterceptor());
config.SetInterceptor(new ContextAwareCommandInterceptor());
config.SetInterceptor(new SqlInterceptor());
You can pull this off by using a composite interceptor.您可以使用复合拦截器来实现这一点。 The following is an implementation that's worked for me.
以下是对我有用的实现。 Just be aware that if you have multiple interceptors that implement a method that returns a value (Like
GetEntity()
), in some cases the first one is returned and others (Like FindDirty()
and OnPrepareStatement()
) the results are combined.请注意,如果您有多个拦截器实现返回值的方法(如
GetEntity()
),则在某些情况下会返回第一个拦截器,而其他情况下(如FindDirty()
和OnPrepareStatement()
)会将结果组合在一起。 Full listing with tests here .此处包含测试的完整列表。
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));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.