Creating a service level for my MVC application?

From what I understand, MVC separates the class definitions (model) from the view (s) through the "glue", which is the controller. The controller must have a separate responsibility and therefore be verifiable. ViewModels are used to combine data from several objects and to "massage" the data from the controller for presentation.

It seems that business logic really has no place ... so I think a different level for services would be appropriate. I just don’t know where to place this layer or how to create services - should it be a class called “services” that contains a bunch of functions? I'm a little new to MVC, so any reading material, samples or general tips for beginners would be awesome.

+57
asp.net-mvc asp.net-mvc-3 asp.net-mvc-4
Feb 15 '13 at 3:33
source share
5 answers

I usually use the service level when developing an ASP.NET MVC application. It looks like the service level template that Martin Fowler talks about in Enterprise Application Architecture Templates. It encapsulates your business logic and makes the controllers pretty thin. Mostly, controllers use the service level to obtain domain models, which are then converted to view models. I also use the Unit of Work Design Pattern for transaction processing and the Repository Template to encapsulate the level of data access for easier unit testing and the ability to easily change ORMs. This figure shows the typical layers that I use in an MVC application.

MVC Architecture

The service level is marked as “Application or domain level” in this diagram because I find that people get confused when you use the term “Service level”. They tend to think that this is a web service. This is actually an assembly that can be used by your favorite web service technology, such as ASP.NET Web API or WCF, as well as a controller.

Regarding naming conventions, I usually use what describes the domain followed by the service. For example, if I have a service level that handles user membership, then I will have a class called MembershipService, which has all the methods that controllers and web services need to request and manage a membership domain. Please note that you can have several domains in one application, so you can have several levels of service. My point is that you do not need to have one monolithic service that will serve the entire application.

+96
Feb 15 '13 at 14:16
source share
— -

My advice is to create separate classes called "services". Put them in a class library project (or namespace) and make them independent in the MVC infrastructure infrastructure. I recommend using some dependency injection as well (best of all, constructor injection). Then your service classes might look like this:

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

Then you use these services on your controllers. Look here for a complete example.

According to the repositories - my advice is to not use them if you intend to use some modern ORMs (NHibernate, EntityFramework), since your business logic will be encapsulated at the service level and your database will already be encapsulated with the ORM framework.

+21
Feb 15 '13 at 9:55
source share

Take a look at the MSDN Best Practices article.

The source code of the application in the article can be found here .

+7
Feb 15 '13 at 9:43
source share

Quote from "Business logic should be in the service, not in the model"? :

In the MVP / MVC / MVVM / MV * architecture, services do not exist at all. Or, if so, this term is used to refer to any common object that can be entered into a controller or view model. Business logic in your model. If you want to create “service objects” for organizing complex operations, which are considered as implementation details. Unfortunately, many people implement MVC, but this is considered an Anemic Domain Model, because the model itself does nothing, it's just a bunch of properties for the user interface.

Some people mistakenly believe that using the 100-line controller method and turning it into a service somehow creates a better architecture. This is really not the case; all he does is add another, possibly unnecessary layer of indirection. Practically speaking, the controller is still doing its job; it just does this through a poorly named "helper" object. I highly recommend submitting a demo of the Jimmy Bogard Wicked Domain Model for a clear example of how to turn an anemic area model into a useful one. This requires careful study of the models that you publish and which operations are truly valid in the business context.

+6
Feb 26 '17 at 12:46 on
source share

It looks like you're after something like a repository template. You can read about it here:

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net- mvc-application

This answer may also help:

Best repository template for ASP.NET MVC

+3
Feb 15 '13 at 3:44
source share



All Articles