Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structuring of Asp.net MVC controller folders for better cohesion

When building complex applications the controllers can start to get unwieldy and very large, which may mean that you split them out into separate controllers. This may not be suitable as it will be reflected in the users experience. Ie. They will see the controller name in the URI.

For example: The default project that ships with MVC has an AccountController, that has actions for login, logoff, register etc.. which seems to violate the Single Responsibility Principle.

So the question is how to resolve this problem and separate out the concerns? An initial response could be just to create separate controllers. Ie.

AccountLoginController

AccountRegisterController

But this would not be a great experience from a customers point of view as it would effect the URI when requesting the resource.

A solution could be to have separate folders for each controller which contains separate class files for the action, each one with a single responsibility like so.

Controllers (folder)
    Account  (folder)
        Register.cs
        Login.cs
        Logout.cs
    AnotherController (folder)
        Actionfile.cs
        Actionfile.cs

The above would separate out the functionality and be highly cohesive.

So, this is a long explanation, but my questions are..

  • Has anyone implemented this before?

  • If so how do you go about it?

  • What are your thoughts regarding this pattern?

like image 579
Matt Avatar asked Oct 01 '10 15:10

Matt


People also ask

What is the folder structure of ASP.NET MVC?

This blog has discussed ASP.NET MVC project folder structures like App_Start, App_Data, Content, Controllers, Models, Scripts, Views, and Global.

What is the use of App_Data folder in MVC?

The App_Data folder of MVC application is used to contain the application related data files like . mdf files, LocalDB, and XML files, etc. The most important point that you need to remember is that IIS is never going to serve files from this App_Data folder.

What must the controller be named in ASP.NET MVC and what folder does it have to be specifically saved under?

In ASP.NET MVC, every controller class name must end with a word "Controller". For example, the home page controller name must be HomeController , and for the student page, it must be the StudentController . Also, every controller class must be located in the Controller folder of the MVC folder structure.

Which is the best approach to assign a session in MVC *?

Which of following is the best approach to assign a session in MVC? Current. Session["LoginID"] =7; is the best approach to assign a session in MVC.


2 Answers

Did you look at Areas?

"Areas let you organize a large project into multiple smaller sections in order to manage the complexity of a large Web application. Each section (“area”) typically represents a separate section of a large Web site and is used to group related sets of controllers and views."

In a nutshell, an area is a folder in your project that has its own subfolders for controllers, models, and views.

like image 190
Oren Trutner Avatar answered Oct 14 '22 13:10

Oren Trutner


It depends on what you mean by single responsibility.

If you mean Authentication as a responsibility, then the out of the box controller is perfect just the way it is. Logging in, Logging out and Registering are all part of the same thing - Authentication. So it makes sense that their code is in the same controller.

If you take the single responsibility principle to the ridiculous extreme, you would never have more than single classes with a single function in a single file.

You have to find a balance between readability/maintainability vs separation of concerns. In this case, it is going too far.

Also, remember that MVC is all about Convention over Configuration, meaning that if you name your controllers and views according to the convention, then they will be discoverable and you will have fewer configuration and routing issues.

Having said that, if you are determined to have a non-conventional Controller Naming convention, so to speak, you can implement your own Controller discovery code which would allow you to use a different convention. From the article linked to above:

public class ControllerConvention : TypeRules, ITypeScanner {
  public void Process(Type type, PluginGraph graph) {
     if (CanBeCast(typeof (IController), type)) {
     string name = type.Name.Replace("Controller", "").ToLower();
      graph.AddType(typeof(IController), type, name);
    }
  }
}
like image 40
Daniel Dyson Avatar answered Oct 14 '22 14:10

Daniel Dyson