代表封闭泛型类型 IQueryHandler 的 2 个类型

2 types that represent the closed generic type IQueryHandler

鉴于以下情况:-

public abstract class PriceBookDataViewModelQuery : IQuery<PriceBookDataViewModel>
{
    public bool IsRenewal { get; set; }
    public string Postcode { get; set; }
}


public class PriceBookDataSseElecViewModelQuery : PriceBookDataViewModelQuery
{
}


public class PriceBookDataSseGasViewModelQuery : PriceBookDataViewModelQuery
{
}

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

在我的容器配置中我有

Container.RegisterManyForOpenGeneric(
  typeof(IQueryHandler<,>), typeof(IQueryHandler<,>).Assembly);

我的查询处理程序是:-

public class PriceBookDataSseElecViewModelHandler :
    IQueryHandler<PriceBookDataViewModelQuery, PriceBookDataViewModel>

public class PriceBookDataSseGasViewModelHandler : 
    IQueryHandler<PriceBookDataViewModelQuery, PriceBookDataViewModel>

我收到以下错误消息:

There are 2 types that represent the closed generic type IQueryHandler. Types: PriceBookDataSseElecViewModelHandler and PriceBookDataSseGasViewModelHandler. Either remove one of the types or use an overload that takes an BatchRegistrationCallback delegate

我理解为什么会发生这种情况,因为我有两个继承自同一个 IQueryHandler 的类型。然而,最终我将有大约 20 个这样的查询,它们将包含相同的属性,因此旨在保持干燥。

除了在 20 查询 类 上复制属性外,我还有哪些选择?如何在我的场景中使用 BatchRegistrationCallback

由于担心对这个问题有点困惑,所以我要说的是:

I understand why this happens as I have two types that inherit from the same IQueryHandler. However eventually I will have around 20 of these queries that will contain the same properties so aiming to stay dry.

如果您有多个具有相同属性的处理程序,为什么不直接将共享属性移出抽象 class 并定义单独且唯一的处理程序?

有时我喜欢将 "DRY" 视为 "Do Repeat Yourself",如果它有效且易于维护和理解,请重复一遍。多 10 class 没关系,因为它们被分成单一职责。

public abstract class PriceBookDataViewModelQuery  {

    public bool IsRenewal { get; set; }
    public string Postcode { get; set; }   
}

public PriceBookDataSseElecViewModelQuery
    : IQuery<PriceBookDataViewModel>, PriceBookDataViewModelQuery {

    // Extra properties
}

public class PriceBookDataSseGasViewModelQuery
    : IQuery<PriceBookDataViewModel>, PriceBookDataViewModelQuery
{
    // Extra properties
}

// First handler 
public class PriceBookDataSseElecViewModelHandler
    : IQueryHandler<PriceBookDataSseElecViewModelQuery, PriceBookDataViewModel>
{
}

// Second handler
public class PriceBookDataSseGasViewModelHandler
    : IQueryHandler<PriceBookDataSseGasViewModelQuery, PriceBookDataViewModel> 
{
}