首页 > 解决方案 > Castle Windsor - 如何创建一个安装程序来处理多级依赖结构

问题描述

我有一个案例,几个不同的类从同一个接口继承。此外,有时从接口继承的类也将其作为依赖项。

我的解决方案如下所示:

InterfaceAssembly
    -IGetData<T>

DataAssembly
    -Repository.CustomerRepository : IGetData<Customer>
    -Repository.ProductRepository : IGetData<Product>
    -Cache.CustomerCache : IGetData<Customer>
    -Cache.ProductCache : IGetData<Product>

我想制作一个将Cache命名空间优先于命名空间的安装程序Repository。但是,在 的情况下CustomerCache,它既实现了 ,又依赖于IGetData<Customer>CustomerCache应该注入CustomerRepository以满足这种依赖关系。

温莎城堡有没有一种简单的方法来处理这种情况?我是否必须特别注意避免它可能认为是循环引用的错误?

标签: c#.netdependency-injectioninversion-of-controlcastle-windsor

解决方案


这似乎是一种装饰器模式。当以正确的顺序注册时,Castle 会识别装饰器。例如,当注册时:

container.Register(Component.For<IGetData<Customer>>().ImplementedBy<CustomerCache>());
container.Register(Component.For<IGetData<Customer>>().ImplementedBy<CustomerRepository>());

然后解析IGetData<Customer>将返回CustomerCachewhich decorates CustomerRepository


推荐阅读