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

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

问题描述

继CQRS的概念(命令查询责任分离),我直接引用的DAL在我的MVC应用程序和做通过的ViewModels所有读取。
但是我的一个同事都问我你会怎么做时,任何业务逻辑已被应用做一读时。
对于如如果您需要在计算方案中的百分比值象下面这样:

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。更糟的是,如果税收的值存储在数据库中有人可能会直接做在数据库中存储的过程。所以我们这里有业务逻辑的潜在泄漏其他层。

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一个非常适合的核心结构域而不是报告的领域。尤其是当您想根据报告前一代不同的场景应用各种计算。认为BI。

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天全站免登陆