在 Windsor Installer 中使用强类型配置参数
我一直在绞尽脑汁安装 Windsor 容器< /a> 使用自定义配置对象。这看起来很简单,但显然有一些重要的东西我没有得到。如果您能帮助我填补这个空白,我将不胜感激。
我有一个配置类:
class MyConfiguration
{
int SomeIntValue;
DateTime SomeDateValue;
Action<string> SomeActionValue;
}
我想将这些配置值作为构造函数参数传递到注册的实现中。我想注册应该类似于:
public class MyInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For<IFoo>.ImplementedBy<Foo>
.Parameters(Parameter.ForKey("parameter1").Eq( INSERT VALUE HERE (?) );
}
}
那么我如何获取这些值并将它们传递到安装程序中?我应该使用这个 IConfigurationStore
参数吗?如果是这样,我该如何填写它以及如何处理它?
此外,似乎所有配置对象都只能存储字符串值,那么如何传递非字符串值(例如 DateTime
)?
谢谢,祝周末愉快。
I've been cracking my head over installing a Windsor container using a custom configuration object. It seems simple, but apparently there's something important I'm just not getting. I'll be grateful if you could help me fill this gap.
I have a configuration class:
class MyConfiguration
{
int SomeIntValue;
DateTime SomeDateValue;
Action<string> SomeActionValue;
}
I want to pass these configuration values as constructor parameters into the registered implementations. I guess the registration should look something like:
public class MyInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For<IFoo>.ImplementedBy<Foo>
.Parameters(Parameter.ForKey("parameter1").Eq( INSERT VALUE HERE (?) );
}
}
So how do I take these values and pass them into the installer? Should I use this IConfigurationStore
parameter? If so, how do I fill it up and what do I do with it?
Besides, it seems like all the configurations objects can only store string values, so how can I pass values which are not strings (such as DateTime
)?
Thanks and have a great weekend.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在绝大多数情况下,您不必显式注册构造函数参数。 自动装配功能应该能够自动为您处理这个问题。这将使您的代码不那么脆弱,从而更易于维护。
因此,您能做的最好的事情就是向容器注册 MyConfiguration。如果只有该类型的单个注册(正常情况),则容器可以明确地解析对该类型的任何请求。因此,如果另一个类将 MyConfiguration 作为构造函数参数,Castle Windsor 将自动为您匹配它们。您不需要明确指定这一点。
但是,在某些情况下,您需要显式分配特定的参数值。对于这些情况,您可以使用 ServiceOverrides。这可能看起来像这样:
如果您需要分配特定实例,则可以使用 DependsOn:
In the huge majority of cases you shouldn't have to explicitly register a constructor argument. The auto-wiring feature should be able to take care of that for you automatically. This will make your code less brittle and thus more maintainable.
So, the best you can do is simply to register MyConfiguration with the container. If this there's only a single registration of the type (the normal scenario), the container can unambiguously resolve any request for the type. Thus, if another class takes MyConfiguration as a constructor parameter, Castle Windsor will automatically match them for you. You don't need to specify this explicitly.
However, there are cases where you need to explicitly assign a particular parameter value. For those cases you can use ServiceOverrides. That might look something like this:
If you need to assign a specific instance, you can instead use DependsOn:
如果您尝试传递类型值,例如整数、字符串、枚举等,请使用 Mark 给出的第二个选项。
if you are trying to pass a type value, like an integer, string, enum etc then go with second option given by Mark..