实现IDataErrorInfo使用NHibernate的延迟加载情况下使用Castle.DynamicProxy [英] Implementing IDataErrorInfo using Castle.DynamicProxy in lazy loading scenario using NHibernate
问题描述
我已经实现了使用Castle.DynamicProxy IIterceptor IDataErrorInfo接口。我还实施了NHibernate的拦截器使用这个拦截该实例我的实体。问题是延迟加载实体。这些正在使用NHibernate的配置文件中指定的代理工厂类,这显然不提供IDataErrorInfo实施建造。这种代理是掩盖IDataErrorInfo的底层实现我的拦截,导致验证失败。
I have implemented IDataErrorInfo interface using Castle.DynamicProxy IIterceptor. I have also implemented a NHibernate interceptor which instantiates my entities using this interceptor. The problem is with lazy loaded entities. These are constructed using a proxy factory class specified in nhibernate config file, which obviously does not provide IDataErrorInfo implementation. This proxies are masking the underlying implementation of IDataErrorInfo by my interceptor which causes the validation to fail.
什么是这个问题的更多钞票的解决方案?
What are the posible solutions of this problem?
(解决问题的一种方法是改变默认代理工厂其中的nhibernate用途。)
(One way to solve the problem would be to change the default proxy factory which nhibernate uses.)
推荐答案
一个解决方案是覆盖缺省的城堡代理工厂和扩展接口阵列IDataErrorInfo。该LazyInitializer不actualy实现这个接口,它只是将调用转发到自己的代理实现。该 DataBindingProxyFactory
必须被登记为NHibernate的代理工厂。
One solution is to override the default Castle proxy factory and extend the interfaces array with IDataErrorInfo. The LazyInitializer does not actualy implement this interface, it only forwards calls to our own proxy implementation. The DataBindingProxyFactory
must then be registered as NHibernate proxy factory.
public class DataBindingProxyFactory : ProxyFactory, IProxyFactoryFactory
{
public IProxyValidator ProxyValidator { get { return new DynProxyTypeValidator(); } }
public IProxyFactory BuildProxyFactory()
{
return new DataBindingProxyFactory();
}
public bool IsInstrumented(Type entityClass)
{
return true;
}
public override INHibernateProxy GetProxy(object id, ISessionImplementor session)
{
try
{
var initializer = new LazyInitializer(EntityName, PersistentClass, id, GetIdentifierMethod,
SetIdentifierMethod, ComponentIdType, session);
var interfaces =
new List<Type>(Interfaces){
typeof (INotifyPropertyChanged),
typeof (IDataErrorInfo)
}.ToArray();
var generatedProxy = IsClassProxy
? DefaultProxyGenerator.CreateClassProxy(PersistentClass, interfaces, initializer)
: DefaultProxyGenerator.CreateInterfaceProxyWithoutTarget(Interfaces[0], interfaces,
initializer);
initializer._constructed = true;
return (INHibernateProxy) generatedProxy;
}
catch (Exception e)
{
log.Error("Creating a proxy instance failed", e);
throw new HibernateException("Creating a proxy instance failed", e);
}
}
}
这篇关于实现IDataErrorInfo使用NHibernate的延迟加载情况下使用Castle.DynamicProxy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!