zoukankan      html  css  js  c++  java
  • 【转】A brief overview of Ncqrs

    What is CQRS / Ncqrs?
    CQRS is an evolutionary step in Domain-Driven Design (DDD). Ncqrs is an open-source implementation for.NET.


    Links: CQRS
    Wikipedia's entry for Command-query separation
    Udi Dahan's explanation of Command-Query Responsibility Segregation.
    Greg Young's video presentation CQRS and Event Sourcing - the Business Perspective.

    Links: Ncqrs 
    For information on Ncqrs, see ncqrs.org.
    To try out the Ncqrs walkthrough tutorial, go here.


    The goal of this blog post is to compare Ncqrs with regular Domain-Driven Design (particularly when used with Remote Facade), and to examine and understand the processes described in the Ncqrs tutorial.

    In regular Domain-Driven Design across a web service boundary, both inputs (commands) and outputs (queries) share the same RemoteFacade and Application Service Layer, as shown in Fig. 1. 

    Fig.1  Regular DDD with Remote Facade
    CQRS
    In Command-Query Responsibility Segregation Pattern, inputs (commands) and outputs (queries) are separated and treated very differently. As Greg Young points out in the video presentation (above), using the RemoteFacade/DDD architecture for simple queries leads to an immense amount of busy work (mapping from the domain to DTOs, etc), when the real purpose of the domain model is to implement behavior/business logic--which only occurs based on input (commands).

    Therefore:
    CQRS in 50 words or less
    1) Keep DDD and the domain model, but call it only from commands. 
    2) Add a new feature: TRACK each command as event history
    3) For queries, SKIP the domain model. Do queries quick, dirty and denormalized.

    How does Ncqrs implement this approach? Let's walk through the journey that a command follows, based on their tutorial. I've created the following diagram as a guide:

    1. In your backend behind the RemoteFacade, create a custom command class such as PostNewTweetCommand (which inherits from Ncqrs.Commanding.CommandBase).
    2. For any input values you wish to pass to the domain model, create properties in the custom command class. For example: 

      public string Message { get; set; }
      public string Who { get; set; }

    3. In your WCF web service, create a method which accepts PostNewTweetCommand as input parameter.
    4. In your client, create the WCF proxy.
    5. In your client, create an action which instantiates PostNewTweetCommand, sets its property values, and passes it to the web service method.
    6. In your WCF web service method, get an ICommandService instance (from Ncqrs.Commanding.ServiceModel) and use it to execute the incoming PostNewTweetCommand input parameter.
    7. Create a PostNewTweetCommandExecutor class (which inherits from CommandExecutorBase<PostNewTweetCommand>) that--by convention--listens for executed instances of PostNewTweetCommand
    8. Have the command executor execute the incoming command, in the context of an IUnitOfWork, against a domain model class (typically an aggregate root)
    9. In the domain model class (eg. Tweet.cs), every public action (such as instantiation, or a method call) creates a domain event to process the incoming command, map over its input values, and then call ApplyEvent().
    10. In this case, the contextual IUnitOfWork writes the event (TweetPostedEvent) to the event store.
    11. As part of setting up the WCF service, three services have been bootstrapped in service of CQRS goals: 

      • ICommandService - already mentioned, this processes the incoming commands, and uses convention to call the correct command executor
      • IEventStore - this processes each event transaction and writes it out to a separate data store (in the tutorial example, a SQL Server database)
      • IEventBus - listens for new events, and for each one, makes a call out to the final piece of the ncqrs puzzle (described next)
    12. The event bus alerts classes (again, by convention) which inherit from IDenormalizer where T is the particular event that has been saved.
    13. The implementing class (in this case, TweetIndexItemDenormalizer is so named based on a new, separate database whose purpose is to store, simple, denormalized, read data.
    14. The TweetIndexItemDenormalizer class implements a Handle method, which takes the TweetPostedEvent as input param, and write the values stored in that event to the denormalized data table.
    15. Finally, the client can now (very simply) access data from this denormalized table (eg. as a simple Linq to SQL call to get a list of matching values.)
  • 相关阅读:
    编程语言的发展趋势及未来方向 目录 编程语言的发展趋势及未来方向 1 第一章 一、历史回顾及趋势概述 2 第一节 首先,编程语言的发展非常缓慢。oo等等,但是远没有好上1000倍。 3 第二节 出现
    Atitit datatype 数据类型 目录 第一章 三大基本类型 数字 字符串 bool 1 第二章 基本类型vs引用类型 1 字符串类型 2 第三章 符合类型vs 简单类型 2 特殊类型
    Atitit 乔姆斯基分类 语言的分类 目录 1.1. 0 –递归可枚举语法 1 1.2. 1 –上下文相关的语法 自然语言 1 1.3. 2 –上下文无关的语法 gpl编程语言 1 1.4. 3
    桁架系统控制
    Pset_RoofCommon
    Pset_ShadingDeviceCommon
    Pset_SlabCommon
    gis数据下载地址
    Pset_StairCommon
    Pset_StairFlightCommon
  • 原文地址:https://www.cnblogs.com/NickYao/p/1853529.html
Copyright © 2011-2022 走看看