简体   繁体   English

Windsor-Castle:使用配置文件在程序集中注册所有类型

[英]Windsor-Castle: Register All Types in assembly using config file

in code i can do some thing like this: 在代码中我可以做这样的事情:

container.Register(AllTypes.FromAssemblyNamed("AssemblyName"));

can i do the same thing using Configuration file "Windsor.Config"??? 我可以使用配置文件“Windsor.Config”做同样的事情???

Responding to your comment. 回应你的评论。

There's also a 3rd way (in Windsor 2.5, currently in beta 2 - final release is expected very soon). 还有第三种方式(在Windsor 2.5中,目前处于测试阶段2 - 预计很快就会发布)。

You can have each of your modules reference Windsor, and each module have its own set of Installers. 您可以让每个模块都引用Windsor,每个模块都有自己的一组安装程序。

Than you can use the new directory scanning capability to install components from all these assemblies: 您可以使用新的目录扫描功能来安装所有这些程序集中的组件:

// In your root assembly
var container = new WindsorContainer();
container.Install(   
   FromAssembly.This(),
   FromAssembly.InDirectory(new AssemblyFilter("Modules")),
   Configuration.FromAppConfig()
)

In addition if you have components following identical structure you can also register components from multiple assemblies in single installer. 此外,如果您的组件具有相同的结构,则还可以在单​​个安装程序中注册来自多个组件的组件。 See more here. 在这里查看更多。

container.Register(
   AllTypes.FromAssemblyInDirectory(new AssemblyFilter("Modules"))
      .Where(t=>t.Namespace.EndsWith(".Services"))
      .WithService.DefaultInterface()
);

我非常确定只有使用流畅的配置API才能为您的应用程序设置约定,以便在创建新组件时不需要单独注册它们,如示例所示。

You can write a trivial facility to do that, eg: 你可以编写一个简单的工具来做到这一点,例如:

AllTypesConfig.xml AllTypesConfig.xml

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <facilities>
    <facility id="alltypes">
      <assemblies>
        <item>Castle.Core</item>
      </assemblies>
    </facility>
  </facilities>
</configuration>

code: 码:

public class AllTypesFacility : AbstractFacility {
    protected override void Init() {
        var asmList = FacilityConfig.Children["assemblies"].Children;
        foreach (var asm in asmList)
            Kernel.Register(AllTypes.FromAssemblyNamed(asm.Value).Pick());
    }
}


var container = new WindsorContainer(@"..\..\AllTypesConfig.xml");
container.AddFacility("alltypes", new AllTypesFacility());
container.Resolve<NullLogger>();

If you need more flexibility it will get progressively harder to represent the fluent config in XML. 如果您需要更大的灵活性,那么在XML中表示流畅的配置将变得越来越难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM