CQRS |
- public class User
- {
- public string Email { get; private set; }
- public bool IsEmailValid(string email)
- {
- bool isMatch = Regex.IsMatch("emailpattern", email);
- if (isMatch)
- {
- Email = email; //Command
- }
- return isMatch; //Query
- }
- }
- public class User
- {
- public string Email { get; private set; }
- public bool IsEmailValid(string email) //Query
- {
- return Regex.IsMatch("emailpattern", email);
- }
- public void ChangeEmail(string email) //Command
- {
- if (IsEmailValid(email) == false)
- throw new ArgumentOutOfRangeException(email);
- Email = email;
- }
- }
- public interface ICommand
- {
- }
- public interface ICommandHandler<in TCommand> where TCommand : ICommand
- {
- void Execute(TCommand command);
- }
- public interface ICommandDispatcher
- {
- void Execute<TCommand>(TCommand command) where TCommand : ICommand;
- }
- public class CreateInventoryItem : ICommand
- {
- public Guid InventoryItemid { get; }
- public string Name { get; }
- public CreateInventoryItem(Guid inventoryItemld, string name)
- {
- InventoryItemId = inventoryItemId;
- Name = name;
- }
- }
- public class InventoryCommandHandler : ICommandHandler<CreateInventoryItem>
- {
- private readonly IRepository<InventoryItem> _repository;
- public InventoryCommandHandlers(IRepository<InventoryItem> repository)
- {
- _repository = repository;
- }
- public void Execute(CreateInventoryItem message)
- {
- var item = new InventoryItem(message.InventoryItemld, message.Name);
- _repository.Save(item);
- }
- //...
- }
- public class CommandDispatcher : ICommandDispatcher
- {
- private readonly IDependencyResolver _resolver;
- public CommandDispatcher(IDependencyResolver resolver)
- {
- _resolver = resolver;
- }
- public void Execute<TCommand>(TCommand command) where TCommand : ICommand
- {
- if (command == null) throw new ArgumentNullException("command");
- var handler = _resolver.Resolve<ICommandHandler<TCommand>>();
- if (handler == null) throw new CommandHandlerNotFoundException(typeof(TCommand));
- handler.Execute(command);
- }
- }
- public interface IQuery<TResult>
- {
- }
- public interface IQueryHandler<in TQuery, out TResult> where TQuery : IQuery<TResult>
- {
- TResult Execute(TQuery query);
- }
- public interface IQueryDispatcher
- {
- TResult Execute<TQuery, TResult>(TQuery query) where TQuery : IQuery<TResult>;
- }
- public class FindUsersBySearchTextQuery : IQuery<User[]>
- {
- public string SearchText { get; }
- public bool InactiveUsers { get; }
- public FindUsersBySearchTextQuery(string searchText, bool inactiveUsers)
- {
- SearchText = searchText;
- InactiveUsers = inactiveUsers;
- }
- }
- public class UserQueryHandler : IQueryHandler<FindUsersBySearchTextQuery, User[]>
- {
- private readonly IRepository<User> _repository;
- public FindUsersBySearchTextQueryHandler(IRepository<User> repository)
- {
- _repository = repository;
- }
- public User[] Execute(FindUsersBySearchTextQuery query)
- {
- var users = _repository.GetAll();
- return users.Where(user => user.Name.Contains(query.SearchText)).ToArray();
- }
- }
- public class QueryDispatcher : IQueryDispatcher
- {
- private readonly IDependencyResolver _resolver;
- public QueryDispatcher(IDependencyResolver resolver)
- {
- _resolver = resolver;
- }
- public TResult Execute<TQuery, TResult>(TQuery query) where TQuery : IQuery<TResult>
- {
- if (query == null) throw new ArgumentNullException("query");
- var handler = _resolver.Resolve<IQueryHandler<TQuery, TResult>>();
- if (handler == null) throw new QueryHandlerNotFoundException(typeof(TQuery));
- return handler.Execute(query);
- }
- }
- public class UserController : Controller
- {
- private IQueryDispatcher _queryDispatcher;
- public UserController(IQueryDispatcher queryDispatcher)
- {
- _queryDispatcher = queryDispatcher;
- }
- public ActionResult SearchUsers(string searchString)
- {
- var query = new FindUsersBySearchTextQuery(searchString);
- User[] users =_queryDispatcher.Execute(query);
- return View(users);
- }
- }
using SimpleInjector;
var container = new Container();
container.Register(typeof(ICommandHandler<>), AppDomain.CurrentDomain.GetAssemblies());
container.Register(typeof(IQueryHandler<,>), AppDomain.CurrentDomain.GetAssemblies());