开发者

please name this "pattern" so I can research and learn more

开发者 https://www.devze.com 2023-01-30 16:20 出处:网络
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 upd开发者_Go百科ates s

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 upd开发者_Go百科ates 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消