hmthur
hmthur

Reputation: 1032

Reusablity of controller in MVC

In the MVC pattern, the controller is the least reusable, compared to the other two aspects. Now let's say I have an app (say for Ordering Pizza), which is available both as a web app and a mobile app (say iPhone). So in that case, I think the model (or data) can be reused. The view might not be reusable.

But regarding the controller, is it possible to reuse anything? Let's say if I already have a working web app, can I reuse controller logic for the mobile app as well? Also, what is and where exactly does "business logic" reside in MVC?

Upvotes: 3

Views: 1623

Answers (1)

Abhijeet Kashnia
Abhijeet Kashnia

Reputation: 12920

The controller calls a service layer. The service layer uses the model to do business logic. Controller never contains business logic. It should only delegate work to the service layer. I consider the service layer as the part that the domain model exposes, you could say it is the "Model" in MVC.

That said, I don't think the MVC frameworks really care if the controller is reusable or not. The important part is the model, which should not change because the service layer code is reused. Besides, if we write our code correctly, the controller will be a very thin layer and reusability should not be a concern.

Can you reuse the controller logic from the web app for a mobile application? I think not, but you could use the service layer. I am sceptical if even the view can be used directly from web to mobile apps, the needs are so different.

I suggest you look at Domain driven design if you are interested in application design and learning how to organize business logic.

Upvotes: 6

Related Questions