{"id":308210,"date":"2020-08-09T21:00:19","date_gmt":"2020-08-09T21:00:19","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=308210"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=308210","title":{"rendered":"\u0422\u0440\u0438 \u043c\u0443\u0448\u043a\u0435\u0442\u0430\u0440\u0430 \u2014 Event Sourcing, Event Storming \u0438 Event Store \u2014 \u0432\u0441\u0442\u0443\u043f\u0430\u044e\u0442 \u0432 \u0431\u043e\u0439: \u0427\u0430\u0441\u0442\u044c 1 \u2014 \u043f\u0440\u043e\u0431\u0443\u0435\u043c Event Store \u0414\u0411"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/514436\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/hx\/ok\/cz\/hxokczmagjowkra41xnvppeasa8.png\"><br \/>  \u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u0420\u0435\u0448\u0438\u043b \u044f \u0437\u043d\u0430\u0447\u0438\u0442 \u043d\u0430 \u0432\u0440\u0435\u043c\u044f \u043e\u0442\u043e\u0439\u0442\u0438 \u043e\u0442 Scala, Idris \u0438 \u043f\u0440\u043e\u0447\u0435\u0433\u043e \u0424\u041f \u0438 \u0447\u0443\u0442\u044c \u0447\u0443\u0442\u044c \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e Event Store \u2014 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0432 \u043f\u043e\u0442\u043e\u043a\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u041a\u0430\u043a \u0432 \u0441\u0442\u0430\u0440\u043e\u0439 \u0434\u043e\u0431\u0440\u043e\u0439 \u043a\u043d\u0438\u0433\u0435 \u0443 \u043d\u0430\u0441 \u0442\u043e\u0436\u0435 \u043c\u0443\u0448\u043a\u0435\u0442\u0435\u0440\u043e\u0432 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 4 \u0438 \u0447\u0435\u0442\u0432\u0435\u0440\u0442\u044b\u0439 \u044d\u0442\u043e DDD. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Event Storming \u0432\u044b\u0434\u0435\u043b\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u044b, \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0441 \u043d\u0438\u043c\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435. \u041f\u043e\u0442\u043e\u043c \u0441\u0434\u0435\u043b\u0430\u044e \u043d\u0430 \u0438\u0445 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u0435\u0433\u043e \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435. \u0411\u0443\u0434\u0443 \u044f \u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043e\u0431\u044b\u0447\u043d\u044b\u0439 TodoList. \u0417\u0430 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u0434\u043e\u0431\u0440\u043e \u043f\u043e\u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434 \u043a\u0430\u0442.<br \/>  <a name=\"habracut\"><\/a><\/p>\n<h2>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/h2>\n<p>  <\/p>\n<ul>\n<li>\u0418\u0437\u0443\u0447\u0430\u044e Scala: \u0427\u0430\u0441\u0442\u044c 2 \u2014 Todo \u043b\u0438\u0441\u0442 \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a<\/li>\n<\/ul>\n<p>  <\/p>\n<h2>\u0421\u0441\u044b\u043b\u043a\u0438<\/h2>\n<p>  <a href=\"https:\/\/gitlab.com\/VictorWinbringer\/eventstoretodoexample\" rel=\"nofollow\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438<\/a><br \/>  <a href=\"https:\/\/gitlab.com\/VictorWinbringer\/eventstoretodoexample\/container_registry\/\" rel=\"nofollow\">\u041e\u0431\u0440\u0430\u0437\u044b docker image<\/a><br \/>  <a href=\"https:\/\/eventstore.com\/\" rel=\"nofollow\">Event Store<\/a><br \/>  <a href=\"https:\/\/microservices.io\/patterns\/data\/event-sourcing.html\" rel=\"nofollow\">Event Soucing<\/a><br \/>  <a href=\"https:\/\/en.wikipedia.org\/wiki\/Event_storming\" rel=\"nofollow\">Event Storming<\/a><\/p>\n<p>  \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0433\u043e\u0432\u043e\u0440\u044f Event Store \u044d\u0442\u043e \u0411\u0414 \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0430 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u0422\u0430\u043a \u0436\u0435 \u043e\u043d\u0430 \u0443\u043c\u0435\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438 \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0447\u0442\u043e\u0431\u044b \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043a\u0430\u043a-\u0442\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c. \u0422\u0430\u043a \u0436\u0435 \u0442\u0430\u043c \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0446\u0438\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u0430\u043a \u0436\u0435 \u0440\u0435\u0430\u0433\u0438\u0440\u0443\u044e\u0442 \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0438 \u043d\u0430 \u0438\u0445 \u043e\u0441\u043d\u043e\u0432\u0435 \u0430\u043a\u043a\u0443\u043c\u0443\u043b\u0438\u0440\u0443\u044e \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0438 TodoCreated \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0441\u0447\u0435\u0442\u0447\u0438\u043a Count \u0432 \u043f\u0440\u043e\u0435\u043a\u0446\u0438\u0438. \u041f\u043e\u043a\u0430 \u0447\u0442\u043e \u0432 \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Event Store \u043a\u0430\u043a Read \u0438 Write Db. \u0414\u0430\u043b\u044c\u0448\u0435 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 \u044f \u0441\u043e\u0437\u0434\u0430\u043c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0411\u0414 \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0443\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 \u0432 \u0411\u0414 \u0434\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u0442\u043e\u0431\u0438\u0448\u044c Event Store. \u0422\u0430\u043a \u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u00ab\u041f\u0443\u0442\u0435\u0448\u0435\u0441\u0442\u0432\u0438\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438\u00bb \u043e\u0442\u043a\u0430\u0442\u044b\u0432\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u043a \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u043d\u0430 \u0438\u043c\u0435\u043b\u0430 \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u043c.<br \/>  \u0418 \u0442\u0430\u043a \u043d\u0430\u0447\u043d\u0435\u043c Event Stroming. \u041e\u0431\u044b\u0447\u043d\u043e \u0434\u043b\u044f \u0435\u0433\u043e \u043f\u0440\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442 \u0432\u0441\u0435 \u0437\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043b\u044e\u0434\u0435\u0439 \u0438 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u043e\u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043a\u0430\u043a\u0438\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0432 \u0442\u043e\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u041f\u041e \u0431\u0443\u0434\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u041f\u041e \u0437\u0430\u0432\u043e\u0434\u0430 \u2014 \u0418\u0437\u0434\u0435\u043b\u0438\u0435\u0418\u0437\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u043e. \u0414\u043b\u044f \u0438\u0433\u0440\u044b \u2014 \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0423\u0440\u043e\u043d. \u0414\u043b\u044f \u0424\u0438\u043d\u0430\u043d\u0441\u043e\u0432\u043e\u0433\u043e \u041f\u041e \u2014 \u0414\u0435\u043d\u0433\u0438\u0417\u0430\u0447\u0438\u0441\u043b\u0435\u043d\u044b\u041d\u0430\u0421\u0447\u0435\u0442 \u0438 \u043f\u0440\u043e\u0447\u0435\u0435 \u0432 \u044d\u0442\u043e\u043c \u0434\u0443\u0445\u0435. \u0422\u0430\u043a \u043a\u0430\u043a \u043d\u0430\u0448\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u0430\u044f \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u044d\u0442\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 TodoList \u0442\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e. \u0418 \u0442\u0430\u043a, \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u043d\u0430 \u0434\u043e\u0441\u043a\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043d\u0430\u0448\u0435\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 (\u0434\u043e\u043c\u0435\u043d\u0430) <br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/si\/b6\/wk\/sib6wkcz59jirqgd3giq-nkiudy.png\"><br \/>  \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044d\u0442\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442. <br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/ir\/sr\/om\/irsromhnzbbbtwnjkgkj0bzl7o8.png\"><br \/>  \u0414\u0430\u043b\u0435\u0435 \u0441\u0433\u0440\u0443\u043f\u043f\u0438\u0440\u0443\u0435\u043c \u044d\u0442\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432\u043e\u043a\u0440\u0443\u0433 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0441 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u043d\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u044b. <br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/3l\/jq\/tx\/3ljqtxhrge2ytbn92s8djmd4ry4.png\"><br \/>  \u041a\u043e\u043c\u0430\u043d\u0434\u044b \u0443 \u043c\u0435\u043d\u044f \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u044f\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u043e \u0432 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0441\u0435\u0440\u0432\u0438\u0441\u0430. \u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. <br \/>  \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043f\u0438\u0448\u0435\u043c \u0421\u043e\u0431\u044b\u0442\u0438\u044f \u0432 \u043a\u043e\u0434\u0435.   <\/p>\n<pre><code class=\"cs\">    public interface IDomainEvent     {       \/\/\u0422\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u043e\u043b\u0435. \u0414\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f id \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0432 Event Strore         Guid EventId { get; }        \/\/\u0422\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u043e\u043b\u0435. \u0421\u044e\u0434\u0430 \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043d\u043e\u043c\u0435\u0440 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0432 \u0441\u0442\u0440\u0438\u043c\u0435 Event Store         long EventNumber { get; set; }     }      public sealed class TodoCreated : IDomainEvent     {        \/\/Id \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e Todo         public Guid Id { get; set; }        \/\/\u0418\u043c\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e Todo         public string Name { get; set; }         public Guid EventId =&gt; Id;         public long EventNumber { get; set; }     }      public sealed class TodoRemoved : IDomainEvent     {         public Guid EventId { get; set; }         public long EventNumber { get; set; }     }      public sealed class TodoCompleted: IDomainEvent     {         public Guid EventId { get; set; }         public long EventNumber { get; set; }     } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0448\u0435 \u044f\u0434\u0440\u043e \u2014 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c   <\/p>\n<pre><code class=\"cs\">    public sealed class Todo : IEntity&lt;TodoId&gt;     {         private readonly List&lt;IDomainEvent&gt; _events;          public static Todo CreateFrom(string name)         {             var id = Guid.NewGuid();             var e = new List&lt;IDomainEvent&gt;(){new TodoCreated()                 {                     Id = id,                     Name = name                 }};             return new Todo(new TodoId(id), e, name, false);         }          public static Todo CreateFrom(IEnumerable&lt;IDomainEvent&gt; events)         {             var id = Guid.Empty;             var name = String.Empty;             var completed = false;             var ordered = events.OrderBy(e =&gt; e.EventNumber).ToList();             if (ordered.Count == 0)                 return null;             foreach (var @event in ordered)             {                 switch (@event)                 {                     case TodoRemoved _:                         return null;                     case TodoCreated created:                         name = created.Name;                         id = created.Id;                         break;                     case TodoCompleted _:                         completed = true;                         break;                     default: break;                 }             }             if (id == default)                 return null;             return new Todo(new TodoId(id), new List&lt;IDomainEvent&gt;(), name, completed);         }          private Todo(TodoId id, List&lt;IDomainEvent&gt; events, string name, bool isCompleted)         {             Id = id;             _events = events;             Name = name;             IsCompleted = isCompleted;             Validate();         }          public TodoId Id { get; }         public IReadOnlyList&lt;IDomainEvent&gt; Events =&gt; _events;         public string Name { get; }         public bool IsCompleted { get; private set; }          public void Complete()         {             if (!IsCompleted)             {                 IsCompleted = true;                 _events.Add(new TodoCompleted()                 {                     EventId = Guid.NewGuid()                 });             }         }          public void Delete()         {             _events.Add(new TodoRemoved()             {                 EventId = Guid.NewGuid()             });         }          private void Validate()         {             if (Events == null)                 throw new ApplicationException(&quot;\u041f\u0443\u0441\u0442\u043e\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043e\u0431\u044b\u0442\u0438\u0439&quot;);             if (string.IsNullOrWhiteSpace(Name))                 throw new ApplicationException(&quot;\u041f\u0443\u0441\u0442\u043e\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438&quot;);             if (Id == default)                 throw new ApplicationException(&quot;\u041f\u0443\u0441\u0442\u043e\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0437\u0430\u0434\u0430\u0447\u0438&quot;);         }     } <\/code><\/pre>\n<p>  \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u0441\u044f \u043a Event Store   <\/p>\n<pre><code class=\"cs\">            services.AddSingleton(sp =&gt;             { \/\/\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u043a TCP \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0440\u0430\u0437\u0440\u044b\u0432\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435.  \/\/\u0412 \u0441\u0430\u043c\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0435\u0441\u0442\u044c \u043e\u043f\u0446\u0438\u0438 \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u044d\u0442\u043e\u0433\u043e. \u041c\u043e\u0436\u043d\u043e \u0438\u0445 \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043d\u0430 \u043e\u0444 \u0441\u0430\u0439\u0442\u0435 \u0433\u043b\u044f\u043d\u0443\u0442\u044c.                 var con = EventStoreConnection.Create(new Uri(&quot;tcp:\/\/admin:changeit@127.0.0.1:1113&quot;), &quot;TodosConnection&quot;);                 con.ConnectAsync().Wait();                 return con;             }); <\/code><\/pre>\n<p>  \u0418 \u0442\u0430\u043a, \u0433\u043b\u0430\u0432\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438 \u0447\u0442\u0435\u043d\u0438\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0438\u0437 Event Store  <\/p>\n<pre><code class=\"cs\">    public sealed class EventsRepository : IEventsRepository     {         private readonly IEventStoreConnection _connection;          public EventsRepository(IEventStoreConnection connection)         {             _connection = connection;         }          public async Task&lt;long&gt; Add(Guid collectionId, IEnumerable&lt;IDomainEvent&gt; events)         {             var eventPayload = events.Select(e =&gt; new EventData( \/\/Id \u0441\u043e\u0431\u044b\u0442\u0438\u044f                 e.EventId, \/\/\u0422\u0438\u043f \u0441\u043e\u0431\u044b\u0442\u0438\u044f                 e.GetType().Name, \/\/\u0412 \u0432\u0438\u0434\u0435 Json (True|False)                 true, \/\/\u0422\u0435\u043b\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u044f                 Encoding.UTF8.GetBytes(JsonSerializer.Serialize((object)e)), \/\/\u041c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f                 Encoding.UTF8.GetBytes((string)e.GetType().FullName)             )); \/\/\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u0448\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435             var res = await _connection.AppendToStreamAsync(collectionId.ToString(), ExpectedVersion.Any, eventPayload);             return res.NextExpectedVersion;         }          public async Task&lt;List&lt;IDomainEvent&gt;&gt; Get(Guid collectionId)         {             var results = new List&lt;IDomainEvent&gt;();             long start = 0L;             while (true)             {                 var events = await _connection.ReadStreamEventsForwardAsync(collectionId.ToString(), start, 4096, false);                 if (events.Status != SliceReadStatus.Success)                     return results;                 results.AddRange(Deserialize(events.Events));                 if (events.IsEndOfStream)                     return results;                 start = events.NextEventNumber;             }         }          public async Task&lt;List&lt;T&gt;&gt; GetAll&lt;T&gt;() where T : IDomainEvent         {             var results = new List&lt;IDomainEvent&gt;();             Position start = Position.Start;             while (true)             {                 var events = await _connection.ReadAllEventsForwardAsync(start, 4096, false);                 results.AddRange(Deserialize(events.Events.Where(e =&gt; e.Event.EventType == typeof(T).Name)));                 if (events.IsEndOfStream)                     return results.OfType&lt;T&gt;().ToList();                 start = events.NextPosition;             }         }          private List&lt;IDomainEvent&gt; Deserialize(IEnumerable&lt;ResolvedEvent&gt; events) =&gt;             events                 .Where(e =&gt; IsEvent(e.Event.EventType))                 .Select(e =&gt;                 {                     var result = (IDomainEvent)JsonSerializer.Deserialize(e.Event.Data, ToType(e.Event.EventType));                     result.EventNumber = e.Event.EventNumber;                     return result;                 })                 .ToList();          private static bool IsEvent(string eventName)         {             return eventName switch             {                 nameof(TodoCreated) =&gt; true,                 nameof(TodoCompleted) =&gt; true,                 nameof(TodoRemoved) =&gt; true,                 _ =&gt; false             };         }         private static Type ToType(string eventName)         {             return eventName switch             {                 nameof(TodoCreated) =&gt; typeof(TodoCreated),                 nameof(TodoCompleted) =&gt; typeof(TodoCompleted),                 nameof(TodoRemoved) =&gt; typeof(TodoRemoved),                 _ =&gt; throw new NotImplementedException(eventName)             };         }     } <\/code><\/pre>\n<p>  \u0425\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043e\u0432\u0441\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e. \u041c\u044b \u0434\u043e\u0441\u0442\u0430\u0435\u043c \u0438\u0437 EventStore \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0435\u0435 \u0438\u0437 \u043d\u0438\u0445 \u0438\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438.  <\/p>\n<pre><code class=\"cs\">    public sealed class TodoRepository : ITodoRepository     {         private readonly IEventsRepository _eventsRepository;          public TodoRepository(IEventsRepository eventsRepository)         {             _eventsRepository = eventsRepository;         }          public Task SaveAsync(Todo entity) =&gt; _eventsRepository.Add(entity.Id.Value, entity.Events);          public async Task&lt;Todo&gt; GetAsync(TodoId id)         {             var events = await _eventsRepository.Get(id.Value);             return Todo.CreateFrom(events);         }          public async Task&lt;List&lt;Todo&gt;&gt; GetAllAsync()         {             var events = await _eventsRepository.GetAll&lt;TodoCreated&gt;();             var res = await Task.WhenAll(events.Where(t =&gt; t != null).Where(e =&gt; e.Id != default).Select(e =&gt; GetAsync(new TodoId(e.Id))));             return res.Where(t =&gt; t != null).ToList();         }     } <\/code><\/pre>\n<p>  \u0421\u0435\u0440\u0432\u0438\u0441 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u043c \u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c\u044e   <\/p>\n<pre><code class=\"cs\">    public sealed class TodoService : ITodoService     {         private readonly ITodoRepository _repository;          public TodoService(ITodoRepository repository)         {             _repository = repository;         }          public async Task&lt;TodoId&gt; Create(TodoCreateDto dto)         {             var todo = Todo.CreateFrom(dto.Name);             await _repository.SaveAsync(todo);             return todo.Id;         }          public async Task Complete(TodoId id)         {             var todo = await _repository.GetAsync(id);             todo.Complete();             await _repository.SaveAsync(todo);         }          public async Task Remove(TodoId id)         {             var todo = await _repository.GetAsync(id);             todo.Delete();             await _repository.SaveAsync(todo);         }          public async Task&lt;List&lt;TodoReadDto&gt;&gt; GetAll()         {             var todos = await _repository.GetAllAsync();             return todos.Select(t =&gt; new TodoReadDto()             {                 Id = t.Id.Value,                 Name = t.Name,                 IsComplete = t.IsCompleted             }).ToList();         }          public async Task&lt;List&lt;TodoReadDto&gt;&gt; Get(IEnumerable&lt;TodoId&gt; ids)         {             var todos = await Task.WhenAll(ids.Select(i =&gt; _repository.GetAsync(i)));             return todos.Where(t =&gt; t != null).Select(t =&gt; new TodoReadDto()             {                 Id = t.Id.Value,                 Name = t.Name,                 IsComplete = t.IsCompleted             }).ToList();         }     } <\/code><\/pre>\n<p>  \u041d\u0443 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u043e\u043a\u0430 \u043d\u0438\u0447\u0435\u0433\u043e \u0432\u043f\u0435\u0447\u0430\u0442\u043b\u044f\u044e\u0449\u0435\u0433\u043e. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u0441\u044f \u043a\u043e\u0433\u0434\u0430 \u044f \u0434\u043e\u0431\u0430\u0432\u043b\u044e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0411\u0414 \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0432\u0441\u0435 \u0437\u0430\u0438\u0433\u0440\u0430\u0435\u0442 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043a\u0440\u0430\u0441\u043a\u0430\u043c\u0438. \u042d\u0442\u043e \u0441\u0440\u0430\u0437\u0443 \u043d\u0430\u043c \u043f\u043e\u0432\u0435\u0441\u0438\u0442 \u043a\u043e\u043d\u0441\u0438\u0441\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c. Event Store \u0438 SQL \u0411\u0414 \u043f\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 \u043c\u0430\u0441\u0442\u0435\u0440 \u2014 \u0441\u043b\u0435\u0439\u0432. \u041e\u0434\u043d\u0430 \u0431\u0435\u043b\u0430\u044f ES \u0438 \u043c\u043d\u043e\u0433\u043e \u0447\u0435\u0440\u043d\u044b\u0445 MS SQL \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0447\u0438\u0442\u0430\u044e\u0442 \u0434\u0430\u043d\u043d\u044b\u0435.<br \/>   \u041b\u0438\u0440\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u0442\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435. \u0412 \u0441\u0432\u0435\u0442\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u043d\u0435 \u043c\u043e\u0433 \u043d\u0435 \u043f\u043e\u0448\u0443\u0442\u0438\u0442\u044c \u043f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 \u043c\u0430\u0441\u0442\u0435\u0440 \u0441\u043b\u0435\u0439\u0432 \u0438 \u0447\u0435\u0440\u043d\u044b\u0445 \u0431\u0435\u043b\u044b\u0445. \u042d\u0445\u0435, \u0443\u0445\u043e\u0434\u0438\u0442 \u044d\u043f\u043e\u0445\u0430, \u0431\u0443\u0434\u0435\u043c \u0432\u043d\u0443\u043a\u0430\u043c \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u0447\u0442\u043e \u0436\u0438\u043b\u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0430 \u043a\u043e\u0433\u0434\u0430 \u0431\u0430\u0437\u044b \u043f\u0440\u0438 \u0440\u0435\u043f\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u043d\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043c\u0430\u0441\u0442\u0435\u0440 \u0438 \u0441\u043b\u0435\u0439\u0432.<br \/>   \u0412 \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u0445 \u0433\u0434\u0435 \u043c\u043d\u043e\u0433\u043e \u0447\u0442\u0435\u043d\u0438\u044f \u0438 \u043c\u0430\u043b\u043e \u0437\u0430\u043f\u0438\u0441\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 (\u0442\u0430\u043a\u0438\u0445 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e) \u044d\u0442\u043e \u0434\u0430\u0441\u0442 \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u0430\u043c\u0430 \u0440\u0435\u043f\u043b\u0438\u043a\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0442\u0435\u0440 \u0441\u043b\u0435\u0439\u0432 \u043e\u043d\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u0430 \u043d\u0430 \u0442\u043e \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0437\u0430\u043c\u0435\u0434\u043b\u044f\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u0438\u0441\u044c (\u043a\u0430\u043a \u0438 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\u0438) \u043d\u043e \u0432\u0437\u0430\u043c\u0435\u043d \u0443\u0441\u043a\u043e\u0440\u044f\u0435\u0442\u0441\u044f \u0447\u0442\u0435\u043d\u0438\u0435 \u0437\u0430 \u0441\u0447\u0435\u0442 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u0411\u0414.<\/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\/514436\/\"> https:\/\/habr.com\/ru\/post\/514436\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/514436\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/hx\/ok\/cz\/hxokczmagjowkra41xnvppeasa8.png\"><br \/>  \u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u0420\u0435\u0448\u0438\u043b \u044f \u0437\u043d\u0430\u0447\u0438\u0442 \u043d\u0430 \u0432\u0440\u0435\u043c\u044f \u043e\u0442\u043e\u0439\u0442\u0438 \u043e\u0442 Scala, Idris \u0438 \u043f\u0440\u043e\u0447\u0435\u0433\u043e \u0424\u041f \u0438 \u0447\u0443\u0442\u044c \u0447\u0443\u0442\u044c \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e Event Store \u2014 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0432 \u043f\u043e\u0442\u043e\u043a\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u041a\u0430\u043a \u0432 \u0441\u0442\u0430\u0440\u043e\u0439 \u0434\u043e\u0431\u0440\u043e\u0439 \u043a\u043d\u0438\u0433\u0435 \u0443 \u043d\u0430\u0441 \u0442\u043e\u0436\u0435 \u043c\u0443\u0448\u043a\u0435\u0442\u0435\u0440\u043e\u0432 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 4 \u0438 \u0447\u0435\u0442\u0432\u0435\u0440\u0442\u044b\u0439 \u044d\u0442\u043e DDD. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Event Storming \u0432\u044b\u0434\u0435\u043b\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u044b, \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0441 \u043d\u0438\u043c\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435. \u041f\u043e\u0442\u043e\u043c \u0441\u0434\u0435\u043b\u0430\u044e \u043d\u0430 \u0438\u0445 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u0435\u0433\u043e \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435. \u0411\u0443\u0434\u0443 \u044f \u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043e\u0431\u044b\u0447\u043d\u044b\u0439 TodoList. \u0417\u0430 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u0434\u043e\u0431\u0440\u043e \u043f\u043e\u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434 \u043a\u0430\u0442.  <\/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-308210","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/308210","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=308210"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/308210\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=308210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=308210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=308210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}