CQRS+Event Sourcing
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace CQRS
- {
- public class EventBroker
- {
- public List<Event> AllEvents = new List<Event>();
- public EventHandler<Command> Commands;
- public EventHandler<Query> Queries;
- public void Command(Command cmd)
- {
- Commands.Invoke(this, cmd);
- }
- public T Query<T>(Query q)
- {
- Queries.Invoke(this, q);
- return (T)q.Result;
- }
- }
- public class Event
- {
- }
- public class Command : EventArgs
- {
- }
- public class Query
- {
- public object Result;
- }
- public class Person
- {
- private int age;
- EventBroker eventBroker;
- public Person(EventBroker eventBroker)
- {
- var self = this;
- this.eventBroker = eventBroker;
- this.eventBroker.Commands += (object sender, Command cmd) =>
- {
- var c = cmd as AgeChangedCommand;
- eventBroker.AllEvents.Add(new AgeChangedEvent(self, self.age, c.Age));
- self.age = c.Age;
- };
- this.eventBroker.Queries += (object sender, Query query) =>
- {
- var q = query as AgeQuery;
- q.Result = self.age;
- };
- }
- }
- public class AgeChangedEvent : Event
- {
- public Person Target;
- public int oldValue;
- public int newValue;
- public AgeChangedEvent(Person target, int oldVal, int newVal)
- {
- Target = target;
- oldValue = oldVal;
- newValue = newVal;
- }
- public override string ToString()
- {
- return $"Age changed from {oldValue} to {newValue}";
- }
- }
- public class AgeChangedCommand : Command
- {
- public Person Target;
- public int Age;
- public AgeChangedCommand(Person p, int age)
- {
- Target = p;
- Age = age;
- }
- }
- public class AgeQuery : Query
- {
- public Person Target;
- public AgeQuery(Person p)
- {
- Target = p;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- EventBroker eb = new EventBroker();
- Person p = new Person(eb);
- //command
- eb.Command(new AgeChangedCommand(p, 18));
- eb.Command(new AgeChangedCommand(p, 30));
- //event list
- foreach (var ev in eb.AllEvents)
- {
- Console.WriteLine(ev.ToString());
- }
- //query
- var res = eb.Query<int>(new AgeQuery(p));
- Console.WriteLine(res);
- Console.ReadKey();
- }
- }
- }
下面是原视频:
https://www.bilibili.com/video/BV1HK411L7Lq/
CQRS+Event Sourcing的更多相关文章
- DDD创始人Eric Vans:要实现DDD原始意图,必须CQRS+Event Sourcing架构
http://www.infoq.com/interviews/Technology-Influences-DDD# 要实现DDD(domain drive design 领域驱动设计)原始意图,必 ...
- [外文理解] DDD创始人Eric Vans:要实现DDD原始意图,必须CQRS+Event Sourcing架构。
原文:http://www.infoq.com/interviews/Technology-Influences-DDD# 要实现DDD(domain drive design 领域驱动设计)原始意 ...
- CQRS Event Sourcing介绍
什么是CQRS模式? CQRS是Command and Query Responsibility Segregation的缩写,直译就是命令与查询责任分离的意思. 命令会改变对象的状态,但不返回任何数 ...
- CQRS, Task Based UIs, Event Sourcing agh!
原文地址:CQRS, Task Based UIs, Event Sourcing agh! Many people have been getting confused over what CQRS ...
- DDD CQRS和Event Sourcing的案例:足球比赛
在12月11日新的有关DDD CQRS和Event Sourcing演讲:改变心态- 以更加面向对象视角看待业务领域建模中,作者以足球比赛football Match为案例说明传统编程方法和CQRS的 ...
- CQRS与Event Sourcing之浅见
引言 DDD是近年软件设计的热门.CQRS与Event Sourcing作为实施DDD的一种选择,也逐步进入人们的视野.围绕这两个主题,软件开发的大咖[Martin Fowler].[Greg You ...
- Event Sourcing Pattern 事件源模式
Use an append-only store to record the full series of events that describe actions taken on data in ...
- Typed Message模式与Event Sourcing
引言 在<设计模式沉思录>(Pattern Hatching: Design Patterns Applied,[美]JohnVlissides著)一书的第4章中,围绕事件Message传 ...
- Event Sourcing
Event Sourcing - ENode(二) 接上篇文章继续 http://www.cnblogs.com/dopeter/p/4899721.html 分布式系统 前篇谈到了我们为何要使用分布 ...
随机推荐
- rename 表名
rename table 旧表名1 to 新表名1,旧表名2 to 新表名2;
- LeetCode530. 二叉搜索树的最小绝对差
题目 又是常见的BST,要利用BST的性质,即中序遍历是有序递增序列. 法一.中序遍历 1 class Solution { 2 public: 3 vector<int>res; 4 v ...
- oracle查看用户的系统权限,角色以及数据库对象权限
select * from dba_sys_privs where GRANTEE='monkey'; select * from dba_role_privs where GRANTEE='monk ...
- 敏捷史话(四):敏捷是人的天性 —— Arie van Bennekum
敏捷是人的天性,是你与生俱来的东西.面对敏捷,Arie van Bennekum 下了这样一个结论. 但这并不意味着人们只能通过天赋获得敏捷,对于想要学习敏捷的人来说,敏捷绝不是仅仅靠学习僵化的框架. ...
- Eureka详解系列(一)--先谈谈负载均衡器
这个系列开始研究 Eureka,在此之前,先来谈谈负载均衡器. 本质上,Eureka 就是一个负载均衡器,可能有的人会说,它是一个服务注册中心,用来注册服务的,这种说法不能说错,只是有点片面. 在这篇 ...
- Docker相关简介以及使用方法
Docker: 可以把它看作是一个软件,在这个软件当中呢,还可以安装其他的软件,还可以把软件所需要的环境依赖一起添加进来,这样让开发人员的程序在不同的环境当中都可以流转起来,避免了程序出现" ...
- JavaScript中eval的替代方法
引自:https://www.cnblogs.com/lxg0/p/7805266.html 通常我们在使用ajax获取到后台返回的json数据时,需要使用 eval 这个方法将json字符串转换成对 ...
- 如何创建一个Java项目
目录 新建项目 项目信息配置 创建Java类 编译和运行 新建项目 首先双击eclipse进入到eclipse页面. 菜单"File"下的"New"里" ...
- Spring Data JPA基本增删改查和JPQL查询(含完整代码和视频连接)
问题:SpringDataJPA怎么使用? 一.考察目标 主要考核SpringDataJPA的用法 二.题目分析 spring data jpa 的使用步骤(下面有具体实现细节) 1.创建maven工 ...
- Centos GitLab 配置
如果重启之后,gitlab-ctl restart报"runsv no running"错,先运行下面命令 systemctl start gitlab-runsvdir.serv ...