0

I dont know how map interface with two implementations, can help.

public interface IQuery
{

}

public interface IQueryResult
{
}

public interface IQueryHandler<in TParameter, out TResult> where TResult : IQueryResult where TParameter : IQuery
{        
    TResult Retrieve(TParameter query);
}

public interface IQueryDispatcher
{           
    TResult Dispatch<TParameter, TResult>(TParameter query)
        where TParameter : IQuery
        where TResult : IQueryResult;
}

public class QueryDispatcher : IQueryDispatcher
{
    private readonly IUnityContainer _container;

    public QueryDispatcher(IUnityContainer container)
    {
        if (container == null)
        {
            throw new ArgumentNullException("container");
        }
        _container = container;
    }

    public TResult Dispatch<TParameter, TResult>(TParameter query)
        where TParameter : IQuery
        where TResult : IQueryResult
    {
        var handler = _container.Resolve<IQueryHandler<TParameter, TResult>>();
        return handler.Retrieve(query);
    }

}

static void Main(string[] args)
{
    using (var container = new UnityContainer())
    {               
        container.RegisterType<IQueryDispatcher, QueryDispatcher>(new HierarchicalLifetimeManager());

        container.RegisterTypes(AllClasses.FromAssemblies(typeof(IQueryHandler<,>).Assembly), WithMappings.FromAllInterfaces, WithName.TypeName, WithLifetime.Hierarchical);

        var _queryDispatcher = container.Resolve<IQueryDispatcher>();
        TasksByStatusQuery query = new TasksByStatusQuery { IsCompleted = true };
        var result = _queryDispatcher.Dispatch<TasksByStatusQuery, TasksByStatusQueryResult>(query);

    }
}

Exception:

Resolution of the dependency failed, type = "CQRSUnity.Cqrs.IQueryHandler`2[CQRSUnity.Query.TasksByStatusQuery,CQRSUnity.QueryResult.TasksByStatusQueryResult]", name = "(none)".

Update:

I can try register each:

container.RegisterType(typeof(IQueryHandler<,>), typeof(TasksByStatusQueryHandler), new HierarchicalLifetimeManager());
container.RegisterType(typeof(IQueryHandler<,>), typeof(TasksByDateQueryHandler), new HierarchicalLifetimeManager());

Exception:

Unable to cast object of type 'CQRSUnity.QueryHandler.TasksByDateQueryHandler' to type 'CQRSUnity.Cqrs.IQueryHandler`2[CQRSUnity.Query.TasksByStatusQuery,CQRSUnity.QueryResult.TasksByStatusQueryResult]'.

davidterra
  • 301
  • 3
  • 15
  • You registered both just fine. The real question is what you expect to do. Do you want to run ALL the `IQueryHandler` for each query, or is there some magic you omitted to say WHICH `IQueryHandler` you really want? You said `WithName.TypeName`, and since there are more than one, there is no "default" `IQueryHandler` without a name. – Eris May 19 '16 at 19:06
  • I want say wich `IQueryHandler` I want execute. Do I need register all implemented `IQueryHandler`? – davidterra May 19 '16 at 19:43
  • 1
    If you want a specific `IQueryHandler`, then use the explicit type name. You don't want an `IQueryHandler`, you want a `TaskByStatusQueryHandler` – Eris May 19 '16 at 19:48
  • Take a look at this question. There's a number of ways to choose which implementation to use. Named registrations is one of them. http://stackoverflow.com/questions/37184286/inject-require-object-depends-on-condition-in-constructor-injection/37184742#37184742 – smoksnes May 20 '16 at 05:06
  • Or this question. Similiar to what you're trying to do. http://stackoverflow.com/questions/34912128/unity-with-the-same-interface-multiple-factories/34945492#34945492 – smoksnes May 20 '16 at 05:09

0 Answers0