c# - 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
以满足这种依赖关系。
温莎城堡有没有一种简单的方法来处理这种情况?我是否必须特别注意避免它可能认为是循环引用的错误?
解决方案
这似乎是一种装饰器模式。当以正确的顺序注册时,Castle 会识别装饰器。例如,当注册时:
container.Register(Component.For<IGetData<Customer>>().ImplementedBy<CustomerCache>());
container.Register(Component.For<IGetData<Customer>>().ImplementedBy<CustomerRepository>());
然后解析IGetData<Customer>
将返回CustomerCache
which decorates CustomerRepository
。
推荐阅读
- reactjs - 使用 Antd 和 redux-form-antd 进行自定义验证
- c# - 无法加载文件或程序集“MediaToolkit”
- c# - Xamarin.Forms.Xaml.XamlParseException:位置 10:18。未找到键 TwoButtonTopBarTemplate 的静态资源
- vba - XMLHTTP with Hash character
- amazon-web-services - 未能通过 iam:PassedToService 为 CodeBuild 传递角色
- php - 如何将多个参数传递给 laravel 命名资源路由
- selenium - 会话在尝试切换到新的弹出窗口时中断
- angularjs - 如何使用for循环和打字稿从api值制作数组数据
- crystal-reports - 当导出为 pdf 时,它会给出错误缺少参数值
- javascript - 动态设置输入仍然使表单无效