Jacek
Jacek

Reputation: 12053

Finding handler for query in QueryBus with Castle Windsor

I try to create application with CQRS and implement QueryBus. There are my queries: generic and one specific query with handler

public interface IQuery<TResult> { }

public interface IQueryHandler<TQuery, TResult> where TQuery : IQuery<TResult>
{
    TResult Execute(TQuery query);
}

public class PeriodPlanListQuery : IQuery<object> { }

public class PeriodPlanListQueryHandler : IQueryHandler<PeriodPlanListQuery, object>
{
    public object Execute(PeriodPlanListQuery query)
    {
        return new { };
    }
}

I use Windsor Castle for resolving dependencies

        container.Register(
            Component.For<IQueryHandler<PeriodPlanListQuery, object>>()
                .ImplementedBy<PeriodPlanListQueryHandler>()
                .LifestylePerWebRequest());

There in my implementation of QueryBus method

    public TResult Send<TResult>(IQuery<TResult> query)
    {
        var handler = _container.Resolve<IQueryHandler<IQuery<TResult>, TResult>>();
        if (handler == null)
            throw new NotSupportedException(query.GetType().FullName);

        var result = handler.Execute(query);
        return result;
    }

I got error missing component, my question is what is wrong my implementation of QueryBus or registration in component

No component for supporting the service Domain.IQueryHandler2[[Domain.IQuery1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], JP.Planner.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] was found

Upvotes: 0

Views: 236

Answers (1)

Krzysztof Kozmic
Krzysztof Kozmic

Reputation: 27384

It looks like the PeriodPlanListQueryHandler is registered to expose IQueryHandler<PeriodPlanListQuery, object> but you're trying to resolve a IQueryHandler<IQuery<object>, object>.

Both ends need to match exactly for this to work.

Right now your model cannot work so it it may need to be readjusted. I'd suggest forget Windsor for a moment and try to work it out in just plain C# with no libraries.

The current code is effectively trying to do:

IQueryHandler<IQuery<object>, object> h = new PeriodPlanListQueryHandler();

That is not valid C# and will not compile, and that's also why your Windsor configuration doesn't work as you expected.

Try to figure out a model that meets your needs outside of Windsor, and then it should be fairly straightforward how to configure Windsor for that model.

Upvotes: 1

Related Questions