多个值对象上的业务逻辑 - 将循环放在哪里?

发布于 2024-08-02 20:12:01 字数 371 浏览 2 评论 0原文

...以及如何最好地处理视图层的成功/失败反馈。

选项是:

doBusinessLogic(things)

或者

for (Thing thing : things) {
  doBusinessLogic(thing)
}

假设我们想要一个以一致的方式接收成功/错误反馈的视图层(即从对值对象的单个或多个操作),最好的方法是什么?

澄清:

处理视图层中业务逻辑调用引发的多种异常类型的代码量很大,并且还会导致维护问题(引入了表示层不知道的新异常)。对于业务逻辑调用来说,处理多个值对象上的错误并将它们“打包”以便视图以一致的方式处理似乎更好。

...and how best to handle success/failure feedback to the view layer.

Options are:

doBusinessLogic(things)

or

for (Thing thing : things) {
  doBusinessLogic(thing)
}

Assuming that we want a view layer that receives success/error feedback in a consistent way (i.e. from single or multiple operations on value objects), what is the best approach?

Clarification:

Handling multiple exception types thrown from a business logic call in the view layer is code heavy and also causes maintenance problems (new exceptions are introduced which the presentation layer doesn't know about). It seems better for the business logic call to handle errors on multiple value objects and 'package' them for the view to deal with in a consistent way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

依 靠 2024-08-09 20:12:01

与您的后一个建议类似的事情怎么样:

for (businessObject : businessObjects) { businessObject.doBusinessLogic() }

这个想法是将业务逻辑放入业务对象的方法中。您的视图层可以循环所有业务对象,告诉每个对象执行其业务。每个业务如何完成是在业务层的业务对象的逻辑中。您可以通过从 doBusinessLogic 方法返回值或针对出现的不良情况抛出异常来处理错误等。

如果您正在执行跨业务对象逻辑(即,对多个业务对象进行操作的逻辑),也许您可​​以构建一个businessObjectManager 类,该类可以具有接受一个或多个业务对象并对其进行操作的方法。因此,通过将管理器对象放在业务层中,您仍然将所有业务逻辑保留在业务层中。

How about something along the lines of your latter suggestion:

for (businessObject : businessObjects) { businessObject.doBusinessLogic() }

The idea is to put the business logic in a method of the business object. Your view layer can loop over all the business objects, telling each one to do their business. How each does its business is in the logic of the business object in the business layer. You can handle errors and such by returning values from the doBusinessLogic method or by throwing exceptions for nasty occurrences.

If you are doing cross-business-object logic (i.e., logic that operates on more than one single business object), perhaps you could build a businessObjectManager class that could have methods that take in one or more business objects and operates on them. Thus, by putting the manager object in the business layer, you still keep all of your business logic in the business layer.

难如初 2024-08-09 20:12:01

最终,我选择了前一种选择,并创建了一个轻量级通用“结果包”类,该类封装了对对象集合的操作的成功和失败。成功或失败取决于相应的对象标识符。

捆绑包填充在每个业务逻辑调用中并返回到视图层。捕获“对对象执行操作”业务逻辑循环中引发的异常,并将相应的失败添加到结果包(以对象 ID 为关键字)。

视图层有一个简单的辅助方法,可以将结果包中包含的反馈呈现给用户。

这非常有效。

我认为您采取的方法是最适合您的系统的。

Ultimately I went for the former option and created a lightweight generic "result bundle" class that encapsulates successes and failures of an action on a collection of objects. A success or failure is keyed to the corresponding object identifier.

A bundlesis populated inside each business logic call and returned to the view layer. Exceptions thrown in the 'perform action on object' business logic loop are caught and a corresponding failure added to the result bundle (keyed to the object ID).

The view layer has a simple helper method that presents the feedback contained in the result bundle to the user.

This works very well.

I think the approach you take with this is whatever fits best with your system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文