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. SQL Server解惑——查询条件IN中能否使用变量

    在SQL Server的查询条件中,能否在IN里面使用变量呢? 如果可以的话,有没有需要注意的地方或一些限制呢?在回答这个问题前,我们先来看看这个例子: IF EXISTS (SELECT 1 FRO ...

  2. Cloudera Manager添加主机节点

    为了监控方便,想把研发环境中的主机节点都纳入Cloudera Manager的管理中,这样在遇到问题时可方便的查看主机的硬件资源情况. 添加主机节点有多种方式,由于我是离线工作,所以选择rpm包的方式 ...

  3. LeetCode404.左叶子之和

    题目 法一.广度优先搜索 1 class Solution { 2 public: 3 int sumOfLeftLeaves(TreeNode* root) { 4 if(root == NULL) ...

  4. 攻防世界 - Crypto(一)

    base64: 根据题目base64可知编码方式,下载附件发现是一个txt文件,把内容用工具解码就彳亍了,即可得到flag, flag: cyberpeace{Welcome_to_new_World ...

  5. pandas数据分析API常用操作

    1.导入数据 df = pd.read_csv( # 该参数为数据在电脑中的路径,可以不填写 filepath_or_buffer='/Users/Weidu/Desktop/sz000002.csv ...

  6. 关于SET/GET PARAMETER ID的注意事项

    通常这两个语法配合 PARAMETER, select-options中的参数 memory id来使用. 如,选择屏幕定义 PARAMETER p1 TYPE c LENGTH 10 MEMORY  ...

  7. JS编写的科学计算器

    最近半个月编写了一个JS+CSS+HTML的网页计算器,从最初的具有简陋界面的简单计算器改版到最终具有科学/标准计算器转换功能并且界面非常友好的计算器,收获良多!总的来说,代码简单,通俗易读,下面贴上 ...

  8. 提取当前文件夹下的所有文件名.bat(Windows批处理文件)

    @echo off dir /s/b *.* > 文件名.txt exit

  9. 架构风格 vs. 架构模式 vs. 设计模式(译)

    4.架构风格 vs. 架构模式 vs. 设计模式(译) - 简书 https://www.jianshu.com/p/d8dce27f279f

  10. Lambda架构正是这样一种用来处理不能够直接实时计算问题的通用架构

    https://mp.weixin.qq.com/s/BGHOw12iCASJy1pgkYZi3w 当数据处理做不到实时,应该怎么办?