CQRS:查询端的业务逻辑 [英] CQRS: business logic on the query side

查看:21
本文介绍了CQRS:查询端的业务逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循 CQRS(命令查询职责分离)的概念,我在我的 MVC 应用程序中直接引用 DAL,并通过 ViewModel 进行所有读取.然而,我的一位同事问我,当阅读时必须应用任何业务逻辑时,你会怎么做.例如如果您需要在如下场景中计算百分比值:

Following the concept of CQRS (Command Query Responsibility Segregation), I am directly referring the DAL in my MVC application and doing all reads via the ViewModels. However a colleague of mine is asking me what will you do when any business logic has to be applied when doing a read. For e.g. if you need to compute a percentage value in scenario like below:

//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 的值存储在数据库中,有人可能会直接在存储过程中的数据库中执行此操作.因此,我们在其他层中存在业务逻辑的潜在泄漏.

In above example, there is a calcuation occuring during the read which is occuring in the DAL layer. We could have created a function to do the calculation but again since we have bypassed the business layer for our read, the function will be located in the DAL. Even worse, someone might do it directly in the DB in a stored proc if the value of Tax is stored in the DB. So we have a potential leakage of business logic here in other layers.

您可能会说为什么不在执行命令时将计算值存储在列中.所以让我们稍微改变一下场景.假设您在一份报告中显示员工的潜在净工资,其中包含当前税率,但工资尚未支付.
你将如何在 CQRS 中处理这个问题?

You might say why don't you store the computed value in a column while doing the command. So let us change the scenario a bit. Let us say you are showing the potential Net Wages for the employee in a report with the current Tax rate and the Wages are yet to be paid.
How would you handle this in CQRS ?

推荐答案

请注意,报告本身可以是一个完整的有界上下文.因此,它的架构可能与您为核心域选择的架构完全不同.

Please take into consideration that reporting can be a whole Bounded Context in its own right. Therefore its architecture can be completely different from the one you chose for your Core Domain.

也许 CQRS 非常适合核心领域,但不适合报告领域.特别是当您想在生成报告之前根据不同场景应用各种计算时.思考商业智能.

Maybe CQRS is a good fit for the Core Domain but not for the domain of reporting. Especially when you want to apply various calculations based on different scenarios prior to report generation. Think BI.

请记住,CQRS 可能不应该应用于整个应用程序.一旦您的应用程序足够复杂,您就应该确定其限界上下文,并对每个限界上下文应用适当的架构模式,即使它们使用相同的数据源.

Please remember that CQRS probably shouldn't be applied across your whole application. Once your application is complex enough you should identify its Bounded Contexts and apply an appropriate architectural pattern to each individually, even if they're using the same data source.

这篇关于CQRS:查询端的业务逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆