3
votes

I have got the following interface definition:

public interface ICommandHandler
{
    ILogger Logger { get; set; }
    bool SendAsync { get; set; }
}

I have multiple implementations that implement ICommandHandler and need to be resolved. While Castle Windows automatically injects the Logger property, when an ILogger is injected, I can't find a way to configure the SendAsync property to be set to true by Windsor during the creation of new instances.

UPDATE

The command handlers implement a generic interface that inherits from the base interface:

public interface ICommandHandler<TCommand> : ICommandHandler
    where TCommand : Command
{
     void Handle(TCommand command);
}

Here is my configuration:

var container = new WindsorContainer();

container.Register(Component
     .For<ICommandHandler<CustomerMovedCommand>>()
     .ImplementedBy<CustomerMovedHandler>());
container.Register(Component
     .For<ICommandHandler<ProcessOrderCommand>>()
     .ImplementedBy<ProcessOrderHandler>());
container.Register(Component
     .For<ICommandHandler<CustomerLeftCommand>>()
     .ImplementedBy<CustomerLeftHandler>());

What ways are there to do this with Castle Windsor?

1

1 Answers

8
votes
.DependsOn(Proprerty.ForKey("sendAsync").Eq(true))

or with anonymous type

.DependsOn(new{ sendAsync = true })

regarding updated question, it seeems what you need is along the following lines:

container.Register(AllTypes.FromThisAssembly()
   .BasedOn(typeof(ICommandHandler<>))
   .WithService.Base()
   .Configure(c => c.DependsOn(new{ sendAsync = true })
                    .Lifestyle.Transient));

That way you register and configure all handlers in one go, and as you add new ones you won't have to go back to add them to registration code.

I suggest reading through the documentation as there's much more to the convention-based registration API than this.