views:

433

answers:

3

So I have a MVC app and in another project I have a normal collection of classes which handle the Business and Data logic for the application. I also have some logic in the Model of the MVC project itself. This logic handles ViewModels and the like, things which could not have been done in the n-tier project as they relate to the MVC project itself and need to be in the same project.

My questions are:

  • Should my model classes have knowledge of the n-tier business logic? Or should only the controller have this knowledge and send data back and forth between the n-tier application and the MVC model as needed?
  • If it's ok for my model to reference the n-tier application, then should my controller access n-tier via the model class?

Hope this makes sense, found it difficult to word correctly to get my point across.

+1  A: 

Think of your models as only containers for data between your controllers and your views. Essentially DTOs.

mxmissile
Well Models also contain logic do they not? They contain the classes which hold data between controller and view but also methods and classes to operate on that data? Therefore my n-tier project in someways is an extension of the model?
Damien
Read Eric's comment in his answer. His point is valid in this case.
mxmissile
+3  A: 

Generally speaking here - Your model classes should not have business logic knowledge. They should have only the information required to display the view to the user (use DTOs as suggested by mxmissile).

Your business logic would either be in your controller, or (better) in a separate service layer called by your controller. Having methods on a model that, for instance, bypass the controller and make calls directly to the database is almost always a bad practice.

The idea here is to make the views as dumb as possible. You send them a model, they pull out the data they need, format it appropriately, and display it. This makes it much easier to create new views of the same data later if you decide you want to change the presentation.

Eric Petroelje
I do have some logic in the model, all relating to preparing the data for display in a view..
Damien
So most of the business logic is in a separate project, should I access this via controller instead of model class then?
Damien
@Damien - if the logic in the model is all simply display related (e.g. sorting, formatting, etc.) then that should be fine. Although I would usually put that logic in the view itself rather than the model. That way if you have another view that has different formatting requirements, you don't have to create a new model with new formatting logic.
Eric Petroelje
I wouldn't put the sorting or formatting logic in the model or the view. I use HtmlHelpers in this case, it keeps the Model as just a DTO and keeps the View dumb.
Luke Smith
A: 

Your ViewData/ViewModel classes in your MVC application may contain instances of your Model classes (mine do). My controllers call my business services and are responsible for any translation between ViewData and Models.

If it's ok for my model to reference the n-tier application, then should my controller access n-tier via the model class?

I wouldn't go through the model to get to the application tiers, I would have the controller be that interfacing component. The controller calls to your application tiers which return instances of your Model from your data access components. You can then translate those instances into more consumable objects by using a ViewData/ViewModel object. You can do this in a controller, or use a separate assembler class.

blu