Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to structure ASP.NET MVC application using per-feature organization instead of per-artifact?

Tags:

asp.net-mvc

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?

like image 777
gschuager Avatar asked Feb 10 '12 11:02

gschuager


People also ask

Should you split your ASP NET MVC project into multiple projects?

It shouldn't! That's why it's designed in a modular way. In most web applications out there, we version and deploy all these assemblies (Web, BLL and DAL) together. So, separating a project into 3 projects does not add any values.

Where should enums live in MVC project structure?

Unless there is a better place to put them, I stick them in the Model folder. If you have a lot of Enums though, you might want to do the folder idea that you were doing.

Why. net Core is better than MVC?

The primary difference between ASP.NET MVC and ASP.NET Core is their cross-platform approach. ASP.NET Core can be used on Windows, Mac, or Linux, whereas ASP.NET MVC can only be used for applications on Windows.

What is @model in asp net core?

Hence, basically models are business domain-specific containers. It is used to interact with database. It can also be used to manipulate the data to implement the business logic. Let's take a look at a simple example of Model by creating a new ASP.Net MVC project. Step 1 − Open the Visual Studio.


2 Answers

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:

  • FEATURE FOLDERS IN ASP.NET MVC by Tim G. Thomas
  • FEATURE FOLDERS AND JAVASCRIPT by Tim G. Thomas
  • Put your controllers on a diet: GETs and queries by Jimmy Bogard
  • How we do MVC by Jimmy Bogard
like image 126
Leonid Vasilev Avatar answered Oct 22 '22 00:10

Leonid Vasilev


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.

like image 2
Zach Green Avatar answered Oct 22 '22 01:10

Zach Green