-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


CQRS

, 18 2017 . 09:55 +
CQRS, , .

, , . enterprise : , -, , .

, . , , .


image

-. , , 3 : , - .

Repository, .
- , - ( Services/BusinessRules/Managers/Helpers). UI -, Repository .

, , -. , , ,

- , , . . . , , .

. , . CQRS.

Command and Query Responsibility Segregation (CQRS)


CQRS , , , , . . , (tiers), .

Command-query separation (CQS).

CQS , :
  • Queries: , . , Query .
  • Commands: , .

User IsEmailValid:
  1. public class User
  2. {
  3. public string Email { get; private set; }
  4. public bool IsEmailValid(string email)
  5. {
  6. bool isMatch = Regex.IsMatch("emailpattern", email);
  7. if (isMatch)
  8. {
  9. Email = email; //Command
  10. }
  11. return isMatch; //Query
  12. }
  13. }

( Query), email. , True, False. , , email ( Command) Email.

, , Query, . , . Query , .

CQS Command Query, :
  1. public class User
  2. {
  3. public string Email { get; private set; }
  4. public bool IsEmailValid(string email) //Query
  5. {
  6. return Regex.IsMatch("emailpattern", email);
  7. }
  8. public void ChangeEmail(string email) //Command
  9. {
  10. if (IsEmailValid(email) == false)
  11. throw new ArgumentOutOfRangeException(email);
  12. Email = email;
  13. }
  14. }

IsEmailValid, email . ChangeEmail .

CQS Query . Query , Query , .

CQS , CQRS . Command, Query. , , , , .

, UI, - :

image

CQRS , Command Query, . , , :

image

, CQRS, , . , . , (tiers) , .

, , . , . (domain layer) , - . , SQL-.

CQRS?


1.
CQRS , . :
  • ;
  • ;
  • .

3 :
  • , ;
  • ;
  • , , .

, , . , . :
  1. public interface ICommand
  2. {
  3. }

, , , . , (UI), , .

.
  1. public interface ICommandHandler<in TCommand> where TCommand : ICommand
  2. {
  3. void Execute(TCommand command);
  4. }

1 , , .
(ICommand). .
  1. public interface ICommandDispatcher
  2. {
  3. void Execute<TCommand>(TCommand command) where TCommand : ICommand;
  4. }

, . , , , . .

. , -, , . , , .
  1. public class CreateInventoryItem : ICommand
  2. {
  3. public Guid InventoryItemid { get; }
  4. public string Name { get; }
  5. public CreateInventoryItem(Guid inventoryItemld, string name)
  6. {
  7. InventoryItemId = inventoryItemId;
  8. Name = name;
  9. }
  10. }

, ICommand, .

, , : , ICommandHandler. , .
  1. public class InventoryCommandHandler : ICommandHandler<CreateInventoryItem>
  2. {
  3. private readonly IRepository<InventoryItem> _repository;
  4. public InventoryCommandHandlers(IRepository<InventoryItem> repository)
  5. {
  6. _repository = repository;
  7. }
  8. public void Execute(CreateInventoryItem message)
  9. {
  10. var item = new InventoryItem(message.InventoryItemld, message.Name);
  11. _repository.Save(item);
  12. }
  13. //...
  14. }

, , , . ICommandHandler . , Execute .

, , , ICommandDispatcher. , .
  1. public class CommandDispatcher : ICommandDispatcher
  2. {
  3. private readonly IDependencyResolver _resolver;
  4. public CommandDispatcher(IDependencyResolver resolver)
  5. {
  6. _resolver = resolver;
  7. }
  8. public void Execute<TCommand>(TCommand command) where TCommand : ICommand
  9. {
  10. if (command == null) throw new ArgumentNullException("command");
  11. var handler = _resolver.Resolve<ICommandHandler<TCommand>>();
  12. if (handler == null) throw new CommandHandlerNotFoundException(typeof(TCommand));
  13. handler.Execute(command);
  14. }
  15. }

DI-, . , . Execute.

2.
. , , . - - , .

:
  • ;
  • (, ..);
  • .

3 :
  • , ;
  • ;
  • , , .


, . , .
  1. public interface IQuery<TResult>
  2. {
  3. }

. , , string int[].

, .
  1. public interface IQueryHandler<in TQuery, out TResult> where TQuery : IQuery<TResult>
  2. {
  3. TResult Execute(TQuery query);
  4. }

.
  1. public interface IQueryDispatcher
  2. {
  3. TResult Execute<TQuery, TResult>(TQuery query) where TQuery : IQuery<TResult>;
  4. }

. , , . , .
  1. public class FindUsersBySearchTextQuery : IQuery<User[]>
  2. {
  3. public string SearchText { get; }
  4. public bool InactiveUsers { get; }
  5. public FindUsersBySearchTextQuery(string searchText, bool inactiveUsers)
  6. {
  7. SearchText = searchText;
  8. InactiveUsers = inactiveUsers;
  9. }
  10. }

, IQueryHandler .
  1. public class UserQueryHandler : IQueryHandler<FindUsersBySearchTextQuery, User[]>
  2. {
  3. private readonly IRepository<User> _repository;
  4. public FindUsersBySearchTextQueryHandler(IRepository<User> repository)
  5. {
  6. _repository = repository;
  7. }
  8. public User[] Execute(FindUsersBySearchTextQuery query)
  9. {
  10. var users = _repository.GetAll();
  11. return users.Where(user => user.Name.Contains(query.SearchText)).ToArray();
  12. }
  13. }

.
  1. public class QueryDispatcher : IQueryDispatcher
  2. {
  3. private readonly IDependencyResolver _resolver;
  4. public QueryDispatcher(IDependencyResolver resolver)
  5. {
  6. _resolver = resolver;
  7. }
  8. public TResult Execute<TQuery, TResult>(TQuery query) where TQuery : IQuery<TResult>
  9. {
  10. if (query == null) throw new ArgumentNullException("query");
  11. var handler = _resolver.Resolve<IQueryHandler<TQuery, TResult>>();
  12. if (handler == null) throw new QueryHandlerNotFoundException(typeof(TQuery));
  13. return handler.Execute(query);
  14. }
  15. }


, . :
  1. public class UserController : Controller
  2. {
  3. private IQueryDispatcher _queryDispatcher;
  4. public UserController(IQueryDispatcher queryDispatcher)
  5. {
  6. _queryDispatcher = queryDispatcher;
  7. }
  8. public ActionResult SearchUsers(string searchString)
  9. {
  10. var query = new FindUsersBySearchTextQuery(searchString);
  11. User[] users =_queryDispatcher.Execute(query);
  12. return View(users);
  13. }
  14. }

, , Execute.

.


. DI- - . :
using SimpleInjector;

var container = new Container();

container.Register(typeof(ICommandHandler<>), AppDomain.CurrentDomain.GetAssemblies());

container.Register(typeof(IQueryHandler<,>), AppDomain.CurrentDomain.GetAssemblies());

SimpleInjector. Register, , , , . , , .

Command/Query , ..?
, . , , DI- ( SimpleInjector ).

image

CQRS
  • ;
  • (SRP);
  • ;
  • ;
  • .

CQRS
  • CQRS ;
  • CQRS ;
  • Command Query , . , ;
  • CQS CQRS. . Query, -1. ;
  • CRUD-.


  • /;
  • CRUD-.


- , . , CQRS, - . . , , . . , .

CQRS . . CQRS .


CQRS
CQRS
DDD, CQRS Event Sourcing
CQRS Documents by Greg Young
Simple CQRS example
DDDD, CQRS and Other Enterprise Development Buzz-words
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/329970/

:  

: [1] []
 

:
: 

: ( )

:

  URL