Kebin Maharjan - 379 - Nov 17, 2014

Hi Demis,
Working on moving to totally move to ServiceStack. I’ve getting rid of ninject.
Question regarding resolving registered types. I’ve a following interface:

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

I register all types that implement ICommandHandler<> as follows:

GetType().Assembly.GetTypes()
    .Where(x => x.IsOrHasGenericInterfaceTypeOf(typeof(ICommandHandler <>)))
    .Each(x => container.RegisterAutoWiredType(x));

How can I then resolve or get all the CommandHandlers that implement certain command? 
This throws ResolutionException:
 _container.Resolve<ICommandHandler<TCommand>>();

In ninject I can get all the handlers using:

_kernel.GetAll<ICommandHandler<TCommand>>();

Thanks!

There’s no API that searches all dependencies but you already have the list of all command types at registration where you can just assign to a variable, e.g:

var commandTypes = GetType().Assembly.GetTypes()
    .Where(x => x.IsOrHasGenericInterfaceTypeOf(typeof(ICommandHandler <>))).ToList();

commandTypes.Each(x => container.RegisterAutoWiredType(x));

Kebin Maharjan:

That is true, but how do I resolve it from a controller? I cannot resolve with:
 ResolveService<ICommandHandler<CreateUserAccountCommand>>();
 

It’s dependency not a Service, so you would use TryResolve<ICommandHandler<CreateUserAccountCommand>

But the normal way to access IOC dependencies is to specify them as public properties (or ctor args):

public ICommandHandler<CreateUserAccountCommand> UserCommand { get; set; }

Kebin Maharjan:

Cool Thanks!