领域模型是应用程序的核心,它包括了领域对象、状态、方法、事件、业务逻辑以及对象之间的关系。现在,我们来为Tiny Library CQRS创建一个领域模型项目。

  1. Solution Explorer 下,右键单击TinyLibraryCQRS项目,单击 Add | New Project… 菜单,这将打开 Add New Project 对话框
  2. Installed Templates 选项卡下,选择 Visual C# | Windows,然后选择 Class Library ,确保所选的Framework版本是.NET Framework 4,在 Name 文本框中,输入TinyLibrary.Domain,然后单击OK按钮
  3. Solution Explorer 下,右键单击 TinyLibrary.Domain 项目的 References 节点,单击 Add Reference… 菜单,这将打开 Add Reference 对话框
  4. .NET 选项卡下,选择 Apworks 然后单击 OK 按钮
    使用Apworks开发基于CQRS架构的应用程序(二):创建领域模型项目
  5. TinyLibrary.Domain 项目下,创建一个 Book 类,由于它是聚合根,因此使其继承于 SourcedAggregateRoot
    using System;
    using Apworks;
    using Apworks.Snapshots;
       4:  
    namespace TinyLibrary.Domain
       6: {
    class Book : SourcedAggregateRoot
       8:     {
    private set; }
    private set; }
    private set; }
    private set; }
    private set; }
    private set; }
      15:  
    base() {  }
    base(id) {  }
      18:  
    void DoBuildFromSnapshot(ISnapshot snapshot)
      20:         {
    new NotImplementedException();
      22:         }
      23:  
    override ISnapshot DoCreateSnapshot()
      25:         {
    new NotImplementedException();
      27:         }
      28:     }
      29: }
  6. 同理,在 TinyLibrary.Domain 项目下,创建一个 Reader
    using System;
    using System.Collections.Generic;
    using Apworks;
    using Apworks.Snapshots;
       5:  
    namespace TinyLibrary.Domain
       7: {
    class Reader : SourcedAggregateRoot
       9:     {
    private set; }
    private set; }
    private set; }
      13:  
    new List<Book>(); }
      15:  
    new List<Book>(); }
      17:  
    void DoBuildFromSnapshot(ISnapshot snapshot)
      19:         {
    new NotImplementedException();
      21:         }
      22:  
    override ISnapshot DoCreateSnapshot()
      24:         {
    new NotImplementedException();
      26:         }
      27:     }
      28: }

从上面两段代码可以看出,有两个protected的方法我们还没有实现,这两个方法是定义在SourcedAggregateRoot基类中的,用来提供与“快照”相关的功能。“快照”是CQRS架构中非常重要的一部分,在下一章节中,我们将了解到,如何为我们的领域模型添加快照功能。

相关文章:

  • 2021-10-16
  • 2021-09-05
  • 2021-11-05
  • 2021-08-14
  • 2021-06-15
  • 2021-09-19
  • 2021-08-30
猜你喜欢
  • 2021-10-29
  • 2022-02-27
  • 2022-03-02
  • 2021-11-08
  • 2022-01-14
  • 2021-12-07
  • 2021-12-22
相关资源
相似解决方案