I am trying to implement a command template with strongly typed input and output parameters for the command itself.
First of all, I created two interfaces that mark input and output for the command:
interface IRequest { } interface IResponse { }
Then I created the base classes and interfaces. This is an abstract receiver.
interface IReceiver<TRequest, TResponse> where TRequest : IRequest where TResponse : IResponse { TResponse Action( TRequest request ); }
and this is an abstract team
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 ); }
Now I'm trying to use these objects, and so I created the necessary concrete 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 }; } }
Finally, I created a CommandProcessor class that should be able to handle several commands in general
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() {
However, I get a compilation error:
Argument 1: cannot be converted from 'SumCommand' to 'AbstractCommand'
Any help or suggestion?