命令模式中的类型错误

Type error in command pattern

我正在尝试为命令本身实现一个具有强类型输入和输出参数的命令模式。

首先,我创建了两个接口来标记命令的输入和输出:

interface IRequest { }
interface IResponse { }

然后我创建了基础 classes 和接口。这是抽象接收器

interface IReceiver<TRequest, TResponse>
    where TRequest : IRequest
    where TResponse : IResponse
{
    TResponse Action( TRequest request );
}

这是抽象命令

abstract class AbstractCommand<TRequest, TResponse>
    where TRequest : IRequest
    where TResponse : IResponse
{
    protected IReceiver<TRequest, TResponse> _receiver;

    public AbstractCommand( IReceiver<TRequest, TResponse> receiver ) {
        _receiver = receiver;
    }

    public abstract TResponse Execute( TRequest request );
}

现在我正在尝试使用这些对象,因此我创建了所需的具体 classes

class TypeARequest : IRequest
{
    public TypeARequest() {
    }

    public int NumericValueA { get; set; }
    public int NumericValueB { get; set; }
}

class TypeAResponse : IResponse
{
    public TypeAResponse() {
    }

    public int Result { get; set; }
}

class SumCommand : AbstractCommand<TypeARequest, TypeAResponse>
{
    public SumCommand( IReceiver<TypeARequest, TypeAResponse> receiver ) : base( receiver ) {
    }

    public override TypeAResponse Execute( TypeARequest request ) {
        return _receiver.Action( request );
    }
}

class SumReceiver : IReceiver<TypeARequest, TypeAResponse>
{
    public TypeAResponse Action( TypeARequest request ) {
        return new TypeAResponse() { Result = request.NumericValueA + request.NumericValueB };
    }

}

最后我创建了一个 CommandProcessor class 它应该能够同时处理多个命令

class CommandProcessor
{
    IList<AbstractCommand<IRequest, IResponse>> _supportedCommands = new List<AbstractCommand<IRequest, IResponse>>();

    public CommandProcessor() {
    }

    void AddSupportedCommand( AbstractCommand<IRequest, IResponse> item ) {
        _supportedCommands.Add( item );
    }

    void SetupSupportedCommands() {
        // ERROR HERE 
        AddSupportedCommand( new SumCommand( new SumReceiver() ) );
    }

}

但是我收到一个编译时错误:

Argument 1: cannot convert from 'SumCommand' to 'AbstractCommand'

有什么帮助或建议吗?

您应该创建一个接口并将您的泛型参数标记为协方差示例:

interface IRequest { }
interface IResponse { }
interface IReceiver<in TRequest, out TResponse>
    where TRequest : IRequest
    where TResponse : IResponse
{
    TResponse Action(TRequest request);
}

interface ICommand<out TRequest, out TResponse>
{

}

abstract class AbstractCommand<TRequest, TResponse> : ICommand<TRequest, TResponse>
    where TRequest : IRequest   
    where TResponse : IResponse
{
    protected IReceiver<TRequest, TResponse> _receiver;

    public AbstractCommand(IReceiver<TRequest, TResponse> receiver)
    {
        _receiver = receiver;
    }

    public abstract TResponse Execute(TRequest request);
}

class CommandProcessor
{
    IList<ICommand<IRequest, IResponse>> _supportedCommands = new List<ICommand<IRequest, IResponse>>();

    public CommandProcessor()
    {
    }

    void AddSupportedCommand(ICommand<IRequest, IResponse> item)
    {
        _supportedCommands.Add(item);
    }

    void SetupSupportedCommands()
    {
        AddSupportedCommand(new SumCommand(new SumReceiver()));
    }

}

这里有更多信息out modifier msdn