12

I would like to know how can I do to structure a web application using one folder per feature (Customers, Orders) instead of one folder per artifact type (Controllers, Views); this seems to be a much better way to organize large projects with lots of features, but I can't find any information about it.

I don't think that using Areas would be a solution because using one Area per-feature would require creating lots of nested folders.

I think that what I want to do should be possible by customizing a IViewEngine implementation, but I'm not sure about that.

Has anyone tried to do this?

gschuager
  • 1,919
  • 1
  • 16
  • 25

2 Answers2

4

You can create root Features folder, create one folder for each of your features and Shared folder within it. Then you can add all files (controllers, models, views, scripts) related to a single feature to it's folder. If multiple features use same file you can it to Shared folder. This is how project structure may look like:

- App
  - Features
    - Orders
      - OrdersController.cs
      - Create.cshtml
      - Create.js
      - CreateModel.cs
      - Edit.cshtml
      - Edit.js
      - EditModel.cs
      - EditViewModel.cs
      ...
    - Customers
      ...
    - Shared
      - _Layout.cshtml
      - Error.cshtml
    - _ViewStart.cshtml
    - Web.config
  - FeatureViewLocationRazorViewEngine.cs
  ...
  - Web.config

In order to use Razor with this folder structure you need to create new view engine class inherited from RazorViewEngine, set proper ViewLocationFormats, MasterLocationFormats, PartialViewLocationFormats and add instance of your view engine to ViewEngines.Engines collection. For sample implementation and it's usage check FEATURE FOLDERS IN ASP.NET MVC article by Tim G. Thomas.

If you want to use classes like Scripts in your views you also need to import their namespaces. One way to do this is to copy Web.config file from old Views folder to Features folder. For another options check How do I import a namespace in Razor View Page? question.

For more information check following articles:

Community
  • 1
  • 1
Leonid Vasilev
  • 11,910
  • 4
  • 36
  • 50
2

You can change the location of where views are stored, if you would like. An example, http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.asp.

ASP.NET MVC is easy to develop with because of the philosophy of convention over configuration. If you really want to change those conventions, you can; however, you will find yourself doing a lot more coding. For example, scaffolding will not work with your configuration.

Why not just create your logical separations inside the artifact folders? So, inside your Models folder have a folder for ViewModels and one for DataModels. Inside the DataModels folder, create folders for the different subsets of models (Customers, Orders, etc.). Just my 2 cents.

Zach Green
  • 3,421
  • 4
  • 29
  • 32
  • This is more or less the reason I don't like the original convention: http://www.paulstovell.com/horizontal-vertical-project-structure - I'm already using this kind of structure in other projects and it suits me well. I know that this can be done customizing the implementations of IViewEngine and IControllerFactory, here I'm looking for some hands-on experience about this change of conventions and why it is or it is not worth it. Note: I will not be using scaffolding – gschuager Feb 11 '12 at 12:53
  • I agree. Grouping by role, e.g. controllers/models/views really starts biting you once the projects grows beyond a few controllers. Feature based organization is so much advanced in organizing production projects. Highly recommend it with any project. – demisx May 17 '15 at 16:30