I’m learning MVC and OOP, but something has me confused:
What is the difference between business and application logic?
Take a look at the following scenario of a payment and invoicing system:
Customers are invoiced, and when they pay the invoice it is acknowledged via a payments on the screen. The user enters the amount they have received from the customer into the system.
Now, is application logic the following (in the controller):
- User can not enter negative numbers
- User must enter input in the style of integers only or #.# or #.## or
##.## etc.
Business logic (in the model):
- The amount entered can not exceed the amount on the invoice (ie
what’s due) - And/or the amount entered can not exceed the amount owed if part
payments have been made previously
Do I have the right idea?
Normally you should put all business logic, including that for formatting and validation into the model. So not entering a negative number is a focus for the model not the controller. Read up on Skinny Controller, Fat Model and maybe the presenter pattern using Draper. Ryan Bates has covered a fair amount of this stuff recently on Railscasts. Probably good to look into that too.