sterix24
sterix24

Reputation: 2400

Where does database interaction/business logic take place in MVC?

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

Answers (1)

dknaack
dknaack

Reputation: 60448

The Controller should call your Business Layer and then creates a simple POCO ViewModel to pass it to the View.

For example.:

  • Your Controller calls the BusinessLayer to get a User from Database.
  • He gets back a User Model WITH logic.
  • Then he creates a 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)

More Information

Upvotes: 5

Related Questions