tags:

views:

143

answers:

4

We have started using MVC framework and now we are bit confused where to write business-logic, in controllers or models?

For the project we are going to use WCF layer as DAL.

People have different views on Model & Controller, and think differently of writing business logic either in 'M' or 'C'.

What is the best practice?

I believe we will be accessing WCF (DAL) service in Model and applying all the business logic or filtering and then Controller accessing the data from Model.

+1  A: 

Most people keep their business logic in the model and this is considered best practice. Steve Sanderson who has written xVal endorses this method.

As I’ve discussed before, validation rules should go in your domain model, because the role of your model is to represent the workings of your business. It ensures that the rules will be enforced consistently, regardless of whether the UI coder remembers them or not.

Check out his post about xVal that talks about the problem you are discussing.

Joel Cunningham
+2  A: 

Try to keep -Business- logic and -Application flow- logic separate. Most people tend to mix those together as -business logic-

Ropstah
A: 

Do not think that model is supposed to be build from data access logic (wcf service in your case) only. I would recommend you to check out Domain Driven Design, it goes well with MVC. Controllers shouldn't contain any business logic. Controller action method should be ~20 or less lines. But that's just my opinion (made up from countless sources).

Arnis L.
+3  A: 

These are my rules:

Controller

Mainly Pageflow . Determines what View is displayed next.

Has access to Services ( ie productService.GetProduct(Model.ProductID) )

Model

I have 2 of them.

POCO-Classes - used by all layers (BLL,DAL)

ViewModel - used by View and Controller for stronly typed views.

View

Hopefully mainly easy HTML

I try to have the layout in a way, that it is possible to have different kind of people work at the project: The frontend guy and the backend guy. The backend guy will do the Service and Repository.

The Frontend guy will do Controller and Views. He also does ajax.

Malcolm Frexner