Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Service Layer for my MVC application?

From what I understand, MVC separates the class definitions (model) from the presentation (view) via the "glue" that is the controller. The controller should have a single responsibility and therefore be testable. ViewModels are used to bring together data from multiple entities and to "massage" the data from the controller for the view.

It seems like business logic doesn't really have a place... so I'm thinking another layer for services would be suitable. I'm just not sure where to place this layer, or how to build the services - should it be a class called "services" that contains a bunch of functions? I'm a bit new to MVC, so any reading material, samples, or general newcomer kind of tips would be awesome.

like image 857
user2062383 Avatar asked Feb 15 '13 03:02

user2062383


People also ask

What is a 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. Now, after clicking on the preceding Service reference option, it will show the Window given below. Now, click on advanced button.

What does service layer do?

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.


2 Answers

I usually use a Service Layer when developing ASP.NET MVC application. It is similar to the Service Layer Pattern that Martin Fowler discusses in Patterns of Enterprise Application Architecture. It encapsulates your business logic and make the controllers pretty thin. Basically the controllers use the service layer to get the domain models that are then transformed into view models. I also use the Unit of Work Design Pattern to handle transactions and the Repository Design Pattern to encapsulate the data access layer for easier unit testing and being able to easily swap out ORM's. This figure shows the typical layers that I use in an MVC application.

MVC Architecture

The service layer is labeled as the "Application or Domain Layer" in this diagram because I find people get confused when you use the term "Service Layer". They tend to think that this is a web service. It is actually an assembly which can be used by your favorite web service technology, such as ASP.NET Web API or WCF, as well as a controller.

As for naming conventions I usually use something that describes the domain followed by service. For example, If I have a service layer that handles user membership then I would have a class called MembershipService that has all of the methods needed by controllers and and web services to query and manipulate the membership domain. Note you may have several domains in the same application so you can have multiple service layers. My point being here that you do not have to have one monolithic service that takes care of the whole application.

like image 144
Kevin Junghans Avatar answered Sep 23 '22 06:09

Kevin Junghans


My advice is to create a separate classes called "services". Put them in different class library (or namespace) project and make them independent on MVC framework infrastructure. I recommend to use also some kind of dependency injection (the best is constructor injection). Then your service classes may look like:

 public class MyService : IMyService  {      IFirstDependency _firstService;      ISecondDependency _secondService;       public MyService(IFirstDependency firstService, ISecondDependency secondService)      {      }       public Result DoStuf(InputDTO)      {          // some important logic               }  } 

Then you consume these service from your controllers. Look here for complete example.

According Repositories - my advice is to do not use them if you are going to use some modern ORM (NHibernate, EntityFramework), because your business logic will be encapsulated in the Service Layer and your database will be already encapsulated with the ORM framework.

like image 29
Marian Ban Avatar answered Sep 25 '22 06:09

Marian Ban