MVC service level - service to a controller or other design?

I have an ASP.NET MVC project with a data layer (NHibernate and Repository), service level and Web layer (MVC).

At the moment, for each controller at the web level, I have a corresponding service level class of service. This works fine, although there is some overlapping of the interface and logic between the service classes (the GetThingById () method is needed for one class), and my main controller uses several services (or there will be a large amount of duplication in the dedicated home controller).

Is there a better way to structure this?

+4
source share
3 answers

Duplication usually means refactoring . Remove the duplicated logic in the service into it, break everything down (the principle of single responsibility) until there is no more duplication.

Then you either simply add several services to your controllers (as you already did for your home controller), or you can create classes that combine multiple access services (see also Delegation , Facade , Decorator and refactoring for aggregating services ) and enter them .

+3
source

There is no silver bullet, so you won’t get the “right” answer here. It depends on your specific scenario.

I personally try not to add as many layers, if it is very necessary, IMO, you need very strong motivation to have so many layers of abstraction, because usually you can create a suitable architecture with fewer components.

I would say that for an average application you don’t even need a formal service level, because it ends with an empty empty method, which simply calls another method in your repository.

If you need to use the service level, it may be more suitable to have one class of service for a domain domain or for each group of objects. Do not think that having a class of service for each controller makes a lot of sense. You can add some helper classes to cover the general logic of the controller, if necessary.

The problem that I see with your approach is that the service level should be part of the model and I don’t think that making it so dependent on the level of your controller is a good strategy.

+2
source

In "Microsoft ASP.NET MVC Programming," Dino Esposito recommends a template that he calls "IPODD." I have little experience with MVC, but this chapter is worth reading if you can keep it. I would say that it offers an experienced look at your question.

You can view the description of the book about the iPODD template here .

-1
source

Source: https://habr.com/ru/post/1491120/


All Articles