如何以及何时应用业务规则?

发布于 2024-09-17 11:55:05 字数 568 浏览 7 评论 0原文

假设我有一个服务 StateService,它有一个方法 ChangeState

ChangeState(State toState, DomainObject object)

我有业务规则来检查目标状态在域对象当前“状态”中是否有效,如何在技术上检查这些规则而不首先在域对象上设置 toState ?首先设置新状态,运行验证,如果一个或多个规则违反取消设置状态,感觉是错误的。

我想出的一种解决方案是创建一些驱动验证的上下文对象,例如。 ChangeStateContext 包含 DomainObject 以及要设置的 State。

另一个相关问题是如何从 ChageState 调用中报告其进展情况?
我可以收集所有破坏并引发异常的验证规则,以及调用者可以相应捕获和处理的规则,或者我可以在 ChangeState 方法上添加返回类型,例如包含有关破坏规则的信息的 ValidationSummary等等。这些情况下的最佳实践是什么?

Suppose I have a service StateService which has a method ChangeState.

ChangeState(State toState, DomainObject object)

I have business rules that checks whether the destination state is valid or not in the domain objects current "state", how can I technically check those rules without first setting the toState on the domain object? It feels wrong to first set the new state, run validation and if one or more rule breaks unset the state.

One solution I came up with is to create some context object that drives the validation eg.
ChangeStateContext that contains the DomainObject along with the State that is to be set.

Another related question is how to report back from the ChageState call how it went?
I can collect all validation rules that broke and throw an exception with those rules that the caller can catch and handle accordingly or I can add a return type on the ChangeState-method like ValidationSummary that contains information about broken rules and such. What is best practices in these cases?

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

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

发布评论

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

评论(2

悲念泪 2024-09-24 11:55:05

当状态转换无效时,您不能从 ChangeState 方法中抛出异常吗?您可以抛出特定的异常,例如 StateTransformationExceptionValidationException,您可以在调用堆栈的更高位置捕获这些异常。您可以选择在这些异常类型中包含额外的属性,以便您可以非常准确地向用户传达出现的问题。

当您想要在单个用户操作后调用多个 ChangeState 时,您将需要一种恢复或回滚的方法。我通常做的是使用工作单元模式(由 LINQ to SQL 和实体框架提供给我)并更改该工作单元内的所有状态。当抛出异常时,我会丢弃整个工作单元及其所有更改。

Can't you throw an exception from the ChangeState method when the state transformation is invalid? You can throw specific exceptions such as StateTransformationException or ValidationException that you can catch higher on the call stack. You can optionally include extra properties to these exception types so that you can communicate very precisely what went wrong to the user.

When you want to call multiple ChangeStates after a single user action, you will need a way to revert or rollback. What I usually do is using the unit of work pattern (supplied to me by LINQ to SQL and Entity Framework) and change all state within this unit of work. When an exception is thrown I throw away the complete unit of work with all its changes.

悲喜皆因你 2024-09-24 11:55:05

DomainObject 类可以有一个 public bool CanChangeState(State toState) 实例方法,如果 toState 是一个则返回 True从主题 DomainObject 的当前状态进行的有效转换。可以在调用 StateService.ChangeState 之前调用此类方法。

当然,如果 StateService 负责状态更改验证,则应在 StateService 中添加 CanChangeState(State toState, DomainObject obj) 方法代码>.

要报告验证错误消息,请将 CanChangeState 的返回类型更改为负责报告验证错误的自定义类型。

The DomainObject class could have a public bool CanChangeState(State toState) instance method, returning True if toState is a valid transition from the current state of the subject DomainObject. Such method can be called prior to the StateService.ChangeState call.

Of course, if the StateService is responsible for the state change validation, then a CanChangeState(State toState, DomainObject obj) method should be added to the StateService.

To report back the validation error messages, change the return Type of the CanChangeState to a custom Type responsible for reporting validation errors.

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