design-patternsmodel-view-controllerbusiness-logicbusiness-rules

Business logic in MVC


I have 2 questions:

Q1. Where exactly does "business logic" lie in the MVC pattern? I am confused between Model and Controller.

Q2. Is "business logic" the same as "business rules"? If not, what is the difference?

It would be great if you could explain with a small example.


Solution

  • Business rules go in the model.

    Say you were displaying emails for a mailing list. The user clicks the "delete" button next to one of the emails, the controller notifies the model to delete entry N, then notifies the view the model has changed.

    Perhaps the admin's email should never be removed from the list. That's a business rule, that knowledge belongs in the model. The view may ultimately represent this rule somehow -- perhaps the model exposes an "IsDeletable" property which is a function of the business rule, so that the delete button in the view is disabled for certain entries - but the rule itself isn't contained in the view.

    The model is ultimately gatekeeper for your data. You should be able to test your business logic without touching the UI at all.