代码之家  ›  专栏  ›  技术社区  ›  TheCatWhisperer

Castle Windsor-如何创建安装程序来处理多级依赖结构

  •  1
  • TheCatWhisperer  · 技术社区  · 8 年前

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

    我的解决方案如下:

    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> . 客户缓存 应该注射 CustomerRepository 以满足这种依赖性。

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

    1 回复  |  直到 8 年前
        1
  •  1
  •   TheCatWhisperer    8 年前

    这似乎是一个装饰图案。Castle认可按正确顺序注册的装修师。例如,注册时:

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

    然后解决 IGetData<Customer> 将返回 CustomerCache 用来装饰 CustomerRepository .