- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
遵循 CQRS(命令查询责任分离)的概念,我直接在我的 MVC 应用程序中引用 DAL,并通过 ViewModel 进行所有读取。然而,我的一位同事问我,如果在读取时必须应用任何业务逻辑,你会怎么做。例如如果您需要在如下场景中计算百分比值:
//Employee domain object
class Employee
{
string EmpName;
Single Wages;
}
//Constant declared in some utility class. This could be stored in DB also.
const Single Tax = 15;
//View Model for the Employee Screen
class EmployeeViewModel
{
string EmpName;
Single GrossWages;
Single NetWages;
}
// Read Facade defined in the DAL
class ReadModel
{
List<EmployeeViewModel> GetEmployeeList()
{
List<EmployeeViewModel> empList = new List<EmployeeViewModel>;
string query = "SELECT EMP_NAME, WAGES FROM EMPLOYEE";
...
..
while(reader.Read())
{
empList.Add(
new EmployeeViewModel
{
EmpName = reader["EMP_NAME"],
GrossWages = reader["WAGES"],
NetWages = reader["WAGES"] - (reader["WAGES"]*Tax)/100 /*We could call a function here but since we are not using the business layer, the function will be defined in the DAL layer*/
}
);
}
}
}
在上面的示例中,在 DAL 层中发生的读取期间发生计算。我们本可以创建一个函数来进行计算,但由于我们已经绕过业务层进行读取,因此该函数将位于 DAL 中。更糟糕的是,如果 Tax 的值存储在数据库中,有人可能会直接在存储过程中的数据库中执行此操作。因此,我们可能会在其他层中泄漏业务逻辑。
您可能会说为什么不在执行命令时将计算值存储在列中。因此,让我们稍微改变一下场景。假设您在一份报告中显示员工的潜在净工资和当前税率,而工资尚未支付。
你会如何在 CQRS 中处理这个问题?
最佳答案
我的理解是,CQRS 与 DDD 相结合会产生一个查询端,该查询端跨限界上下文聚合数据,而命令端则严格针对该特定命令的限界上下文执行命令。
这将使您的报告根据需要检索其数据。
然后您可以将一些 ICalculator 注入(inject)到读取端的查询处理程序中以进行业务逻辑计算。
例如:
public class EmployeeQueryHandler : EmployeeIQueryHandler
{
private readonly INetWageCalculator _calculator;
private readonly IEmployeeRepository _repo;
public Repository(INetWageCalculator calculator, IEmployeeRepository repo)
{
_calculator = calculator;
_repo = repo;
}
public List<EmployeeViewModel> ExecuteQuery()
{
var employees = _repo.GetEmployeeList();
foreach(var emp in employees)
{
// You have to get tax from somewhere, perhaps its passed in as
// a parameter...
emp.NetWages = _calculator.Calculate(emp.GrossWages, Tax);
}
return employees;
}
}
public class EmployeeRepository : IEmployeeRepository
{
List<EmployeeViewModel> GetEmployeeList()
{
List<EmployeeViewModel> empList = new List<EmployeeViewModel>;
string query = "SELECT EMP_NAME, WAGES FROM EMPLOYEE";
...
..
while (reader.Read())
{
empList.Add(
new EmployeeViewModel
{
EmpName = reader["EMP_NAME"],
GrossWages = reader["WAGES"],
// This line moves to the query handler.
//NetWages = reader["WAGES"] - (reader["WAGES"] * Tax) / 100 /*We could call a function here but since we are not using the business layer, the function will be defined in the DAL layer*/
}
);
}
}
}
这使您可以在其他地方使用相同的计算器服务重用计算净工资的业务逻辑。
为了性能,如果您不想循环遍历结果两次,您也可以将计算器注入(inject)存储库。
关于architecture - CQRS:查询端的业务逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11465025/
我了解了 CQRS 的一般概念,但是当涉及到超越示例代码和幻灯片以处理现实世界问题时,我有一些问题。 验证 当您需要对涉及检查数据库值的命令进行验证时,您会怎么做? 注册一项服务,我必须输入一个唯一的
我目前正在尝试了解如何构建事件存储的内部结构。到目前为止我得到了什么: 一个事件存储有两个表(集合,...),一个用于聚合,一个用于事件。 聚合表包含以下数据:aggregateId (可能是一个 G
我最近阅读了很多关于 CQRS 的文章,对我来说,它似乎与 Event Sourcing 密切相关。 但就像这个答案说的https://stackoverflow.com/a/9217461/2770
我有 CQRS+ES 设计的应用程序。这是我在过去一年里一直在 CQRS+ES 世界中阅读的新内容,它非常有意义,但实现完美的意义并不总是那么容易。 无论如何,我的一个或多个问题是: 包含多个命令(步
我想知道如何在 CQRS 中创建多个聚合根。 示例:我有一个手机聚合根和 Simcard 聚合根。身份证 这些聚合应该是订阅聚合根的一部分。 我需要创建一个基于 SimCard 的订阅聚合,并且系统中
用户在我们的站点上注册并登录。RegisterUserCommand 被发送到异步命令处理程序。 用户想要更改他们的地址,但 RegisterUserCommand 尚未处理。系统中没有用户记录。 这
事件溯源和 CQRS 很棒,因为它使开发人员摆脱了一个预先建模的数据库的困扰,除非有大数据迁移项目,否则开发人员必须在应用程序的整个生命周期中使用该数据库。 CQRS 和 ES 还具有其他好处,例如扩
我想我理解了 ES + CQRS 上下文中读取模型的思想(如果不正确请指正)。然而,我对在“严肃”报道的背景下使用它仍有一些疑问。假设我使用关系数据库和一些 ORM 来处理我的读取模型。一个基本的“摘
我正在使用 Akka 持久性实现 CQRS 系统,并且我正在尝试了解 CQRS 的请求响应位。 关于如何将响应发送回客户端和 this article 的答案很少。还提到了一些不错的模式。但是,不是使
在基于 CQRS/ES 的系统中,您将事件存储在事件存储中。这些事件涉及一个聚合,并且它们相对于它们所属的聚合有一个顺序。此外,聚合是一致性/事务边界,这意味着任何事务保证仅在每个聚合级别上给出。 现
根据CQRS à la Greg Young ,事件处理程序(和下游事件反规范化器)对事件发布者之前发布的传入事件使用react。 现在让我们假设在运行时我们想要添加一个新的事件反规范化器:基本上,这
是否存在单个命令导致 CQRS 中有多个事件的情况?任何人都可以举一个例子吗? 我问是因为聚合是事务边界,编写聚合的实现取决于这个问题的答案。 最佳答案 是的,例如考虑命令 AssignUserToG
大概我们可以通过应用相同的命令集来恢复状态,那么为什么不简单地存储命令而不是事件呢? 最佳答案 事件,传达“这发生在我们的系统中”。当一个命令被接受和处理时,事件就会发生。没有人可以拒绝或改变它发生的
我正在学习什么是CQRS模式,并了解到还有CQS模式。 当我尝试搜索时,我发现了很多关于CQRS的图表和信息,但没有找到太多关于CQS的信息。 CQRS 模式中的关键点 在 CQRS 中,有一种要写入
我的CQRS / ES设计中有时间问题。为了便于讨论,让我们基于Microsoft的 有关此主题的示例, session 管理(https://msdn.microsoft.com/en-us/lib
我只是想知道我将尝试实现的设计是否有效 CQRS。 我将有一个查询处理程序,它本身会向其他子处理程序发送更多查询。它的主要任务是聚合来自多个服务的结果。 从处理程序内部发送查询可以吗?我已经可以在我的
我第一次尝试使用 CQRS 和事件溯源,我有几点需要一些指导。我想实现一个 SO 风格的声誉系统。这似乎非常适合这种架构。 以 SO 为例。假设一个问题被投票,这会生成一个 UpvoteCommand
我的问题与this one有关.虽然相关的问题和答案说明了我们为什么要将它们分开,但我想确保我对意图的理解是正确的。在我见过的所有示例中,命令的意图似乎是它可以被拒绝,并且它更新内存中的对象,然后事件
这更像是一个与 CQRS 和 Axon 相关的理论问题。下面是一个不言自明的设置,代码是 伪代码 ,它并不意味着编译。 假设为了处理来自聚合“Foo”的命令,我们首先需要查询另一个聚合“Bar”的状态
亲们,我正在考虑将 Dapper.net 等 microORM 用于 CQRS 应用程序 (Asp.Net MVC) 的读取访问组件,并使用 Entity Framework 来操作域。 这是 CQR
我是一名优秀的程序员,十分优秀!