请将此“模式”命名为这样我就可以研究和了解更多

发布于 2024-10-06 20:59:19 字数 478 浏览 2 评论 0原文

最近一段时间,我听到有人支持这样一个事实:域模型不应允许通过后续的 Save 调用通过属性更新域对象。但所有更新都应该通过显式方法完成。我如何理解所说内容的示例:

糟糕的代码(对我来说这似乎很正常):

var x = _repository.GetCustomerByID(5);
x.Firstname = "Travis";
x.Lastname = "Laborde";
_respository.SaveCustomer(x);

我相信这个人正在推销的代码看起来像:

var x = _repository.GetCustomerByID(5);
x.UpdateCustomerName("Travis", "Laborde");
_repository.SaveCustomer(x);

我想了解更多 - 这种模式有一个名称吗?我可以在 Bing 上用 Google 搜索它吗?

Some time recently, I heard someone espousing the fact that a domain model should not allow updating the domain objects via properties with a subsequent Save call. But rather all updates should be done through explicit methods. Example of how I understand what was said:

Bad Code (that seems pretty normal to me):

var x = _repository.GetCustomerByID(5);
x.Firstname = "Travis";
x.Lastname = "Laborde";
_respository.SaveCustomer(x);

The Code that I believe this person was pitching would look like:

var x = _repository.GetCustomerByID(5);
x.UpdateCustomerName("Travis", "Laborde");
_repository.SaveCustomer(x);

I'd like to learn more - is there a name to this pattern so that I can Google it on Bing?

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

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

发布评论

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

评论(1

⊕婉儿 2024-10-13 20:59:19

我不知道这种模式有一个特定的名称,但根据您的描述,有一个基本的实际原因:

编写 x.Firstname = "Travis" 不会让 x 对象知道 Firstname 值已更改。这使得很难实现仅在已更改的字段上使用 UPDATESaveCustomer 函数。

当然,在支持将成员赋值视为函数调用的语言中(例如,C# 对其属性所做的那样),这种模式变得不那么有趣。

I'm not aware of this pattern having a specific name, but from what you describe, there's a basic practical reason for this:

Writing x.Firstname = "Travis" does not let the x object know that the Firstname value was changed. This makes it hard to implement a SaveCustomer function that only uses UPDATE on the fields that were changed.

Of course, in a language that does support treating member assignment as a function call (like, say, C# does with its properties), this pattern becomes much less interesting.

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