c# - 是否可以在 CaSTLe Windsor 中提供依赖于另一个组件属性的服务?

标签 c# castle-windsor ioc-container

假设我有 ISettings 和属性 string Setting1 并且我有

public class MyComponent : IMyService
{
    public MyComponent(string setting1)
    {
        // set fields
    }
}

是否可以连接 Windsor 说应该使用 ISettings.Setting1 来满足 MyComponent 的依赖性?

最佳答案

我会建议 2 个选项。

首先,使用 ISettings 作为依赖,必要时使用 Setting1

public class MyComponent : IMyService
{
    public MyComponent(ISettings settings)
    {
        // access settings.Setting1
    }
}

其次,Windsor .DependsOn 函数将一些原始属性传递给组件。

container.Register(
                Component.For<IMyService >()
                .ImplementedBy<MyComponent >()
                .DependsOn(Dependency.OnValue("setting1", ISettingsInstance.Setting1));

关于c# - 是否可以在 CaSTLe Windsor 中提供依赖于另一个组件属性的服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38527343/

相关文章:

dependency-injection - 在 Prism 应用程序 DI 容器中注册装饰类型

dependency-injection - NInject 中的 Container.GetAllInstances<T> 等价物是什么?

dependency-injection - IoC 上下文中基于代码的注册与设计时注册

c# - 调用 method_getImplementation 在设备上抛出 ExecutionEngineException

c# - Horizo​​ntalLineAnnotation 调整大小

c# - 带有 IXmlSerializable 的命名空间前缀

nhibernate - 从域对象访问服务的规则

c# - 温莎城堡 :- Inject Dictionary of Interfaces via configuration

c# - CaSTLe DynamicProxy 接口(interface)代理生成

c# - 返回到 UWP XAML 页面 ContentDialog.RunAsync() 后导致 'Value does not fall within the expected range.'