I am putting together an MVC 4 project that is likely to grow extensively over the next few years. With the luxury and challenge of starting with a clean slate, I am trying to figure out the best practice for routing and organizing my controllers and views.
The MvcCodeRouting NuGet package is compelling and I plan on using it. However, I am not sure whether it would also be wise to utilize MVC Areas. It seems like MVC Areas are superfluous if MvcCodeRouting is used, but I am not sure if the extra bit of code organization offered by Areas is still desirable.
To illustrate what I mean, let’s take this fictitious grading application. It has the following high level areas:
- Public portal
- Authenticated student portal
- Authenticated teacher portal
- Authenticated administrator portal
Let’s say each portal will end up with 100 different views (let’s be ambitious!). If that scale is realized, MVC Areas seem like a nice way to partition the code, with MvcCodeRouting used within each Area. Or perhaps we should just organize the code anyway we like and let MvcCodeRouting do the heavy lifting.
So the Areas plus MvcCodeRouting design would look something like this (folder structure):
- MvcWebProject
- Areas
- Admin
- Controllers
- Models
- Views
- Public
- Controllers
- Models
- Views
- Student
- Controllers
- Models
- Views
- Teacher
- Controllers
- Models
- Views
- Admin
- Content
- Controllers
- Scripts
- Views
- Areas
And the MvcCodeRouting design without Areas would look something like this (folder structure):
- MvcWebProject
- Content
- Controllers
- Admin
- Public
- Student
- Teacher
- Models
- Admin
- Public
- Student
- Teacher
- Scripts
- Views
- Admin
- Public
- Student
- Teacher
Implicit in these designs is that MvcCodeRouting would be used to further organize classes via namespaces.
Of these too designs, which is the better approach? Why? Is there another approach that is even better? Is sharing resources among the portals easier or more difficult in one design or the other?
Note that this is just the front-end to a whole solution of code, so the whole service/domain/database behind this has its own specific architecture. For the purpose of this question, I am just interested how these different application areas should be architected in MVC.
I wouldn’t use Areas just for the folder organization. Areas are completely unnecessary with MvcCodeRouting since you can use namespaces instead, and using both could be confusing.
Let me suggest other folder structures you can use with MvcCodeRouting:
Option #1
Option #2
On this one you can put ViewModels in the same directory/namespace as the controller that uses it.
The one thing that MvcCodeRouting doesn’t change is the location of the Views folder.