Registering Generic decorators in castle windsor?

105 views
Skip to first unread message

Farhad Taran

unread,
Apr 14, 2015, 9:15:34 AM4/14/15
to castle-pro...@googlegroups.com
I am trying to decorate my command handlers using castle windsor but it seems that my registrations are not correct as the class is not decorated.

the other question I have is should my decorators live in the composition root or in the same assembly of the class that they are decorating?

right now I have moved them to the composition root as castle windsor was trying to register my decorators along with the other classes and I would get the error:

    Component TempSearch.Command.Data.Decorators.TransactionCommandHandlerDecorator`1 could not be registered. There is already a component with that name. Did you want to modify the existing component instead? If not, make sure you specify a unique name.

another issue is that I am declaring my IDbConnection as per web request and my commandHandlers depend on my IDbConnection but the command handlers are being registered as singletons even though I explicitly declare them as per web request.


I have the following installer:

    internal class CommandsInstaller : IWindsorInstaller
        {
            public void Install(IWindsorContainer container, IConfigurationStore store)
            {
                container.Register(
                    Component.For<IDbConnection>()
                        .UsingFactoryMethod(() => ConnectionHelper.GetOpenDbConnection(Connection.DatabaseName.ReedOnline))
                        .LifestylePerWebRequest());
    
                container.Register(
                    Classes
                        .FromAssemblyContaining<EcruiterCommands>()
                        .Where(t => t.Name.EndsWith("Commands"))
                        .WithService
                        .AllInterfaces().LifestylePerWebRequest());
    
                container.Register(
                    Classes
                        .FromAssemblyContaining<EcruiterCommands>()
                        .BasedOn(typeof (ICommandHandler<>))
                        .WithService.AllInterfaces()
                        .LifestylePerWebRequest());
    
                container.Register(Component.For(typeof(ICommandHandler<>))
                    .ImplementedBy(typeof(TransactionCommandHandlerDecorator<>))
                    .LifestylePerWebRequest());
    
                //var metadataProviderContributorsAssemblies = new[] {typeof (EcruiterCommands).Assembly};
               
            }
        }
and this is my decorator:

    namespace TempSearch.Ioc.Decorators.CommandHandlers
    {
        public class TransactionCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand>
        {
            private readonly ICommandHandler<TCommand> decorated;
    
            public TransactionCommandHandlerDecorator(ICommandHandler<TCommand> decorated)
            {
                this.decorated = decorated;
            }
    
            public void Handle(TCommand command)
            {
                using (var scope = new TransactionScope())
                {
                    decorated.Handle(command);
    
                    scope.Complete();
                }
            }
        }
    }


![enter image description here][1]


 
Reply all
Reply to author
Forward
0 new messages