F
F
footballer2018-07-21 13:44:16
ASP.NET
footballer, 2018-07-21 13:44:16

ASP.NET: Is it possible to move controllers into a separate project when using IoC?

Created an ASP.NET WEB API application using the visual studio template, the studio generated a WebApi web project to launch, in which it created a default controller. Next, I created the DataAccess, BusinessLogic, and Domain projects. The Domain contains entities from the database and interfaces for DataAccess and BusinessLogic, respectively, DataAccess and BusinessLogic implement these interfaces and refer to the Domain. The WebApi project also refers to Domain, but does not refer to DataAccess and BusinessLogic, because WebApi knows nothing about implementations, but only knows about interfaces, and real DataAccess and BusinessLogic are substituted through the IoC container (for this, we also had to redefine the initialization of the controllers through the same IoC container, whoever worked with IoC will understand).
In previous projects, I only saw that the IoC container was initialized right inside the WebApi project, but because of this, I had to add references to DataAccess and BusinessLogic in WebApi. And then I thought, why should WebApi refer to DataAccess and BusinessLogic, if it should never work with them directly anyway, but only through interfaces from Domain. Here I thought that IoC initialization can be left in the WebApi web project generated by the studio, where the controller was originally located, and the controller itself can be moved to a separate project, for example, WebApiControllers. As a result, for the WebApiControllers project, it will be possible not to add references to DataAccess and BusinessLogic, but only to Domain, and there will be no unnecessary dependencies in it. Because
Is it possible to make such a scheme, or is it very bad? I have never seen on projects that controllers are taken out into a separate non-start project.
Or leave everything as it is and do not worry about the fact that, due to the initialization of IoC, you had to link the WebApi project to the DataAccess and BusinessLogic projects?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Silin, 2018-07-21
@byme

I usually do a separate IoC project in a similar situation, which knows about DataAccess and BusinessLogic. After that, I make an extension method in it for IServiceCollection in which I register everything, and already in WebApi I call it

M
MrDywar Pichugin, 2018-07-21
@Dywar

Read Macro Seaman - Dependency Injection.
IoC is the "dirtiest" place. Place it at the application start point, where it belongs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question