开发者

Method Cohesion

I have persistent objects that are saved to the DB (insert, update, delete).开发者_开发问答 Is it better to combine this logic in a single method - Save or have 3 separate methods for cohesion?


You should definitely not combine them into a single method because that would violate the Single Responsibility Principle (which I find from my own experience applies to method design as well as class design) and would increase cyclomatic complexity and hence, increase testing effort.

You will probably want to have a class dedicated to that purpose that has those 3 methods, for the same reason.

Regarding cohesion, 3 separate methods that are related (like in your case) and placed in a class, will keep a good cohesion, but please keep in mind the other class design principles as well.


This really kind of depends. Generally speaking I would keep the delete separate, and I tend to prefer a single method for updates/inserts.


My preference would probably be to have one public Save method and have that function call the Insert, Update, or Delete method as appropriate. Those methods can be protected or private, whatever fits the design.

[Edit]: I should add that I would probably not put that code in the model class, but move it into it's own repository class. I've found that keeping the model class very simple provides the most flexibility in your design.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜