Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a service layer in an MVC architecture

How would one typically implement a service layer in an MVC architecture? Is it one object that serves all requests to underlying business objects? Or is more like an object that serves different service objects that in their turn interact with business objects?

So:

  1. Controller -> Service -> getUserById(), or:

  2. Controller -> ServiceManager -> getUserService() -> getUserById()

Also, if the latter is more appropriate, would you configure this ServiceManager object in a bootstrap? In other words, register the different services that you will be needing for your app to the service manager in a bootstrap?

If none of the above is appropriate, what would help me get a better understanding of how a service layer should be implemented?

Thank you in advance.

like image 943
Decent Dabbler Avatar asked Aug 29 '09 18:08

Decent Dabbler


People also ask

What is the service layer 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. For example, the product service layer in Listing 3 has a CreateProduct() method.

How can add service in ASP.NET MVC?

Right-click on the created ASP.NET MVC Application and click Add Service Reference, as shown below.

What role does service layer play in the domain facade implementation approach?

Defines an application's boundary with a layer of services that establishes a set of available operations and coordinates the application's response in each operation.

What is the role of service layer?

The service layer of an IMS architecture provides multimedia services to the overall IMS network. This layer contains network elements which connect to the Serving-CSCF (Call Session Control Function) using the IP multimedia Subsystem Service Control Interface (ISC). The ISC interface uses the SIP signalling protocol.


2 Answers

The way I read this question, there's really two things that should be answered:

A) I would prefer splitting "Service" into "CustomerService" and "OrderService", in other words grouped by domain concepts.

B) Secondly I'd use dependency injection to get the proper service directly where I need it, so I'm basically using alt 1. The added abstraction in alternative 2 provides no additional value for me, since the IoC container does the important part.

like image 130
krosenvold Avatar answered Sep 30 '22 00:09

krosenvold


Using a "facade" is one way to go:

"A facade is an object that provides a simplified interface to a larger body of code"

like image 21
Dexygen Avatar answered Sep 30 '22 00:09

Dexygen