如何在现有Castle Windsor注册中添加依赖项?

时间:2016-06-30 15:10:48

标签: c# castle-windsor

我们有一堆控制器,我们通过BasedOn查询注册Castle。我们希望在其中一个控制器上添加额外的配置依赖。我们可以在所有控制器上注册该参数。以下代码是我们解决问题的方法,但我想知道是否有更优雅/内置的解决方案。

public class ControllersInstaller : IWindsorInstaller
{
    private readonly IAppConfig _appConfig = new AppConfig();
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(FindControllers().Configure(ConfigureComponentRegistration).LifestyleTransient());

        container.Register(Component.For<BarController>()
            .LifestyleTransient()
            .DependsOn(Dependency.OnValue("faxNumber", 
            _appConfig.GetAppSetting<string>("FaxNumber"))));
    }

    private void ConfigureComponentRegistration(ComponentRegistration obj)
    {
        obj.LifestyleTransient();
    }

    private BasedOnDescriptor FindControllers()
    {
        return Classes.FromThisAssembly()
            .BasedOn<IController>()
            .If(Component.IsInSameNamespaceAs<FooController>(true))
            .If(t => t.Name.EndsWith("Controller") && t.Name != "BarController")                
            .LifestyleTransient();
    }
}

2 个答案:

答案 0 :(得分:2)

我建议您只使用更具体的版本覆盖现有注册,该版本具有_appConfig的必要依赖性。因此,您不必使用此过滤器:

#version 410\n
layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec2 vt
uniform mat4 view, proj, model;
out vec2 texture_coordinates;
void main() {
    texture_coordinates = vt;
    gl_Position = proj * view * model* vec4(vertex_position, 1.0);
};

在此处查看我的答案,了解如何覆盖现有组件:this

答案 1 :(得分:0)

你完全可以,use ConfigureFor<> method

container.Register(FindAllControllers()
   .ConfigureFor<BarController>(x =>
      x.DependsOn(Dependency.OnAppSettingsValue("faxNumber"))
   )
);

正如我所注意的那样,我不确定为什么你的例子指明生命三次。只做一次就足够了。