1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CQRS
  6. {
  7. public class EventBroker
  8. {
  9. public List<Event> AllEvents = new List<Event>();
  10. public EventHandler<Command> Commands;
  11. public EventHandler<Query> Queries;
  12.  
  13. public void Command(Command cmd)
  14. {
  15. Commands.Invoke(this, cmd);
  16. }
  17.  
  18. public T Query<T>(Query q)
  19. {
  20. Queries.Invoke(this, q);
  21. return (T)q.Result;
  22. }
  23. }
  24.  
  25. public class Event
  26. {
  27. }
  28.  
  29. public class Command : EventArgs
  30. {
  31. }
  32.  
  33. public class Query
  34. {
  35. public object Result;
  36. }
  37.  
  38. public class Person
  39. {
  40. private int age;
  41. EventBroker eventBroker;
  42. public Person(EventBroker eventBroker)
  43. {
  44. var self = this;
  45. this.eventBroker = eventBroker;
  46. this.eventBroker.Commands += (object sender, Command cmd) =>
  47. {
  48. var c = cmd as AgeChangedCommand;
  49. eventBroker.AllEvents.Add(new AgeChangedEvent(self, self.age, c.Age));
  50. self.age = c.Age;
  51. };
  52. this.eventBroker.Queries += (object sender, Query query) =>
  53. {
  54. var q = query as AgeQuery;
  55. q.Result = self.age;
  56. };
  57. }
  58. }
  59.  
  60. public class AgeChangedEvent : Event
  61. {
  62. public Person Target;
  63. public int oldValue;
  64. public int newValue;
  65.  
  66. public AgeChangedEvent(Person target, int oldVal, int newVal)
  67. {
  68. Target = target;
  69. oldValue = oldVal;
  70. newValue = newVal;
  71. }
  72.  
  73. public override string ToString()
  74. {
  75. return $"Age changed from {oldValue} to {newValue}";
  76. }
  77. }
  78.  
  79. public class AgeChangedCommand : Command
  80. {
  81. public Person Target;
  82. public int Age;
  83.  
  84. public AgeChangedCommand(Person p, int age)
  85. {
  86. Target = p;
  87. Age = age;
  88. }
  89. }
  90.  
  91. public class AgeQuery : Query
  92. {
  93. public Person Target;
  94. public AgeQuery(Person p)
  95. {
  96. Target = p;
  97. }
  98. }
  99.  
  100. class Program
  101. {
  102. static void Main(string[] args)
  103. {
  104. EventBroker eb = new EventBroker();
  105. Person p = new Person(eb);
  106.  
  107. //command
  108. eb.Command(new AgeChangedCommand(p, 18));
  109. eb.Command(new AgeChangedCommand(p, 30));
  110.  
  111. //event list
  112. foreach (var ev in eb.AllEvents)
  113. {
  114. Console.WriteLine(ev.ToString());
  115. }
  116.  
  117. //query
  118. var res = eb.Query<int>(new AgeQuery(p));
  119.  
  120. Console.WriteLine(res);
  121.  
  122. Console.ReadKey();
  123. }
  124. }
  125. }

下面是原视频:

https://www.bilibili.com/video/BV1HK411L7Lq/

CQRS+Event Sourcing的更多相关文章

  1. DDD创始人Eric Vans:要实现DDD原始意图,必须CQRS+Event Sourcing架构

    http://www.infoq.com/interviews/Technology-Influences-DDD# 要实现DDD(domain drive  design 领域驱动设计)原始意图,必 ...

  2. [外文理解] DDD创始人Eric Vans:要实现DDD原始意图,必须CQRS+Event Sourcing架构。

    原文:http://www.infoq.com/interviews/Technology-Influences-DDD# 要实现DDD(domain drive  design 领域驱动设计)原始意 ...

  3. CQRS Event Sourcing介绍

    什么是CQRS模式? CQRS是Command and Query Responsibility Segregation的缩写,直译就是命令与查询责任分离的意思. 命令会改变对象的状态,但不返回任何数 ...

  4. CQRS, Task Based UIs, Event Sourcing agh!

    原文地址:CQRS, Task Based UIs, Event Sourcing agh! Many people have been getting confused over what CQRS ...

  5. DDD CQRS和Event Sourcing的案例:足球比赛

    在12月11日新的有关DDD CQRS和Event Sourcing演讲:改变心态- 以更加面向对象视角看待业务领域建模中,作者以足球比赛football Match为案例说明传统编程方法和CQRS的 ...

  6. CQRS与Event Sourcing之浅见

    引言 DDD是近年软件设计的热门.CQRS与Event Sourcing作为实施DDD的一种选择,也逐步进入人们的视野.围绕这两个主题,软件开发的大咖[Martin Fowler].[Greg You ...

  7. Event Sourcing Pattern 事件源模式

    Use an append-only store to record the full series of events that describe actions taken on data in ...

  8. Typed Message模式与Event Sourcing

    引言 在<设计模式沉思录>(Pattern Hatching: Design Patterns Applied,[美]JohnVlissides著)一书的第4章中,围绕事件Message传 ...

  9. Event Sourcing

    Event Sourcing - ENode(二) 接上篇文章继续 http://www.cnblogs.com/dopeter/p/4899721.html 分布式系统 前篇谈到了我们为何要使用分布 ...

随机推荐

  1. rename 表名

    rename table 旧表名1 to 新表名1,旧表名2 to 新表名2;

  2. LeetCode530. 二叉搜索树的最小绝对差

    题目 又是常见的BST,要利用BST的性质,即中序遍历是有序递增序列. 法一.中序遍历 1 class Solution { 2 public: 3 vector<int>res; 4 v ...

  3. oracle查看用户的系统权限,角色以及数据库对象权限

    select * from dba_sys_privs where GRANTEE='monkey'; select * from dba_role_privs where GRANTEE='monk ...

  4. 敏捷史话(四):敏捷是人的天性 —— Arie van Bennekum

    敏捷是人的天性,是你与生俱来的东西.面对敏捷,Arie van Bennekum 下了这样一个结论. 但这并不意味着人们只能通过天赋获得敏捷,对于想要学习敏捷的人来说,敏捷绝不是仅仅靠学习僵化的框架. ...

  5. Eureka详解系列(一)--先谈谈负载均衡器

    这个系列开始研究 Eureka,在此之前,先来谈谈负载均衡器. 本质上,Eureka 就是一个负载均衡器,可能有的人会说,它是一个服务注册中心,用来注册服务的,这种说法不能说错,只是有点片面. 在这篇 ...

  6. Docker相关简介以及使用方法

    Docker: 可以把它看作是一个软件,在这个软件当中呢,还可以安装其他的软件,还可以把软件所需要的环境依赖一起添加进来,这样让开发人员的程序在不同的环境当中都可以流转起来,避免了程序出现" ...

  7. JavaScript中eval的替代方法

    引自:https://www.cnblogs.com/lxg0/p/7805266.html 通常我们在使用ajax获取到后台返回的json数据时,需要使用 eval 这个方法将json字符串转换成对 ...

  8. 如何创建一个Java项目

    目录 新建项目 项目信息配置 创建Java类 编译和运行 新建项目 首先双击eclipse进入到eclipse页面. 菜单"File"下的"New"里" ...

  9. Spring Data JPA基本增删改查和JPQL查询(含完整代码和视频连接)

    问题:SpringDataJPA怎么使用? 一.考察目标 主要考核SpringDataJPA的用法 二.题目分析 spring data jpa 的使用步骤(下面有具体实现细节) 1.创建maven工 ...

  10. Centos GitLab 配置

    如果重启之后,gitlab-ctl restart报"runsv no running"错,先运行下面命令 systemctl start gitlab-runsvdir.serv ...