是否可以在 Castle Windsor 中提供依赖于另一个组件的 属性 的服务?
Is it possible in Castle Windsor to make a service with dependency on property of another component?
假设我有 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));
假设我有 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));