【问题标题】:Is it possible in Castle Windsor to make a service with dependency on property of another component?温莎城堡是否可以提供依赖于另一个组件属性的服务?
【发布时间】:2016-07-22 13:10:53
【问题描述】:

假设我有 ISettingsstring Setting1 属性,我有

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

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

【问题讨论】:

  • 是的,这是可能的。见kozmic.net/2010/02/15/…,但为什么没有MyComponent 只是将ISettings 作为构造函数参数?
  • @PhilDegenhardt - 你想要提供这个链接的功劳吗?如果您将其发布为答案,我会将其标记为已接受。

标签: c# castle-windsor ioc-container


【解决方案1】:

我会建议 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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多