简体   繁体   English

具有多步骤过程的应用程序层不使用域层是否可以?

[英]Is It Okay for an Application Layer with a multi-step process to not use the domain layer

I am working on an N-Layered .NET application with the following layers: 我正在使用具有以下层的N层.NET应用程序:

  • Presentation 介绍
  • Application 应用
  • Domain
  • Infrastructure (Contains Persistence and common utility functions like email) 基础结构(包含持久性和常见实用程序功能,如电子邮件)

At some point in my application, a request is approved. 在我的申请中的某个时刻,请求被批准。 Once approved, there are 4 steps which must be performed as follows: 批准后,必须执行以下4个步骤:

  1. Assign a product tag code to the issued product in the Request. 将产品标签代码分配给请求中的已发行产品。
  2. Update the Request status from “Being Processed” to “Order Completed” 将请求状态从“正在处理”更新为“订单已完成”
  3. Send an email to the requester stating their product is ready for pickup 发送电子邮件给请求者,说明他们的产品已准备好提货
  4. Send an email to the requester's manager informing them of what their employee has been given and a copy of the request they approved for the product 向请求者的经理发送电子邮件,告知他们所获得的雇员以及他们批准该产品的请求的副本

The above steps must be part of an atomic operation meaning they must all be done or the action is cancelled. 以上步骤必须是原子操作的一部分,这意味着它们必须全部完成或取消操作。 I am thinking of having the Application Layer directing the Request Repository and Persistence Layer to carry out the tasks as follows: 我正在考虑让应用程序层指导请求存储库和持久层来执行以下任务:

  1. Application Layer requests for the Request Repository to perform steps 1 and 2 as a unit of work transaction 应用程序层请求请求存储库执行步骤1和2作为工作事务单元
  2. Application Layer requests for the Infrastructure layer to carry out steps 3 and 4. 应用程序层请求基础结构层执行步骤3和4。

I'm not exactly sure if the Application Layer is supposed to do this. 我不确定应用层是否应该这样做。 I think it is because the first 2 actions require contacting a repository which I have avoided doing in the Domain Layer. 我认为这是因为前两个操作需要联系存储库,而我避免在域层中这样做。 The last 2 steps involve the Infrastructure Layer which the domain layer may talk to via a dependency injected instance. 最后两个步骤涉及域层可以通过依赖项注入实例与之对话的基础结构层。 Following this logic, the Application Layer is not asking the Domain Layer to do anything. 按照这种逻辑,应用层不会要求域层执行任何操作。 Is this typically how it is for a multi-step process like this when you have an Application Layer? 当您具有应用程序层时,对于这样的多步骤过程,通常情况是这样吗? Or did I miss something here? 还是我想念这里的东西?

I am aware of a framework called Windows Workflow but I'm not sure if that would help in this case as this multi-step process does not involve human interaction along the processing steps where things could be waiting for a few days. 我知道一个叫做Windows Workflow的框架,但是我不确定在这种情况下是否会有所帮助,因为此多步骤过程不涉及可能需要等待几天的处理步骤所涉及的人机交互。 I also don't want to make the application overly complex if I don't have to. 如果不需要,我也不想使应用程序过于复杂。

Thanks in advance. 提前致谢。

Steps 1 and 2 in your question sound like domain logic to me. 对我来说,您问题中的第1步和第2步听起来像领域逻辑。 So no, it is not okay. 所以不,这不好。 These Two steps should be carried out in the domain. 这两个步骤应在域中执行。 You can do the following: 您可以执行以下操作:

  1. In the application service, load the relevant aggregate (probably the Request ). 在应用程序服务中,加载相关的聚合(可能是Request )。

  2. Either invoke the operation that does step 1 and 2 on the Request , or (if the operation does not really belong to the Request ) invoke a domain service that performs these two steps. 调用对Request执行第1步和第2步的操作,或者(如果该操作确实不属于Request )则调用执行这两个步骤的域服务。

  3. Once the domain operation is completed, the application service can save the modified Request back to the DB. 域操作完成后,应用程序服务可以将修改后的Request保存回数据库。

  4. If the above steps are successful, you can send now the emails. 如果上述步骤成功,则可以立即发送电子邮件。 You should have some kind of retry mechanism in place, so that the email gets sent eventually, even if there is a temporary problem. 您应该具有某种重试机制,以便即使有暂时性问题,也可以最终发送电子邮件。

If the changes in step two modify more than one aggregate, the process becomes slightly more complex. 如果第二步中的更改修改了一个以上的聚合,则过程将变得稍微复杂一些。 Sagas as mentioned in the comments are one solution to this problem. 评论中提到的Sagas是解决此问题的一种方法。 Still, the individual changes are domain operations, so model them accordingly. 尽管如此,单个更改仍是域操作,因此请对其进行建模。

As suggested by plalx in the comment, the sending of the emails can be offloaded to a domain event consumer if you have an eventing infrastructure in place. 正如plalx在评论中所建议的那样,如果您具有事件基础结构,则可以将电子邮件的发送卸载给域事件使用者。 This usually solves the retry mechanism for free. 这通常可以免费解决重试机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM