0
votes

I'm using Commands and CommandHandlers with Castel Windsor registration. There are some examples:

//command
public class DeleteEntityComand : ICommand
    {
        public int Id { get; set; }
    }

//command handler
public class DeleteEntityCommandHandler : ICommandHandler<DeleteEnityComand>
    {
    ...
    }

//registrations
Types.FromAssembly(Assembly.GetAssembly(typeof(DeleteEntityCommandHandler)))
                    .BasedOn(typeof(ICommandHandler<>))
                    .WithService.AllInterfaces().LifestyleTransient()

And it works perfect. But now I want to use generic command handler like this:

//command
public class SoftRemoveCommand<T> : ICommand
    {
        public int Id { get; set; }
    }

//command handler
public class SoftRemoveCommandHandler<T> : ICommandHandler<SoftRemoveCommand<T>>
    {
     ..
    }

But unfortunately in this case previous registration doesn't work. It tries to resolve my abstract BaseCommandHandler which is also implements ICommandHandler interface. So how can I register my generic command handlers properly?

Update:

After adding WithService.Base:

//registrations
Types.FromAssembly(Assembly.GetAssembly(typeof(DeleteEntityCommandHandler)))
                    .BasedOn(typeof(ICommandHandler<>))
                    .WithService.AllInterfaces()
                    .WithService.Base() 
                    .LifestyleTransient()

Castle is trying to resolve SoftRemoveCommandHandler<SoftRemoveCommand<T>> but not SoftRemoveCommandHandler<T> and it throws an exception "Coul not proxy".

Is it possible to make Castle resolve SoftRemoveCommandHandler<T>, not SoftRemoveCommandHandler<SoftRemoveCommand<T>>?

1
Could you may be post a compiling piece of code that shows the problem. Maybe a gist ?Marwijn

1 Answers

0
votes

can you post more of your code so I can better understand it and especially the .Resolve<> line that you are trying.

I assume your resolve line looks something like this: container.Resolve<ICommandHandler<SoftRemoveCommand<int>>() for example.

What I think the problem is that when Castle is trying to resolve this it only knows "one level" of the open generics - the one of the ...BasedOn(typeof(ICommandHandler<>))... You need to help castle resolve the "inner" level.

This article seems to solve this problem. See Krzysztof's answer and blog