2017-04-14 44 views
0

我正在使用温莎城堡。我有一个公务舱和配置以下的拦截器:在温莎城堡自定义IDependencyResolver中进行延迟初始化

windsorContainer.Register(Component.For<IMyBusinessInterface>() 
    .ImplementedBy<MyBusinessClass>() 
    .Interceptors<MyInterceptorAttribute>() 
    .LifestyleTransient()); 

IMyBusinessInterfaceMyBusinessClass是项目Business内。 MyInterceptorAttribute在项目Interceptors内。 Business项目参考Interceptors项目。

MyInterceptorAttribute用作数据注解上的MyBusinessClass

[MyInterceptorAttribute] 
public class MyBusinessClass 
{ 
    public IUnitOfWork uow { get; set; } 
    public MyBusinessClass(IUnitOfWork uow) 
    { 
     uow = uow; 
    } 

    [MyInterceptorAttribute] 
    public MyReturnValue MyBusinessMethod(MyArguments arguments) 
    { 
     //... 
    } 
} 

我需要通过(或检索)的MyInterceptorAttribute相同的实例的unit of work已在容器内实例化的内部的方法温莎城堡。

在这一刻我传递了两个不同的实例。

我无法检索MyInterceptorAttribute中的工作单元实例,因为要获取invocation.InvocationTarget我会创建一个循环引用。

public class MyInterceptorAttribute : IInterceptor 
{ 
    public void Intercept(IInvocation invocation) 
    { 
     IMyBusinessInterface obj = invocation.InvocationTarget as MyBusinessClass; //I cannot do this because of circular reference 

     //obj.uow = .. 

    } 
} 

所以我想通过依赖注入添加工作单位的公共财产MyInterceptorAttribute内,领取营业类的工作单位的同一个实例......是这样的:

public class MyInterceptorAttribute : IInterceptor 
{ 
    public IUnitOfWork uow { get; set; } 

    public void Intercept(IInvocation invocation) 
    { 
     //uow...    
    } 
} 

但在这一刻我得到两个不同的实例。是否有可能得到相同的实例修改,例如windsor种姓配置...我想使用DependsOnDynamicParameters或类似的东西?

谢谢

回答

0

,而不是实施你的拦截器作为一个属性,它是由.NET运行库实例化,因此不能由温莎依赖注入,尝试使用标准[Interceptor(typeof(MyInterceptor))]属性。

然后,您可以在Windsor注册MyInterceptor,无论您希望的任何生活方式以及Unit Of Work的依赖注入或任何其他依赖项都将按预期工作。