When should I create a new controller class in Asp.net MVC (design question)?

Before asking a question, here is my understanding of the controller in the MVC pattern.

  • The controller is the application level (in DDD)
  • It controls the flow of applications.
  • He stays thin
  • He controls the unit of work (aka Transaction)

My question is, " when should I create a new controller class? ". I will give an example of DinnerController in NerdDinner .

  • Is this a controller for the Dinner Module ? (Is this a module? IMO, it's too small for a module)
  • If so, should I create a controller for each module ? And the controller will become thick?
  • If this is not the case, when should I create a new controller?

I personally prefer to create a Controller class for use case . For example, CreateDinnerControllelr, EditDinnerController, ListDinnerController, SearchDinnerController, etc. But there are a few drawbacks to IMO, such as

  • Sometimes it violates the DRY principle (it may be necessary to create the same ViewModel in two places, for example, Create and Edit may have a DinnerViewModel)
  • Need to explicitly define routing? (I'm still sorting through the route as / Dinner / Create, / Dinner / Editing / 1)

Thanks in advance.

+3
source share
2 answers

After working with MVC and ASP.net rails, I think that the controller should create each resource (in a REST style application).

0

, . Restful URL design, , , . .

http://example.com/examples/1/edit - maps to edit method on ExamplesController
http://example.com/examples/1/new - maps to new method on ExamplesController
http://example.com/examples - maps to index method on ExamplesController
http://example.com/examples/1/delete - maps to delete method on ExamplesController

http://example.com/users/1/edit - maps to edit method on UsersController
http://example.com/users/1/new - maps to new method on UsersController
http://example.com/users - maps to index method on UsersController
http://example.com/users/1/delete - maps to delete method on UsersController
+1

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


All Articles