开发者

Simple OO design: where to put the Add responsibility?

My application domain has Users. It also has Organisations. Users can create Organisations.

My question is On which controller should I put the Cre开发者_高级运维ateOrganisations method? Does it belong on the UserController? Or does it belong on the OrganisationController? And how do I make that decision?

In any case, I plan to have an Add method on the Organisation model.

Any thoughts?


It belongs on the OrganizationController.

The Organization controller should be managing any creation/change to the organization model. The user should have no knowledge on how or what is involved in creating an organization. If you give them the ability to create organizations you are coupling logic. You make this decision because you have both a User and a Organization Controller.

If your user/organization don't really justify existence, aka they aren't really controlling much then merge them into one.

If both classes exist I vote for organization.


Like you said, a User can create an organization, so unless the organization doesn't tracks the owner (unlikely), you should have the CreateOrganization method delegated onto the user. A basic idea I can think of would be like:

  • User: Creates organizations by calling OrganizationRepository::Add( OwnerId )
  • Organizations: Can only be created given the id of the owner.

If (and only if) you don't need to track the creator of organizations, then the idea of placing an Add method directly on the organization without requiring anything else looks fine.

Hope I can help! David

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜