我们将从一个独立的故事开始,只是让你理解为什么:我想要处理任何针对同一个接口更改数据的操作:ICommand存在一些名为ICommandHandlers的东西,可以处理我想要的任何命令.所以,如果我想要一个CreatePersonCommand,我需要一个CreatePersonCommandHandler.
所以这是控制台应用程序的主体,它演示了这一点:(需要简单的注入器)
// The e.g. CreatePersonCommand, with TResult being Person, as an example.
public interface ICommand<TResult>
{
}
//This handles the command, so CreatePersonCommandHandler
public interface ICommandHandler<in TCommand, out TResult>
where TCommand : ICommand<TResult>
{
TResult Handle(TCommand command);
}
// Imagine a generic CRUD set of operations here where we pass
// in an instance of what we need made
public class CreateBaseCommand<TModel> : ICommand<TModel>
{
public TModel ItemToCreate { get; set; }
}
public class DeleteBaseCommand<TModel> : …Run Code Online (Sandbox Code Playgroud) c# generics dependency-injection inversion-of-control simple-injector