We want to move our composition root out of our UI layer and into another project. That means moving the Startup
class out of the UI layer. How can we configure routes when the Startup
class is not in the UI layer's project?
We currently have this setup.
- The
DAL
depends on theBLL
, because theDAL
implements theBLL
interfaces. - The
UI
depends on theBLL
, because theUI
controllers call intoBLL
interfaces. - The
UI
depends on theDAL
, because the UI'sStartup
contains the composition root.
Current Dependency Graph
+---------+ UI.xproj
| +
| |
| |
| |
| v
| BLL.xproj
| ^
| |
| |
| |
| +
+---------> DAL.xproj
Based on this article and numerous examples in the aspnet GitHub account, we're wiring up our dependencies within the UI.Startup
class. The article states:
Register all dependencies in the application startup method, before doing anything else.
That's what we're doing in the UI.Startup
class. What we'd like to do, though, is to move our Startup
class into a fourth project, so that we can eliminate the dependency of the UI
on the DAL
.
Desired Dependency Graph
+--------------------> UI.xproj
| +
| |
| |
| |
+ v
Application.xproj +--------> BLL.xproj
+ ^
| |
| |
| |
| +
+--------------------> DAL.xproj
Our current UI
uses ASP.NET MVC and the routing just works, as if by magic, with two simple steps:
- Inside
UI.Startup
, we simply callservices.AddMvc()
andapp.UseMvc()
. - Onto our controller we add
[Route("api/[controller]")]
.
When we run the project within Visual Studio, we can browse to /api/my-controller/
and see the expected action result. Groovy.
In our desired setup, the UI
will no longer have a Startup
class, because that call will now be in the Application
. The Application
will not only be responsible for configuring the dependencies, it will also need to configure routes.
How can we configure the routes to point to the controllers within the UI
layer?