Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

There are a number of questions on Stack Overflow regard MVC best practices, but most of those seem to revolve around things like using Dependency Injection, or creating helper functions, or do's and don'ts of what to do in views and controllers.

My question is more about how to architect an MVC application. For example, we are encouraged to use DI with the Repository pattern to decouple data access from the controller, however very little is said on HOW to do that specifically for MVC. Where would we place the Repository classes, for instance? They don't seem to be model related specifically, since the model should likewise be relatively decoupled from the actual data access technologies.

A second question involves how to structure the layers or tiers. Most example applications (Nerd dinner, Music Store, etc..) all seem to use a single tier, 2 layer approach (not counting tests) that typically has controllers directly calling L2S or EF code.

If I want to create a multi-tier/layer application what are some of the best practices there in regards to MVC?


migration rejected from stackoverflow.com Nov 22 '13 at 9:09

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as too broad by gnat, MichaelT, Dan Pichelman, Robert Harvey, BЈовић Nov 22 '13 at 9:09

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs. If this question can be reworded to fit the rules in the help center, please edit the question.

SO is already filled with examples of the things you say you can't find. Where to put repositories, what should be in views, how to use DI, check user @stacey 's questions as she just went through all of this. All of the topics you've asked about are probably dupes. –  jfar Dec 27 '10 at 17:41

3 Answers 3

up vote 11 down vote accepted

its a great question, the examples I found to be extremely helpful are

I have posted an answer before (here), which is relevant for this question

in short

Using Open Source:

Using MS

The last 1, I believe is across multiple tiers, and Sharp Arhcitecture using WCF also can use multiple tiers.

a last note. to achive a 3 tier arch with a 2 tier application (3 logical tiers) you can use a reverse proxy


DI is accomplished in ASP MVC using a Controller Factory. This factory is used to resolve your controller dependencies.

Check this out:

Controller Factory

MvcContrib has some Controller Facotry implementations that you can use out of the box. I use their Castle Windsor implementation and it works well. Would also suggest checking out their TestHelper Class. It has some very cool functionality for mocking Controller HTTPContext, Sessions, etc.. MVCContrib

Personally I like to give my Models a Repository instance to work with. The model exposes an api to the repository(CRUD). The controller's dependency on a particular model is injected on creation (constructor) this is injected via the Controller Factory. This is my entry point to the object graph which my IoC container manages. Castle Windsor

Good Luck!


Where would we place the Repository classes, for instance?

They belong in the model; they're the in-application model.

How to do I structure the layers? If I want to create a multi-tier/layer application what are some of the best practices there in regards to MVC?

Tiers Represent physical separations of code. Layers represent logical separations. The layers (as they presently are) work well for MVC. Depending on the amount of business logic, it can either be placed in your Controller, or it can be placed in a separate assembly and can be used by the controller during the request cycle.

So you're suggesting that they should go in the UI Project of a multi-tier application? –  Erik Funkenbusch Dec 27 '10 at 17:19
@Mystere Man If it's not gigantic, then they should go in the project that hosts your MVC application. Specifically, the business logic would go into the controller and each action would have its own logic. MVC isn't just a UI only pattern; that's why I disagree with your assertion that it's a 'UI project'. It isn't. It's an MVC project that as a View section (there's your UI). –  George Stocker Dec 27 '10 at 17:22
Ok, perhaps I phrased that poorly. However, do you not agree that the view layer should not be manipulating the database? And if you put the Repository classes in the model, then the view can do so. –  Erik Funkenbusch Dec 27 '10 at 17:30
in a Small MVC application, the UI "Layer" is simply the folder that holds the views. In a larger application, it could be its own project. If it is its own project, then it would coordinate with the controller, and the controller could hook into the BusinessLayer as needed. No one outside of the controller would even need to know the business layer existed. I think you're automatically thinking these are in separate projects, but they don't have to be. –  George Stocker Dec 27 '10 at 18:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.