{"id":318771,"date":"2021-02-28T21:00:13","date_gmt":"2021-02-28T21:00:13","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=318771"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=318771","title":{"rendered":"CQS (CQRS) \u0441\u043e \u0441\u0432\u043e\u0438\u043c \u0431\u043b\u044d\u043a\u0434\u0436\u0435\u043a\u043e\u043c"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">Command-query separation (CQS) \u2014 \u044d\u0442\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043d\u0430 read \u0438 write.<\/p>\n<p>  Command Query Responsibility Segregation (CQRS) \u2014 \u044d\u0442\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430 read \u0438 write. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u0443 \u043f\u0438\u0448\u0435\u043c, \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043c\u043e\u0436\u0435\u043c \u0447\u0438\u0442\u0430\u0442\u044c. \u041c \u2014 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.<\/p>\n<p>  \u042d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043a\u0430\u043a \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0430, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043e\u0434\u043d\u043e. \u041d\u043e \u043a\u0430\u043a \u0432\u0441\u0435\u0433\u0434\u0430, \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u0431\u043e\u043b\u0435\u0435-\u043c\u0435\u043d\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u044d\u0442\u0430 \u0448\u0442\u0443\u043a\u0430 \u0434\u0430\u0435\u0442 \u0441\u0431\u043e\u0439.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0420\u0430\u0437\u043c\u044b\u0448\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0432\u0435\u044f\u043d\u044b \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435\u0439 <a href=\"https:\/\/habr.com\/ru\/post\/543828\">\u041f\u0430\u0442\u0442\u0435\u0440\u043d CQRS: \u0442\u0435\u043e\u0440\u0438\u044f \u0438 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 ASP.Net Core 5<\/a> \u0438 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b \u0434\u043b\u044f \u0430\u043d\u0435\u043c\u0438\u0447\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438. \u0414\u043b\u044f DDD \u0432\u0441\u0435 \u043f\u043e-\u0434\u0440\u0443\u0433\u043e\u043c\u0443.<\/p>\n<h2>\u0418\u0441\u0442\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043f\u0440\u0430\u0432\u043a\u0430<\/h2>\n<p>  \u041d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0436\u0430\u043b\u0443\u0439 \u0441\u0442\u043e\u0438\u0442 \u0441 \u0438\u0441\u0442\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u043f\u0440\u0430\u0432\u043a\u0438. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0431\u044b\u043b\u043e \u043a\u0430\u043a-\u0442\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">public interface IEntityService {     EntityModel[] GetAll();     EntityModel Get(int id);     int Add(EntityModel model);     void Update(EntityModel model);     void Delete(int id); }  public interface IEntityRepository {     Entity[] GetAll();     Entity Get(int id);     int Add(Entity entity);     void Update(Entity entity);     void Delete(int id); } <\/code><\/pre>\n<p>  \u0421 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435\u043c CQS \u0441\u0442\u0430\u043b\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">public class GetEntitiesQuery {      public EntityModel[] Execute() { ... } }  public class GetEntityQuery {      public EntityModel Execute(int id) { ... } }  public class AddEntityCommand {      public int Execute(EntityModel model) { ... } }  public class UpdateEntityCommand {      public void Execute(EntityModel model) { ... } }  public class DeleteEntityCommand {      public void Execute(int id) { ... } } <\/code><\/pre>\n<p>  <\/p>\n<h2>\u042d\u0432\u043e\u043b\u044e\u0446\u0438\u044f<\/h2>\n<p>  \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u043c, \u0434\u0432\u0430 \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0445 god-\u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u043d\u0430 \u043c\u043d\u043e\u0433\u043e \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0445 \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0434\u0435\u043b\u0430\u0435\u0442 \u043e\u0434\u043d\u0443 \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0432\u0435\u0449\u044c \u2014 \u043b\u0438\u0431\u043e \u0447\u0438\u0442\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435, \u043b\u0438\u0431\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442. \u042d\u0442\u043e \u0443 \u043d\u0430\u0441 CQS. \u0415\u0441\u043b\u0438 \u0435\u0449\u0435 \u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u0434\u0432\u0430 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 (\u043e\u0434\u043d\u043e \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0438 \u043e\u0434\u043d\u043e \u0434\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438) \u2014 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0443\u0436\u0435 CQRS. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0447\u0442\u043e \u0438\u0437 \u0441\u0435\u0431\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 GetEntityQuery \u0438 UpdateEntityCommand (\u0437\u0434\u0435\u0441\u044c \u0438 \u0434\u0430\u043b\u0435\u0435 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0439 \u043f\u0441\u0435\u0432\u0434\u043e\u043a\u043e\u0434):<\/p>\n<pre><code class=\"cs\">public class GetEntityQuery {     public EntityModel Execute(int id)     {         var sql = &quot;SELECT * FROM Table WHERE Id = :id&quot;;         using (var connection = new SqlConnection(...connStr...))         {              var command = connection.CreateCommand(sql, id);              return command.Read();         }     } }  public class UpdateEntityCommand {     public void Execute(EntityModel model)     {         var sql = &quot;UPDATE Table SET ... WHERE Id = :id&quot;;         using (var connection = new SqlConnection(...connStr...))         {              var command = connection.CreateCommand(sql, model);              return command.Execute();         }     } } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043a \u043d\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 ORM. \u0418 \u0432\u043e\u0442 \u0442\u0443\u0442 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b. \u0427\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0442\u0435\u043c \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442\u0441\u044f. \u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u044d\u0442\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">public class UpdateEntityCommand {     public void Execute(EntityModel model)     {         var entity = db.Entities.First(e =&gt; e.Id == model.Id); \/\/ &lt;-- \u043e\u043f\u0430, \u0430 \u0447\u0442\u043e \u044d\u0442\u043e? query?         entity.Field1 = model.Field1;          db.SaveChanges();     } } <\/code><\/pre>\n<p>  \u0414\u0430, \u0435\u0441\u043b\u0438 ORM \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0441\u0440\u0430\u0437\u0443, \u0442\u043e \u0432\u0441\u0435 \u0431\u0443\u0434\u0435\u0442 \u0445\u043e\u0440\u043e\u0448\u043e:<\/p>\n<pre><code class=\"cs\">public class UpdateEntityCommand {     public void Execute(EntityModel model)     {         var entity = new Entity { Id = model.Id, Field1 = model.Field1 };         db.Attach(entity);          db.SaveChanges();     } } <\/code><\/pre>\n<p>  \u0422\u0430\u043a \u0430 \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u0434\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u0438\u0437 \u0431\u0430\u0437\u044b? \u041a\u0443\u0434\u0430 \u0434\u0435\u0432\u0430\u0442\u044c query \u0438\u0437 command? \u041d\u0430 \u0443\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">public class GetEntityQuery {     public Entity Execute(int id)     {         return db.Entities.First(e =&gt; e.Id == model.Id);     } }  public class UpdateEntityCommand {     public void Execute(Entity entity, EntityModel model)     {         entity.Field1 = model.Field1;          db.SaveChanges();     } } <\/code><\/pre>\n<p>  \u0425\u043e\u0442\u044f \u044f \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b \u0435\u0449\u0435 \u0442\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442:  <\/p>\n<pre><code class=\"cs\">public class UpdateEntityCommand {     public void Execute(EntityModel model)     {         var entity = _entityService.Get(model.Id); \/\/ )))          entity.Field1 = model.Field1;          db.SaveChanges();     } }  public class EntityService {     public Entity Get(int id)     {         return db.Entities.First(e =&gt; e.Id == model.Id);     } } <\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0435\u0441\u0442\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0435. \u042d\u0442\u0430 \u0441\u0442\u0440\u043e\u0447\u043a\u0430 \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0435\u0442 \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0431\u044b\u0442\u044c query.<\/p>\n<p>  \u041b\u0430\u0434\u043d\u043e, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438\u0441\u044c \u043d\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u0441 GetEntityQuery \u0438 UpdateEntityCommand. \u0422\u0430\u043c \u0445\u043e\u0442\u044f \u0431\u044b query \u043d\u0435 \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0431\u044b\u0442\u044c \u0447\u0435\u043c-\u0442\u043e \u0434\u0440\u0443\u0433\u0438\u043c. \u041d\u043e \u043a\u0443\u0434\u0430 \u044d\u0442\u043e \u0432\u0441\u0435 \u0441\u043b\u043e\u0436\u0438\u0442\u044c \u0438 \u043e\u0442\u043a\u0443\u0434\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c? \u041f\u043e\u043a\u0430 \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u043e\u0434\u043d\u043e \u043c\u0435\u0441\u0442\u043e \u2014 \u044d\u0442\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440, \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">public class EntityController {     [HttpPost]     public EntityModel Update(EntityModel model)     {         var entity = new GetEntityQuery().Execute(model.Id);                  new UpdateEntityCommand().Execute(entity, model);          return model;     } } <\/code><\/pre>\n<p>  \u0414\u0430 \u0438 \u0447\u0435\u0440\u0435\u0437 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u043e\u0441\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cs\">public class EntityController {     [HttpPost]     public EntityModel Update(EntityModel model)     {         var entity = new GetEntityQuery().Execute(model.Id);                  new UpdateEntityCommand().Execute(entity, model);                  _notifyService.Notify(NotifyType.UpdateEntity, entity); \/\/ &lt;-- \u0410 \u044d\u0442\u043e query \u0438\u043b\u0438 command?          return model;     } } <\/code><\/pre>\n<p>  \u0412 \u0438\u0442\u043e\u0433\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0443 \u043d\u0430\u0441 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0442\u043e\u043b\u0441\u0442\u0435\u0442\u044c.<\/p>\n<h2>\u041b\u0438\u0440\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u0442\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435 IDEF0 \u0438 BPMN<\/h2>\n<p>  \u041c\u0430\u043b\u043e \u0442\u043e\u0433\u043e, \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u0431\u0438\u0437\u043d\u0435\u0441-\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u0441\u043b\u043e\u0436\u043d\u044b\u0435. \u0415\u0441\u043b\u0438 \u0432\u0437\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u043d\u0430 \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u044b IDEF0 \u0438\u043b\u0438 BPMN \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u043b\u043e\u043a\u043e\u0432, \u0437\u0430 \u043a\u0430\u0436\u0434\u044b\u043c \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u043e\u0436\u0435\u0442 \u0441\u043a\u0440\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u043e\u0434 \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0430 \u0438\u0437 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0438\u043b\u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u0430\u044f \u0441\u0435\u0440\u0438\u044f \u0431\u043b\u043e\u043a\u043e\u0432.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/proxy\/ttSfU8xwYEaO3lqW62K03xSgtRNbFDpYED_iLwlE0PJh6XHlDoFFPoVQ6FbJFoUy7qcO9TOOyrhu57LjWPy5lLWMdYLKi-RlhUCZ97kzq8A\" alt=\"image\"\/><\/p>\n<p>  \u0418 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u0434\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438: \u043f\u043e \u0433\u0435\u043e-\u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0430\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u0433\u043e\u0434\u0443 \u0432 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0435. \u0415\u0441\u0442\u044c \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0443\u0441\u043b\u043e\u0432\u043d\u043e-\u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0432 \u0432\u0438\u0434\u0435 \u043a\u044d\u0448\u0430. \u041a\u044d\u0448 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439. \u0425\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0432\u044b\u0431\u043e\u0440\u043a\u0438: \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0434\u0435\u043c \u0432 \u043a\u044d\u0448, \u0435\u0441\u043b\u0438 \u0442\u0430\u043c \u0435\u0441\u0442\u044c \u0442\u043e\u0447\u043a\u0430 \u0432 \u0440\u0430\u0434\u0438\u0443\u0441\u0435 10 \u043a\u043c \u043e\u0442 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0438 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 1 \u0447\u0430\u0441\u0430 \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u043f\u043e\u0433\u043e\u0434\u0443 \u0438\u0437 \u043a\u044d\u0448\u0430. \u0418\u043d\u0430\u0447\u0435 \u0438\u0434\u0435\u043c \u0432\u043e \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0441\u0435\u0440\u0432\u0438\u0441. \u0417\u0434\u0435\u0441\u044c \u0438 query, \u0438 command, \u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a \u0432\u043d\u0435\u0448\u043d\u0435\u043c\u0443 \u0441\u0435\u0440\u0432\u0438\u0441\u0443 \u2014 \u0432\u0441\u0435-\u0432-\u043e\u0434\u043d\u043e\u043c.<\/p>\n<h2>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h2>\n<p>  \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0430\u0432\u043d\u043e \u0432\u0438\u0442\u0430\u043b\u043e \u0432 \u043e\u0431\u043b\u0430\u043a\u0430\u0445, \u043d\u043e \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u043e\u0444\u043e\u0440\u043c\u043b\u044f\u043b\u043e\u0441\u044c \u0432 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c \u0432\u0438\u0434\u0435. \u041f\u043e\u043a\u0430 \u044f \u043e\u0434\u043d\u0430\u0436\u0434\u044b \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b \u043d\u0435\u0447\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0435\u0435 \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435. \u042f \u0432\u0437\u044f\u043b \u0435\u0433\u043e \u0437\u0430 \u043e\u0441\u043d\u043e\u0432\u0443 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0441\u0432\u043e\u0439 \u0431\u043b\u044d\u043a\u0434\u0436\u0435\u043a.<\/p>\n<p>  \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u043c \u0438\u0441\u043a\u043e\u043c\u044b\u0439 CQS \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u043d \u0434\u043b\u044f \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c. \u0422\u0430\u043c \u0441 \u043d\u0438\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043d\u0435\u0442. \u041a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0438\u043b\u0441\u044f \u0443 \u043d\u0430\u0441 \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435 \u2014 \u044d\u0442\u043e \u0431\u0438\u0437\u043d\u0435\u0441-\u043a\u043e\u0434, \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438. \u0418 \u0438\u043c\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0432\u044b\u0434\u0435\u043b\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u0438\u0435 \u2014 \u0431\u0438\u0437\u043d\u0435\u0441-\u0438\u0441\u0442\u043e\u0440\u0438\u044f. \u0418\u043b\u0438 Story.<\/p>\n<p>  \u041e\u0434\u043d\u0430 \u0431\u0438\u0437\u043d\u0435\u0441-\u0438\u0441\u0442\u043e\u0440\u0438\u044f \u2014 \u044d\u0442\u043e \u043e\u0434\u0438\u043d \u0438\u0437 \u0431\u043b\u043e\u043a\u043e\u0432 \u043d\u0430 \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0435 IDEF0. \u041e\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0431\u0438\u0437\u043d\u0435\u0441-\u0438\u0441\u0442\u043e\u0440\u0438\u0438, \u043a\u0430\u043a \u0431\u043b\u043e\u043a IDEF0 \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0431\u043b\u043e\u043a\u0438. \u0418 \u043e\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u0438\u0441\u043a\u043e\u043c\u044b\u043c \u043f\u043e\u043d\u044f\u0442\u0438\u044f\u043c CQS \u2014 \u044d\u0442\u043e \u043a Query \u0438 Command.<\/p>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043a\u043e\u0434 \u0438\u0437 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u043c\u044b \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u043c \u0432 Story:  <\/p>\n<pre><code class=\"cs\">public class EntityController {     [HttpPost]     public EntityModel Update(EntityModel model)     {         return new UpdateEntityStory().Execute(model);     } }  public class UpdateEntityStory {     public EntityModel Execute(EntityModel model)     {         var entity = new GetEntityQuery().Execute(model.Id);                  new UpdateEntityCommand().Execute(entity, model);                  _notifyService.Notify(NotifyType.UpdateEntity, entity);          return model;     } } <\/code><\/pre>\n<p>  \u0418 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043d\u043a\u0438\u043c.<\/p>\n<p>  \u0414\u0430\u043d\u043d\u0430\u044f UpdateEntityStory \u0438\u043d\u043a\u0430\u043f\u0441\u0443\u043b\u0438\u0440\u0443\u0435\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0431\u0438\u0437\u043d\u0435\u0441-\u043f\u0440\u043e\u0446\u0435\u0441\u0441. \u0415\u0435 \u043c\u043e\u0436\u043d\u043e \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 \u0432\u044b\u0437\u043e\u0432\u0430\u0445 API). \u041e\u043d\u0430 \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u0434\u0432\u0435\u0440\u0433\u0430\u0435\u0442\u0441\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u0438 \u043d\u0438\u043a\u043e\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u043a\u043e\u0432\/\u0444\u0435\u0439\u043a-\u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432.<\/p>\n<p>  \u0414\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0443 IDEF0\/BPMN \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0431\u0440\u043e\u0441\u0430\u0442\u044c \u043f\u043e \u0442\u0430\u043a\u0438\u043c Story, \u0447\u0442\u043e \u0434\u0430\u0441\u0442 \u0431\u043e\u043b\u0435\u0435 \u043b\u0435\u0433\u043a\u0438\u0439 \u0432\u0445\u043e\u0434 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442. \u0412\u0441\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0443\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441: \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u0435\u043d\u044f\u0435\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e (\u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0443 IDEF0) \u2014 \u0437\u0430\u0442\u0435\u043c \u0434\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u044b \u2014 \u0430 \u0443\u0436\u0435 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0434\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0431\u0438\u0437\u043d\u0435\u0441-\u043a\u043e\u0434. \u041c\u043e\u0436\u043d\u043e \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442, \u043f\u043e \u044d\u0442\u0438\u043c Story \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u0432 \u0432\u0438\u0434\u0435 IDEF0\/BPMN \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c.<\/p>\n<p>  \u041d\u043e \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0441\u0442\u0440\u043e\u0439\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430:<br \/>  1. Story \u2014 \u0432\u0445\u043e\u0434\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438. \u0418\u043c\u0435\u043d\u043d\u043e \u043d\u0430 \u043d\u0435\u0435 \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440.<br \/>  2. \u041d\u043e \u0432\u043d\u0443\u0442\u0440\u044c Story \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u043f\u0430\u0434\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u0432\u0435\u0449\u0438 \u043a\u0430\u043a HttpContext \u0438 \u0442\u043e\u043c\u0443 \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435. \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u043e\u0433\u0434\u0430 Story \u043d\u0435\u043b\u044c\u0437\u044f \u0431\u0443\u0434\u0435\u0442 \u043b\u0435\u0433\u043a\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 hangfire background job \u0438\u043b\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u2014 \u0442\u0430\u043c \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 HttpContext).<br \/>  3. \u0412\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b Story \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b. Story \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0447\u0442\u043e-\u043b\u0438\u0431\u043e \u0438\u043b\u0438 \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043d\u0438\u0447\u0435\u0433\u043e (\u0445\u043e\u0442\u044f \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u0438 \u0445\u043e\u0440\u043e\u0448\u043e \u0431\u044b \u043e\u043d\u0430 \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b\u0430). <br \/>  4. Story \u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043a\u0430\u043a \u0441 \u0431\u0438\u0437\u043d\u0435\u0441-\u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u043c\u0438, \u0442\u0430\u043a \u0438 \u0441 \u043c\u043e\u0434\u0435\u043b\u044f\u043c\u0438 \u0438 DTO. \u041c\u043e\u0436\u0435\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043c\u0430\u043f\u043f\u0435\u0440\u044b \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440\u044b.<br \/>  5. Story \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u0435 Story.<br \/>  6. Story \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b. \u0425\u043e\u0442\u044f \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0432\u044b\u0437\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u0436\u0435 \u043e\u0444\u043e\u0440\u043c\u0438\u0442\u044c \u043a\u0430\u043a Story. \u041e\u0431 \u044d\u0442\u043e\u043c \u043d\u0438\u0436\u0435 \u0441 \u043d\u0430\u0448\u0438\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u043c \u043f\u043e\u0433\u043e\u0434\u044b.<br \/>  7. Story \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0443 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445. \u042d\u0442\u043e \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 Query \u0438 Command. \u0415\u0441\u043b\u0438 \u043d\u0430\u0440\u0443\u0448\u0438\u0442\u044c \u044d\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432\u044b\u0442\u0435\u043a\u0443\u0442 \u043d\u0430\u0440\u0443\u0436\u0443 \u0438 \u0440\u0430\u0437\u043c\u0430\u0436\u0443\u0442\u0441\u044f \u043f\u043e \u0432\u0441\u0435\u043c\u0443 \u043f\u0440\u043e\u0435\u043a\u0442\u0443.<br \/>  8. \u041d\u0430 Story \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0432\u0435\u0448\u0438\u0432\u0430\u0442\u044c \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u044b. \u041e\u0431 \u044d\u0442\u043e\u043c \u0442\u043e\u0436\u0435 \u043d\u0438\u0436\u0435.<br \/>  9. Story \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c Query \u0438 Command.<br \/>  10. \u0420\u0430\u0437\u043d\u044b\u0435 Story \u043c\u043e\u0433\u0443\u0442 \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0438 \u0438 \u0442\u0435 \u0436\u0435 Query \u0438 Command.<br \/>  11. Query \u0438 Command \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u0435 Story, Query \u0438 Command.<br \/>  12. \u0422\u043e\u043b\u044c\u043a\u043e Query \u0438 Command \u043c\u043e\u0433\u0443\u0442 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0443 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<br \/>  13. \u0412 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c \u0431\u0435\u0437 Story \u0438 \u0438\u0437 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u043e\u0432 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443 Query \u0438\u043b\u0438 Command.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0442\u043e\u0442 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u043c \u043f\u043e\u0433\u043e\u0434\u044b:  <\/p>\n<pre><code class=\"cs\">public class GetWeatherStory {     public WeatherModel Execute(double lat, double lon)     {         var weather = new GetWeatherQuery().Execute(lat, lon, DateTime.NowUtc);          if (weather == null)         {              weather = _weatherService.GetWeather(lat, lon);              new AddWeatherCommand().Execute(weather);         }          return weather;     } }  public class GetWeatherQuery {     public WeatherModel Execute(double lat, double lon, DateTime currentDateTime)     {         \/\/ \u041d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 SQL \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u0438\u0441\u043a\u0430 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c:         \/\/ * \u0432 \u0440\u0430\u0434\u0438\u0443\u0441\u0435 10 \u043a\u043c \u043e\u0442 \u0442\u043e\u0447\u043a\u0438 lat\/lon         \/\/ * \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 1 \u0447\u0430\u0441\u0430 \u043e\u0442 currentDateTime         \/\/ \u0421 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 PostGis \u0438\u043b\u0438 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0445          return result;     } }  public class AddWeatherCommand {     public void Execute(WeatherModel model)     {         var entity = new Weather { ...\u043f\u043e\u043b\u044f \u0438\u0437 model... };         db.Weathers.Add(entity);         db.SaveChanges();     } }  public class WeatherService {     public WeatherModel GetWeather(double lat, double lon)     {         var client = new Client();         var result = client.GetWeather(lat, lon);         return result.ToWeatherModel(); \/\/ \u043c\u0430\u043f\u043f\u0435\u0440 \u0438\u0437 dto \u0432 \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c     } } <\/code><\/pre>\n<h2>\u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u044b<\/h2>\n<p>  \u0418 \u0432 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043e \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430\u0445. \u0427\u0442\u043e\u0431\u044b Story \u0441\u0442\u0430\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u0433\u0438\u0431\u043a\u0438\u043c\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e c\u043b\u043e\u0436\u0438\u0442\u044c \u0438\u0445 \u0432 DI \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \/ mediator. \u0418 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u0435\u043a\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0432\u044b\u0437\u043e\u0432.<\/p>\n<p>  \u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0438:<\/p>\n<p>  1. \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c Story \u0432\u043d\u0443\u0442\u0440\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 scoped \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445:  <\/p>\n<pre><code class=\"cs\">public class EntityController {     [HttpPost]     public EntityModel Update(EntityModel model)     {         return _mediator.Resolve&lt;UpdateEntityStory&gt;().WithTransaction().Execute(model);     } }  \/\/ \u0438\u043b\u0438  [Transaction] public class UpdateEntityStory {     ... } <\/code><\/pre>\n<p>  2. \u041a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432  <\/p>\n<pre><code class=\"cs\">public class EntityController {     [HttpPost]     public ResultModel GetAccessRights()     {         return _mediator             .Resolve&lt;GetAccessRightsStory&gt;()             .WithCache(&quot;key&quot;, 60)             .Execute();     } }  \/\/ \u0438\u043b\u0438  [Cache(&quot;key&quot;, 60)] public class GetAccessRightsStory {     ... } <\/code><\/pre>\n<p>  3. \u041f\u043e\u043b\u0438\u0442\u0438\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u043e\u0432  <\/p>\n<pre><code class=\"cs\">public class GetWeatherStory {     public WeatherModel Execute(double lat, double lon)     {         var weather = new GetWeatherQuery().Execute(lat, lon, DateTime.NowUtc);          if (weather == null)         {              weather = _mediator                  .Resolve&lt;GetWeatherFromExternalServiceStory&gt;()                  .WithRetryAttempt(5)                  .Execute(lat, lon);               _mediator.Resolve&lt;AddWeatherCommand&gt;().Execute(weather);         }          return weather;     } }  \/\/ \u0438\u043b\u0438  [RetryAttempt(5)] public class GetWeatherFromExternalServiceStory {     ... } <\/code><\/pre>\n<p>  4. \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u0430\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430  <\/p>\n<pre><code class=\"cs\">public class GetWeatherStory {     public WeatherModel Execute(double lat, double lon)     {         var weather = new GetWeatherQuery().Execute(lat, lon, DateTime.NowUtc);          if (weather == null)         {              weather = _mediator                  .Resolve&lt;GetWeatherFromExternalServiceStory&gt;()                  .WithRetryAttempt(5).                  .Execute(lat, lon);               _mediator.Resolve&lt;AddWeatherStory&gt;()                  .WithDistributedLock(LockType.RedLock, &quot;key&quot;, 60)                  .Execute(weather);         }          return weather;     } }  \/\/ \u0438\u043b\u0438  [DistributedLock(LockType.RedLock, &quot;key&quot;, 60)] public class AddWeatherStory {     ... } <\/code><\/pre>\n<p>  \u0418 \u0442\u043e\u043c\u0443 \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435.<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/544034\/\"> https:\/\/habr.com\/ru\/post\/544034\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">Command-query separation (CQS) \u2014 \u044d\u0442\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043d\u0430 read \u0438 write.<\/p>\n<p>  Command Query Responsibility Segregation (CQRS) \u2014 \u044d\u0442\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430 read \u0438 write. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u0443 \u043f\u0438\u0448\u0435\u043c, \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043c\u043e\u0436\u0435\u043c \u0447\u0438\u0442\u0430\u0442\u044c. \u041c \u2014 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.<\/p>\n<p>  \u042d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043a\u0430\u043a \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0430, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043e\u0434\u043d\u043e. \u041d\u043e \u043a\u0430\u043a \u0432\u0441\u0435\u0433\u0434\u0430, \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u0431\u043e\u043b\u0435\u0435-\u043c\u0435\u043d\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u044d\u0442\u0430 \u0448\u0442\u0443\u043a\u0430 \u0434\u0430\u0435\u0442 \u0441\u0431\u043e\u0439.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-318771","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/318771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=318771"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/318771\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}