views:

226

answers:

3

I'm a newbie. I want to ask about the MVC model for separation of concerns. I have read up a few MVC tutorials but I don't yet have a full understanding of the roles of each of the Model, View and Controller.

For instance say I am writing an application for a user to monitor a portfolio. I would like the landing page to display lists of investments based of different criteria, for instance one may list investments based on amount invested, another may order it based on investment performance.

My question is, in accordance with the design pattern where should I write logic for generating the lists; in the Model, View or Controller?

Also any asp.net MVC examples demonstrating seperation of concerns is much appreciated.

Thanks in advance guys.

+3  A: 

At the risk of repeating myself, I'll point you to the answer I gave in this thread. The entire thread is probably worth your time, as are dozens of others on Stack Overflow.

To break it down simply:

Controllers - control application flow and makes decisions about data.

Models - perform business logic.

Views - produce output.

For your particular situation, you will want to produce your lists in the View layer. Use templates to create your list structure, and fill them with data fetched from the Model layer.

I'm not an asp.net programmer, so I can't give you a reliable example, but have a hunt around for other SO threads.

zombat
+1  A: 

Hi,

MVC pattern "requires" you to insert all your "business logic" in the Models. Models are used to access database and fetch data and mold it in a way that you just have to use a Controller to assign it into a View.

An graphical example : http://www.bhartisoftland.com/technologies-skill-sets/gifs/mvc-php.png

Needless to say perhaps, that you can bypass the use of models and write all your logic in the Controllers, but that would result in a very extensive and probably redundant amount of code. Controllers are used so you can call Models and Views, and exchange information from one to another with just a few lines of code.

yoda
+1  A: 

Nice question, this is subjective and there are many solutions, it comes down to the context I think and the preferences of the individual.

With ASP.Net implementation of MVC alot of people talk about the Model being more of a ViewModel than a Model as in some other frameworks (somewhat of a DTO). This in mind and looking at the Controller as just a coordinator of the flow of the application, it would not be wrong to generate the lists in an additional layer accessed via a service of some type. You would make a request to that service for a set of ViewModels which meet a specified set of criteria and let that extra layer worry about the way in which those lists are generated from that set of criteria. This way all the controller needs to know about is passing some criteria to the service and providing the view with a set of models (viewmodels) to display, the view is free of making any decisions about what to do with the data it has been provided, and the models are nice and lightweight.

Hope this explanation makes sense, and I'm open to criticism if people don't agree...

Simon Fox