在 Windsor Installer 中使用强类型配置参数

发布于 2024-10-26 09:02:25 字数 943 浏览 3 评论 0原文

我一直在绞尽脑汁安装 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不甘平庸 2024-11-02 09:02:25

在绝大多数情况下,您不必显式注册构造函数参数。 自动装配功能应该能够自动为您处理这个问题。这将使您的代码不那么脆弱,从而更易于维护

因此,您能做的最好的事情就是向容器注册 MyConfiguration。如果只有该类型的单个注册(正常情况),则容器可以明确地解析对该类型的任何请求。因此,如果另一个类将 MyConfiguration 作为构造函数参数,Castle Windsor 将自动为您匹配它们。您不需要明确指定这一点。

但是,在某些情况下,您需要显式分配特定的参数值。对于这些情况,您可以使用 ServiceOverrides。这可能看起来像这样:

container.Register(Component.For<MyConfiguration>().Named("myConfig"));
container.Register(Component
    .For<IFoo>()
    .ImplementedBy<Foo>()
    .ServiceOverrides(new { parameter1 = "myConfig" }));

如果您需要分配特定实例,则可以使用 DependsOn:

var myConfig = new MyConfig();
container.Register(Component
    .For<IFoo>()
    .ImplementedBy<Foo>()
    .DependsOn(new { parameter1 = myConfig }));

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:

container.Register(Component.For<MyConfiguration>().Named("myConfig"));
container.Register(Component
    .For<IFoo>()
    .ImplementedBy<Foo>()
    .ServiceOverrides(new { parameter1 = "myConfig" }));

If you need to assign a specific instance, you can instead use DependsOn:

var myConfig = new MyConfig();
container.Register(Component
    .For<IFoo>()
    .ImplementedBy<Foo>()
    .DependsOn(new { parameter1 = myConfig }));
心清如水 2024-11-02 09:02:25

如果您尝试传递类型值,例如整数、字符串、枚举等,请使用 Mark 给出的第二个选项。

int val = 23;
container.Register(Component
    .For<IFoo>()
    .ImplementedBy<Foo>()
    .DependsOn(new { parameter1 = val }));

if you are trying to pass a type value, like an integer, string, enum etc then go with second option given by Mark..

int val = 23;
container.Register(Component
    .For<IFoo>()
    .ImplementedBy<Foo>()
    .DependsOn(new { parameter1 = val }));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文