请将此“模式”命名为这样我就可以研究和了解更多
最近一段时间,我听到有人支持这样一个事实:域模型不应允许通过后续的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道这种模式有一个特定的名称,但根据您的描述,有一个基本的实际原因:
编写
x.Firstname = "Travis"
不会让x
对象知道Firstname
值已更改。这使得很难实现仅在已更改的字段上使用UPDATE
的SaveCustomer
函数。当然,在支持将成员赋值视为函数调用的语言中(例如,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 thex
object know that theFirstname
value was changed. This makes it hard to implement aSaveCustomer
function that only usesUPDATE
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.