Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3 project structure

Tags:

asp.net-mvc

I am trying to find the best way to layout my MVC 3 project. When searching online I came across a suggestion that basically said right click on project and add area. What this did was create an area folder with same controller/view/model structure in the same project. This is not what I want. I want the flexibility of having separate projects. I will keep only the views in the main web project. Everything else in a separate project.

Towards that attempt I created a separate project for my controllers. Now I am stuck with pointing a controller action to a view. In all the online examples it was right click and add view. This being a class library project I don't have that flexibility. Where am I going wrong?

All examples that I have found including the ones I have gone through on Asp.net basically explain how to create study applications, which is only good for learning purposes. A large commercial application can't possibly have all the views/models/controllers in one project. Or is that the way it is supposed to go in MVC? I am not sure if doing everything with mouse clicks is also a good idea. In the webforms world also there were a lot of study-for-beginners applications that used mouse clicks to create basic CRUD applications, but in real commercial projects, we never used those methods.

What are your thoughts, guidance on this?

Thanks for your time...

like image 877
user20358 Avatar asked Jan 24 '12 14:01

user20358


2 Answers

MVC is based on a convention; the convention is you put all the views on /views, the models in /models and the controllers in /controllers. You can change the convention but it will not make your life easier.

From a conceptual point of view this does make sense. If you keep all domain logic and data access in separate projects all you are left with is the web related stuff, your controllers, view models and views. That's your MVC project.

Note that if you want to split off parts into separate projects you may find portable areas useful.

like image 73
Marnix van Valen Avatar answered Sep 19 '22 16:09

Marnix van Valen


I don't see why you can't use the built in generators as a base for your views and controllers? Nothing says that you have to leave them as generated. I personally thinks that's it's really nice to get a base generated for me (with mouse clicks).

The MVC project is just a UI layer. It's madness to put logic in it for large scale applications. It's therefore usually fine to have one project for all the UI. It actually makes it easier to get an overview of the UI.

That said, there are ways to get a plugin based solution where you can move the controllers (, models and views) to class libraries. But it's not easy.

  1. You need to create a virtual path provider (to find the views)
  2. Make all views embedded
  3. Modify the project file to get the "Add view" dialog etc.
  4. Use areas (makes it easier)
  5. Tell the BuildManager that your plugin DLL exists.

You also need to modify the virtual path provider to access the views from your plugin folders if you want to be able to modify the views during runtime in visual studio. Any change would otherwise require a rebuild of the plugin DLL.

Update

Video for MVC2 (MVC3 areas works the same): http://www.asp.net/mvc/videos/mvc-2/how-do-i/aspnet-mvc-2-areas

Do note that that video is for areas in the same project. Having areas in separate class libraries are more complex. The easiest solution is to use the portable areas as suggested by someone else.

like image 36
jgauffin Avatar answered Sep 21 '22 16:09

jgauffin