Pregunta

I have some executor-classes that implements one or two interfaces (IHandleMessages<> and/or CommandExecutor<>).

Can I register all these executor classes - with whichever interface(s) it implements of the two - as services. Without ending up having all other interfaces on the class as services too.

My initial attempt was this:

public class Test
{
    [Fact]
    public void SomeTest()
    {
        var container = new WindsorContainer();
        container.Register(Classes.FromThisAssembly().BasedOn(typeof(CommandExecutor<>)).WithService.Base().LifestyleTransient(),
                           Classes.FromThisAssembly().BasedOn(typeof(IHandleMessages<>)).WithService.Base().LifestyleTransient());

        container.ResolveAll<CommandExecutor<object>>().Count().ShouldBe(2);
        container.ResolveAll<IHandleMessages<object>>().Count().ShouldBe(2);
    }

    public interface IHandleMessages<T> { }
    public interface CommandExecutor<T> { }

    public class HandlesMessagesOnly : IHandleMessages<object> { }
    public class HandlesMessagesAndExecutesCommands : CommandExecutor<object>, IHandleMessages<object> { }
    public class ExecutesCommandsOnly : CommandExecutor<object> { }
}

But that does not work. Is there a solution for this?

I'm using Windsor 3.1.0.

EDIT: I guess what I'm really asking is: Is it possible to find the same type twice, and just have the discoverer add more services to that type's registration?

¿Fue útil?

Solución 2

I've made a pull request to Windsor which was accepted in 3.2, and you can now do this:

Container.Register(
   Classes.FromThisAssembly()
      .BasedOn<IFoo>()
      .OrBasedOn(typeof(IBar))
      .WithServiceBase()
  .LifestyleTransient());

Read more here

Otros consejos

This will make your test pass:

container.Register(
    Classes
        .FromThisAssembly()
        .BasedOn(typeof(CommandExecutor<>))
        .WithServiceBase()
        .WithServiceFirstInterface()     // Ensures first interface is included.
        .LifestyleTransient(),
    Classes
        .FromThisAssembly()
        .BasedOn(typeof(IHandleMessages<>))
        .WithServiceBase()
        .LifestyleTransient()
);

For more sophisticated interface selection techniques see this question.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top