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. 跟我一起学Redis之加个哨兵让主从复制更加高可用

    前言 主从复制的实现在上一篇已经分享过,虽然主从复制本身的确让读写分离更加高效,但是对于整体高可用存在很大的劣势:当主节点宕机了之后还需要人为重新进行主从关系配置:这不是开玩笑嘛,这样人为干预,故障恢 ...

  2. Mybatis的CRUD 增删改查

    目录 namespace 命名空间 select insert update delete Mybatis 官网: https://mybatis.org/mybatis-3/zh/getting-s ...

  3. 【RAC】打完补丁后,发现只有一台rac可以启动,另一台无法启动

    安装11Gr2单机asm后,打完11.2.0.3.7的psu后,发现启动不起来数据库,alert日志内容如下: Errors in file /u01/app/oracle/diag/rdbms/bd ...

  4. kubernetes之为每个命名空间的pod设置默认的requests以及limits

    一  为啥需要为命名空间里面添加pod添加默认的requests和limits? 通过前面的学习我们已经知道,如果节点上面的pod没有设置requests和limits,这些容器就会受那些设置了的控制 ...

  5. VBScript调用winscp,实现sftp操作

    最新有一个需求,需要在ssis中调用sftp下载文件,由于服务器上只有framework2.0,并且需要用sqlserver代理调用作业,限制了很多. 首先用的是脚本任务,进程调用winscp.com ...

  6. Vijos-P1103题解【线段树】

    本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: https://www.vijos.org/p/1103 题目描述: 一条马路从数轴0到L,每 ...

  7. [Usaco 2012 Feb]Nearby Cows

    题目描述 FJ发现他的牛经常跑到附近的草地去吃草,FJ准备给每个草地种足够的草供这个草地以及附近草地的奶牛来吃.FJ有N个草地(1<=N<=100000),有N-1条双向道路连接这些草地, ...

  8. 将ffmpeg编译为wasm版本且在浏览器中运行

    2020年大前端技术趋势解读 原创 IMWeb团队 腾讯IMWeb前端团队 5天前

  9. 一个支付宝小程序在一段时间内只能保留一个 WebSocket 连接

    一个支付宝小程序在一段时间内只能保留一个 WebSocket 连接 my.connectSocket - 支付宝开放平台 https://opendocs.alipay.com/mini/api/vx ...

  10. 类型检查和鸭子类型 Duck typing in computer programming is an application of the duck test 鸭子测试 鸭子类型 指示编译器将类的类型检查安排在运行时而不是编译时 type checking can be specified to occur at run time rather than compile time.

    Go所提供的面向对象功能十分简洁,但却兼具了类型检查和鸭子类型两者的有点,这是何等优秀的设计啊! Duck typing in computer programming is an applicati ...