Reputation: 2400
I'm trying to learn about MVC and I'm confused about where to put business logic.
All the web applications I've built have been using N-tier architecture and because of this I'm used to putting business logic and database interaction in their own classes/respective 'layers', but how does this work in MVC?
From what I've gathered so far, it seems that this should all be stored in the Model? But I'm confused, because it seems it could just as easily be stored in the Controller? The Controller is responsible for returning the appropriate View.. so wouldn't it make sense that all the logic is stored here?
If anyone could just give me an idea of best practices in this situation I'd be very grateful.
Thanks!
Upvotes: 4
Views: 3715
Reputation: 60448
The Controller should call your Business Layer and then creates a simple POCO ViewModel
to pass it to the View
.
For example.:
UserViewModel
which has only properties and pass it to the View
Because one major thing about MVC is seperation of concern. You should create a ViewModel that has only the data your View needs, no logic.
A ViewModel is just a simple POCO class (Plain Old CLR Object, a class that only has properties, no logic)
Upvotes: 5