Having many methods in one VS controller having multiple controllers

I am developing an application and I am pondering something. So I thought I would ask people with experience to tell me what they think about it.

So, I have this controller (storage controller), which is huge and has many methods, Get and Post actions, etc. It works very well, but I'm still under development.

I am wondering what would be the best practice: to have this controller that contains so many methods and actions, or share methods into many controllers? Is it ideal to have one controller with almost all methods or many controllers?

And before you ask, yes, everything in my store controller is "connected to the store." But in my store I have items, packages, etc.

Edit

Thanks everyone! Following your advice, I split my huge controller Storeinto smaller controllers: one for items, one for packages, etc. It really makes the code more readable. In addition, a lot of comments will provide me on my way to the update, so thank you very much!

+4
source share
6 answers

It is definitely better to split all the actions of your controller into several logical packets - in order to move them to separate controllers. Thanks to this, everything becomes more readable and intuitive for you as well as for other developers who will work on the project.

Question: how to divide it?

In fact, there are several criteria that can influence such a decision, for example:

  • . , , , , CheckoutController, ProductController, UserAccountController ..
  • , - . Store, , , , , . , , -.

, CRUD, . , , .

, ASP.NET MVC CoC (Convention over Configuration), . .

+3

, , ,

, ,

, , , FAQ ..

HomeController, ProductContsController, BlogController FAQController

, , , , .

, , - SSL, (, shoppingcart ..) SSL

+2

, . , , , , . , , .

, , - , :

  • .. -MVC. Controller, Model View . , ItemsController Store, Store/Items. , # : , .

  • . MVC 5 , . , . , , , , "" URL-. URL-, . MVC 5 , Nuget AttributeRouting, ( ). , , MVC 5.

+2

, . : BaseStoreController Controller, . StorePackageController, StoreItemsController .. BaseStoreController.

, , .. Command Pattern Facade, .

+1

"Store" , . "Store" , , , , , , - "Store" , , "HelperClasses" . , , , , ( Store), View . " ". Store , -. - . , - , . , .

, , , , ViewModel , , , , , , "Store".

+1

.

public partial class StoreController: Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

in .cs .vb:

public partial class StoreController
{
    public ActionResult Show()
    {
        return View();
    }
}

: http://msdn.microsoft.com/en-us/library/wa80x488.aspx

+1

Source: https://habr.com/ru/post/1523348/


All Articles