More controllers but less action or fewer controllers but more action

What do you define to create a new controller instead of adding additional actions to an existing controller? Where do you draw the line and WHY?

+6
source share
3 answers

What, of course, does not get into the picture is the number of actions¹ - at least in the sense that "oh, I have more than 50 actions in this controller, let him start another one."

The ² directive should be: controllers are a logical group for actions that work with the same type of object (a model of the same type may be a better definition). If it so happens that you have such a model rich in functionality that 30 separate actions can be performed on it, put them in the same controller.

On the other hand, coins: if you have simple models and you find that you write controllers in just a few steps, this should be a reason to feel good about application support, not a cause for concern.


Notes:

¹ Of course, a controller with so many actions is a red flag for possible code abuse, so the number should be taken into account - just not like some kind of hard rule.

² And this is really a guide - the goal here is to build a supported system, and not follow some religious scriptures.

+5
source

The main factor that should determine when to create a new controller is the logic / functionality that they perform. You will want to make sure that you are single issues :

public class ProfileController { } public class MainController { } public class AccountController { } public class ShoppingCartController { } 

Each of the previous controllers is used to coordinate communication between their respective services / domain models and their representations.

+5
source

As a general rule, I keep all the actions as simple as I can, if they get big or too big, then I see if I can create helper functions.

I have a controller on the DB object, if necessary.

So, for example, I would have a user controller, a basket controller, a goods controller, etc. etc.

I don’t think there are any specific rules. Its easy to maintain everything is logical. If this is logical for you, then, as a rule, everything that matters is if you are not on the team, then this should be logical for everyone.

Hope this helps.

+2
source

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


All Articles