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. PHP MySQLi extension is not loaded

    PHP MySQLi extension is not loaded 如何解决呢?  yum -y install mysqli.so  huozhe yum -y install php-mysql

  2. Can't locate CPAN.pm in @INC

    [root@test]# perl -MCPAN -e 'install DBD::mysql'Can't locate CPAN.pm in @INC (@INC contains: /usr/lo ...

  3. kubernets之namespace

    一 命名空间的介绍以及作用 1  概念 为了方便不同部门之间对kubernets集群的使用,并且对其进行有效的隔离,kubernets提供了一种资源隔离手段,通过将各种不同资源分组到 一个区域,并且统 ...

  4. LeetCode543.二叉树的直径

    题目 1 class Solution { 2 public: 3 int minimum = INT_MIN; 4 vector<int>res; 5 int diameterOfBin ...

  5. java基础-01代理类

    简单的代理类实现案例主实现类:ProxyTestimport java.lang.reflect.InvocationHandler;import java.lang.reflect.Proxy;im ...

  6. python 字典(formkey 建立 取值 赋值 删除 )

      formkey快速建立空字典   result = {}.fromkeys(['name','age','job'],None) print(result)   #往字典里添加元素 result. ...

  7. 【Windows】Win10家庭版启用组策略gpedit.msc

    [前言] 大家都认为,Windows 10家庭版中并不包含组策略,其实不然,它是有相关文件的,只是不让你使用而已.那么我们让系统允许你使用就好了. [操作步骤] 1.首先你需要在桌面上新建一个txt文 ...

  8. C指针的这些使用技巧,掌握后立刻提升一个Level

    这是道哥的第016篇原创 关注+星标公众号,不错过最新文章 目录 一.前言 二.八个示例 1. 开胃菜:修改主调函数中的数据 2. 在被调用函数中,分配系统资源 2.1 错误用法 2.2 正确用法 3 ...

  9. 登陆的时候出现javax.xml.bind.DatatypeConverter错误

    错误详情: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/Da ...

  10. Server:www121 Server:www120 Server:NWS_SP 内容被散列,并在响应中放入Etag When to Use Entity-Tags and Last-Modified Dates

    1 Request URL:http://www.biyao.com/minisite/bzzx 2 Request Method:GET 3 Status Code:200 OK 4 Remote ...