代码之家  ›  专栏  ›  技术社区  ›  Jacek

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

  •  0
  • Jacek  · 技术社区  · 7 年前

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

    我使用温莎城堡解决依赖关系

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

        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或注册有什么问题

    没有支持服务的组件 域.IQueryHandler 2[[Domain.IQuery 1[[系统对象, mscorlib,版本=4.0.0.0,区域性=neutral, Culture=neutral,PublicKeyToken=null][系统对象,mscorlib, Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089]]

    1 回复  |  直到 7 年前
        1
  •  1
  •   Krzysztof Kozmic    7 年前

    PeriodPlanListQueryHandler 已注册公开 IQueryHandler<PeriodPlanListQuery, object> IQueryHandler<IQuery<object>, object>

    现在你的模型不能工作,所以可能需要重新调整。我建议暂时忘掉温莎,试着在没有库的普通C中解决它。

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

    这是无效的C#并且不会编译,这也是为什么您的Windsor配置不能按预期工作的原因。

    尝试找出一个模型,以满足您的需求以外的温莎,然后它应该是相当简单的如何配置温莎的模型。