CQRS之旅——旅程4(扩展和增强订单和注册限界上下文) (7)

下面的代码示例显示了一个非通用的DAO类,MVC控制器使用该类在读端查询会议信息。它封装了前面展示的ConferenceRegistrationDbContext类。

public class ConferenceDao : IConferenceDao { private readonly Func<ConferenceRegistrationDbContext> contextFactory; public ConferenceDao(Func<ConferenceRegistrationDbContext> contextFactory) { this.contextFactory = contextFactory; } public ConferenceDetails GetConferenceDetails(string conferenceCode) { using (var context = this.contextFactory.Invoke()) { return context .Query<Conference>() .Where(dto => dto.Code == conferenceCode) .Select(x => new ConferenceDetails { Id = x.Id, Code = x.Code, Name = x.Name, Description = x.Description, StartDate = x.StartDate }) .FirstOrDefault(); } } public ConferenceAlias GetConferenceAlias(string conferenceCode) { ... } public IList<SeatType> GetPublishedSeatTypes(Guid conferenceId) { ... } }

Jana(软件架构师)发言:

注意,这个ConferenceDao类只包含返回数据的方法。MVC控制器使用它来检索要在UI中显示的数据。

重构可用座位(SeatsAvailability)聚合

在我们CQRS之旅的第一阶段,领域包含一个ConferenceSeatsAvailabilty聚合根类,这是对会议剩余座位数量进行的建模。在旅程的现在这个阶段,团队将ConferenceSeatsAvailabilty聚合替换为SeatsAvailability,以反映特定会议可能有多种座位类型。例如,完整会议的席位、会前研讨会的席位和鸡尾酒会的席位。下图显示了新的SeatsAvailability聚合及其组成类。

这个聚合反应了下面两个模型:

一个会议可能有多种座位类型。

每个座位类型可能有不同的座位数量。

领域现在包括一个SeatQuantity值类型,您可以使用它来表示特定座椅类型的数量。

之前,聚合会根据是否有足够的座位数量来引发ReservationAccepted或ReservationRejected事件,现在,聚合引发一个SeatsReserved事件,该事件报告它可以预订多少个特定类型的座位。这意味着预留的座位数目可能与所要求的座位数目不相符。此信息被传递回UI,以便注册者决定如何继续预订。

AddSeats方法

您可能在最上面的架构图中注意到,SeatsAvailability聚合包含一个AddSeats方法,但没有相应的命令。AddSeats方法调整给定类型的可用座位总数。业务客户负责进行任何此类调整,并在Conference Management限界上下文中进行。当可用座位总数发生更改时,Conference Management限界上下文将引发事件。然后,SeatsAvailability类在其处理程序中调用AddSeat方法来处理事件。

对测试的影响

本节将讨论在现在这个阶段解决的一些测试问题。

验收测试和领域专家

在第3章“订单和注册限界上下文”中,您看到了一些UI原型,开发人员和领域专家一起工作,以改进系统的一些功能需求。这些UI原型的计划用途之一是为系统形成一组验收测试的基础。

对于验收测试方法,团队有以下目标:

验收测试应该以领域专家能够理解的格式清楚地表达出来。

应该可以自动执行验收测试。

为了实现这些目标,领域专家与测试团队的成员配对,并使用SpecFlow来指定核心验收测试。

使用SpecFlow feature来定义验收测试

使用SpecFlow定义验收测试的第一步是使用SpecFlow notation。这些测试被保存为feature文件在一个Visual Studio项目中。以下代码示例来自于ConferenceConfiguration.feature文件,该文件在Features\UserInterface\Views\Management文件夹下。它显示了Conference Management限界上下文的验收测试。典型的SpecFlow测试场景由一组Given、When和Then语句组成。其中一些语句包含测试使用的数据。

Markus(软件开发人员)发言:

事实上,SpecFlow feature文件使用Gherkin语言,这是一种专门为行为描述创建的领域特定语言(DSL)。

Feature: Conference configuration scenarios for creating and editing Conference settings In order to create or update a Conference configuration As a Business Customer I want to be able to create or update a Conference and set its properties Background: Given the Business Customer selected the Create Conference option Scenario: An existing unpublished Conference is selected and published Given this conference information | Owner | Email | Name | Description | Slug | Start | End | | William Flash | william@fabrikam.com | CQRS2012P | CQRS summit 2012 conference (Published) | random | 05/02/2012 | 05/12/2012 | And the Business Customer proceeds to create the Conference When the Business Customer proceeds to publish the Conference Then the state of the Conference changes to Published Scenario: An existing Conference is edited and updated Given an existing published conference with this information | Owner | Email | Name | Description | Slug | Start | End | | William Flash | william@fabrikam.com | CQRS2012U | CQRS summit 2012 conference (Original) | random | 05/02/2012 | 05/12/2012 | And the Business Customer proceeds to edit the existing settings with this information | Description | | CQRS summit 2012 conference (Updated) | When the Business Customer proceeds to save the changes Then this information appears in the Conference settings | Description | | CQRS summit 2012 conference (Updated) | ...

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wppzzg.html