使用Castle Windsor在QueryBus中查找查询的处理程序

问题描述 投票:0回答:1

我尝试使用CQRS创建应用程序并实现QueryBus。有我的查询:泛型和一个带处理程序的特定查询

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 { };
    }
}

我使用Windsor Castle来解决依赖关系

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

在我的QueryBus方法的实现

    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;
    }

我得到错误缺少组件,我的问题是我的QueryBus实现或组件注册有什么问题

没有用于支持服务的组件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]]

c# generics castle-windsor cqrs
1个回答
1
投票

看起来像PeriodPlanListQueryHandler注册暴露IQueryHandler<PeriodPlanListQuery, object>,但你正试图解决一个IQueryHandler<IQuery<object>, object>

两端都需要完全匹配才能正常工作。

现在你的模型不能工作,所以它可能需要重新调整。我建议忘记温莎一会儿,并尝试在没有库的普通C#中解决它。

当前的代码有效地尝试:

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

这不是有效的C#,也不会编译,这也是你的Windsor配置不能按预期工作的原因。

尝试找出一个满足您在Windsor之外的需求的模型,然后如何为该模型配置Windsor应该相当简单。

© www.soinside.com 2019 - 2024. All rights reserved.