slayer_b
slayer_b

Reputation: 551

How many model classed can be served by one controller

I am using Spring MVC

I have 2 classes for models and 2 views one for list of objects and another one for a single object

I wonder is it by MVC concept to serve both objects in a single controller Yes objects are similar but not the same I will need different methods to handle them anyway

The question is: Is it ok from the side of mvc to use one controller for both or not ?

Upvotes: 0

Views: 38

Answers (1)

Bogdan
Bogdan

Reputation: 24590

I wonder is it by MVC concept to serve both objects in a single controller [...] Is it ok from the side of mvc to use one controller for both or not?

MVC does not impose such rules. MVC is just about splitting your code into different pieces each with its own job. MVC is about separating concerns in three large parts: Models, Views, Controllers. But separation of concerns is not an MVC thing. It's a general principle, just like the single responsibility principle is too.

So it is for you to decide if the controller should handle both, or if you need two controllers. You should ask yourself, are the functionalities related between the list of objects and one object details? do parts of the code duplicate if you use two controllers? is one controller doing to much? are two controllers doing too little?, if one changes should the other one change too?, etc.

Read on the above two principles then decide how you think it's best to build it (with one or with two controllers).

Upvotes: 1

Related Questions