使用Apworks开发基于CQRS架构的应用程序(二):创建领域模型项目

领域模型是应用程序的核心,它包括了领域对象、状态、方法、事件、业务逻辑以及对象之间的关系。现在,我们来为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 按钮
    AddApworksReferenceDialog
  5. TinyLibrary.Domain 项目下,创建一个 Book 类,由于它是聚合根,因此使其继承于 SourcedAggregateRoot
       1: using System;
       2: using Apworks;
       3: using Apworks.Snapshots;
       4:  
       5: namespace TinyLibrary.Domain
       6: {
       7:     public class Book : SourcedAggregateRoot
       8:     {
       9:         public string Title { get; private set; }
      10:         public string Publisher { get; private set; }
      11:         public DateTime PubDate { get; private set; }
      12:         public string ISBN { get; private set; }
      13:         public int Pages { get; private set; }
      14:         public bool Lent { get; private set; }
      15:  
      16:         public Book() : base() {  }
      17:         public Book(long id) : base(id) {  }
      18:  
      19:         protected override void DoBuildFromSnapshot(ISnapshot snapshot)
      20:         {
      21:             throw new NotImplementedException();
      22:         }
      23:  
      24:         protected override ISnapshot DoCreateSnapshot()
      25:         {
      26:             throw new NotImplementedException();
      27:         }
      28:     }
      29: }
  6. 同理,在 TinyLibrary.Domain 项目下,创建一个 Reader
       1: using System;
       2: using System.Collections.Generic;
       3: using Apworks;
       4: using Apworks.Snapshots;
       5:  
       6: namespace TinyLibrary.Domain
       7: {
       8:     public class Reader : SourcedAggregateRoot
       9:     {
      10:         public string LoginName { get; private set; }
      11:         public string Name { get; private set; }
      12:         public List<Book> Books { get; private set; }
      13:  
      14:         public Reader() : base() { Books = new List<Book>(); }
      15:  
      16:         public Reader(long id) : base(id) { Books = new List<Book>(); }
      17:  
      18:         protected override void DoBuildFromSnapshot(ISnapshot snapshot)
      19:         {
      20:              throw new NotImplementedException();
      21:         }
      22:  
      23:         protected override ISnapshot DoCreateSnapshot()
      24:         {
      25:              throw new NotImplementedException();
      26:         }
      27:     }
      28: }

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

posted @ 2011-01-26 15:07  dax.net  阅读(4124)  评论(2编辑  收藏  举报