Question

I have some services and the interfaces for them in two separated assemblies, one for the interfaces and another for the implementations.

I'm trying to register them using Castle Windsor. It works perfectly using something like this:

 container.Register(Component
.For(IService1)
.ImplementedBy(Service1)
.LifeStyle.Transient());

 container.Register(Component
.For(IService2)
.ImplementedBy(Service2)
.LifeStyle.Transient());

The thing here is... is possible to register all components at once? Without having to register them one by one. I have seen that is possible to register multiple interfaces to a simple component, o multiple implementations for a single interface, but I haven't seen if it's possible to register multiple interfaces from one assembly with multiple implementations from another assembly.

I don't know if this has any relevance, but all interfaces have the same base(IService in this case).

Thanks a lot.

Was it helpful?

Solution

What you are looking for is Registering components by conventions. Basically what you do is that you declare what implementations Castle should consider, and what services it should associate those implementations with.

container.Register(
    // we want all concrete classes in this assembly
    Classes.FromThisAssembly()
    // but we filter them to keep only the ones in a specific namespace
    .InSameNamespaceAs<RootComponent>()
    // we register thoses classes to interfaces that match the classes names
    .WithService.DefaultInterfaces()
    // setting the lifestyles for all components in this batch
    .LifestyleTransient()
);

There are many other options for convention-based registration, the best way is to take a longer look at the linked page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top