Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVCS - Model View Controller Service

I've been using MVC for a long time and heard about the "Service" layer (for example in Java web project) and I've been wondering if that is a real architectural pattern given I can't find a lot of information about it.

The idea of MVCS is to have a Service layer between the controller and the model, to encapsulate all the business logic that could be in the controller. That way, the controllers are just there to forward and control the execution. And you can call a Service in many controllers (for example, a website and a webservice), without duplicating code.

like image 417
Matthieu Napoli Avatar asked Apr 18 '11 11:04

Matthieu Napoli


People also ask

What is controller model and service?

The idea of MVCS is to have a Service layer between the controller and the model, to encapsulate all the business logic that could be in the controller. That way, the controllers are just there to forward and control the execution.

What is the job of the service in MVC `?

A service layer is an additional layer in an ASP.NET MVC application that mediates communication between a controller and repository layer. The service layer contains business logic. In particular, it contains validation logic.

How does Model-View-Controller work?

First, the browser sends a request to the Controller. Then, the Controller interacts with the Model to send and receive data. The Controller then interacts with the View to render the data. The View is only concerned about how to present the information and not the final presentation.

What is the difference between controller and service?

In our analogy, the controller is the manager, while the service is the worker. If you think about what the manager's role is, he/she typically: manages the incoming work requests. decides which worker should do the work.


2 Answers

I had been thinking of this pattern myself without seeing any reference to this any where else and searched Google and found your Question here :)

Even today there is not much any body talking about or posting about the

View-Controller Service Pattern.

enter image description here

Thought to let you know other are thinking the same and the image above is how I view how it should be.

Currently I am using it in a project I am working on now.

I have it in Modules with each layers in the image above with in it's own self contained Module.

enter image description here

The Services layer is the "connector" "middleman" "server side Controller" in that what the "client" side Controller does for the client, the "Service" does for the server.

In other words the Client side "Controller" only "talks" with the "Service" aka Server Side Controller.

Controller ---> Requests and Receive from the <----- Service Layer

The Service layer fetches or give information to the layers on the server side that needs it.

By itself the Service does not do anything but connect the server layers with what they need.

Here is a code sample:

enter image description here

like image 30
Kbdavis07 Avatar answered Sep 29 '22 07:09

Kbdavis07


The service layer can be interpreted a lot of ways, but it's usually where you have your core business processing logic, and sits below your MVC architecture, but above your data access architecture.

For example, you layer of a complete system may look like this:

  1. View Layer: Your MVC framework & code of choice
  2. Service Layer: Your Controller will call this layer's objects to get or update Models, or other requests.
  3. Data Access Objects: These are abstractions that your service layer will call to get/update the data it needs. This layer will generally either call a Database or some other system (eg: LDAP server, web service, or NoSql-type DB)

The service layer would then be responsible for:

  • Retrieving and creating your 'Model' from various data sources (or data access objects).
  • Updating values across various repositories/resources.
  • Performing application-specific logic and manipulations, etc.

The Model you use in your MVC may or may not come from your services. You may want to take the results your service gives you and manipulate them into a model that's more specific to your medium (eg: a web page).

like image 140
Clinton Avatar answered Sep 29 '22 08:09

Clinton