2009-02-24 26 views
4

我使用Unity为可插拔体系结构动态地解析类型。我还使用拦截通过AOP应用业务规则验证(使用ValidationAspects)。最后,我使用NHibernate作为ORM来坚持域对象。在Unity下使用代理获取实际实例使用NHibernate拦截

为了使AOP工作,我们使用VirtualMethodInterceptor,因为接口拦截不适用于NHibernate。我有一个超过ISession的外观,它处理存储库操作的接口和实际类型之间的转换。

要确保在图中通过NHibernate的获取所有对象都正确代理的AOP,我做了一个NH IInterceptor实施和推翻了Instantiate()方法,所以我可以创建的对象提供NH,而不是把它叫new()。然后,我使用Container.Resolve()来取回代理对象并注入验证,并将其返回给NH来填充。这工作正常。

发生会话刷新时出现问题。 NHibernate会因为它在图形中看到的对象是代理类型而不是真实类型而感到不安。我们映射的方式(全部通过属性,所有虚拟)NH应该能够通过代理获得它需要的所有值,如果我可以重写类型检查。

我需要知道的是:给定一个由Unity创建的透明代理对象并且启用了拦截,是否有一种方法可以获得对它的代理的“真实”实例的直接引用,或者b)覆盖NH和告诉它在运行时动态地将待处理对象的对象视为已知的映射类型?

+0

你能建立这样一个基本的测试案例?你有没有考虑使用NHibernate.Validator而不是这个? (http://nhforge.org/wikis/validator10/nhibernate-validator-1-0-0-documentation.aspx) – 2009-02-24 22:30:00

回答

0

我们使用拦截进行缓存。所以在我们班,实现ICallHandler我们有这样的代码:

public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) 
    { 
     //... 
     var nextHandler = getNext(); 

     var realReturn = nextHandler(input, getNext); 

     var cacheRefreshingItemParameters = new CacheRefreshingItemParameters 
     { 
      InterfaceMethod = input.MethodBase, 
      InterfaceType = input.MethodBase.DeclaringType, 
      TargetType = input.Target.GetType() //remember original type 
     }; 
     _cacheProvider.Add(cacheKey, realReturn.ReturnValue, cacheRefreshingItemParameters); 

     //... 
     return (cachedReturn); 
    } 

装进缓存UpdateCallback cacheRefreshingItemParameters然后解决原有业务:

var service = _unityContainer.Resolve(parameters.TargetType);