开发者

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

开发者 https://www.devze.com 2023-01-09 23:17 出处:网络
in开发者_StackOverflow code i can do some thing like this: container.Register(AllTypes.FromAssemblyNamed(\"AssemblyName\"));

in开发者_StackOverflow code i can do some thing like this:

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

can i do the same thing using Configuration file "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).

You can have each of your modules reference Windsor, and each module have its own set of Installers.

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()
);


I am pretty sure that only with the fluent configuration API can you set up conventions for your application so that as you create new components you aren’t required to register them individually, as you example shows.


You can write a trivial facility to do that, e.g.:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号