Castle Windsor: How to specify a constructor parameter from code?

前端 未结 6 485
轻奢々
轻奢々 2020-12-30 08:34

Say I have the following class

MyComponent : IMyComponent {
  public MyComponent(int start_at) {...}
}

I can register an instance of it wit

6条回答
  •  攒了一身酷
    2020-12-30 09:15

    Have you considered using Binsor to configure your container? Rather than verbose and clumsy XML you can configure Windsor using a Boo based DSL. Here's what your config will look like:

    component IMyComponent, MyComponent:
       start_at = 1
    

    The advantage is that you have a malleable config file but avoid the problems with XML. Also you don't have to recompile to change your config as you would if you configured the container in code.

    There's also plenty of helper methods that enable zero friction configuration:

      for type in Assembly.Load("MyApp").GetTypes():
        continue unless type.NameSpace == "MyApp.Services"
        continue if type.IsInterface or type.IsAbstract or type.GetInterfaces().Length == 0
        component type.GetInterfaces()[0], type
    

    You can get started with it here.

提交回复
热议问题