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的更多相关文章

  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. (一)React Ant Design Pro + .Net5 WebApi:先搞定服务器,顺手装个Nginx

    腾讯云搞定服务器,具体过程就不赘述了,文档都有,咨询客服或者自行百度,体验一下过程. 一. 服务器 1. 云服务器 cvm 1核2G centos8.0 2. 域名注册 www.homejok.com ...

  2. 你都用过SpringCloud的哪些组件,它们的原理是什么?

    前言 看到文章的题目了吗?就是这么抽象和笼统的一个问题,确实是我面试中真实被问到的,某共享货车平台的真实面试问题. SpringCloud确实是用过,但是那是三四年前了,那个时候SpringCloud ...

  3. os.walk() 遍历目录下的文件夹和文件

    os.walk(top, topdown=True, onerror=None, followlinks=False) top:顶级目录 os.walk()返回一个三元tupple(dirpath, ...

  4. iptables自动屏蔽访问网站最频繁的IP

    iptables自动屏蔽访问网站频繁的IP 屏蔽每分钟访问超过200的IP 方法1:根据访问日志(Nginx为例 #!/bin/bash DATE=$(date +%d/%b/%Y:%H:%M) AB ...

  5. 关于JDK15的简单理解

    一.为什么要了解JDK15? 2020年9月15日,Oracle官方发布了JDK15版本,及时关注官方的更新动态,可以让我们在日常开发中更合理的选择更加优秀的工具方法,避免使用一些过时的或一些即将被删 ...

  6. 与图论的邂逅05:最近公共祖先LCA

    什么是LCA? 祖先链 对于一棵树T,若它的根节点是r,对于任意一个树上的节点x,从r走到x的路径是唯一的(显然),那么这条路径上的点都是并且只有这些点是x的祖先.这些点组成的链(或者说路径)就是x的 ...

  7. 1.2V转3.3V芯片电路图,超简电路

    镍氢可充电电池1.2V转成3.3V的电路和电子产品很多,在实际适用中,即使是两节镍氢电池串联供电也是会有供电电压下降和不稳定的影响,这是因为电池电量减少,而导致电池的电压也是会随着降低. 一般情况下, ...

  8. 运用 pyinstaller 打包的python exe文件运行 去掉命令行窗口及其他参数汇总

    运行exe文件的时候,会弹出一个dos命令窗口,这个窗口可以看到一些打印信息,如果想只运行tkinter 页面,去掉dos窗口需要在打包的时候 加上 -w 参数 pyinstaller -F XX.p ...

  9. (06)-Python3之--判断、循环

    1.判断(if) 语法: if 条件(True/False): 条件为真时,执行的代码(要干的事情)[elif 条件: 条件为真时,执行的代码(要干的事情)elif 条件: 条件为真时,执行的代码(要 ...

  10. SpringBoot配置文件基础部分说明

    SpringBoot-yml文件配置说明 友情提示:有一些代码中有乱码,请脑补删除,适合快速入门 #开启spring的Bebug模式,可以查看有哪些自动配置生效 #debug=true #开启热启动, ...