c# - Configure routes and dependency injection outside of MVC UI -


we want move our composition root out of our ui layer , project. means moving startup class out of ui layer. how can configure routes when startup class not in ui layer's project?

we have setup.

  • the dal depends on bll, because dal implements bll interfaces.
  • the ui depends on bll, because ui controllers call bll interfaces.
  • the ui depends on dal, because ui's startup contains composition root.

current dependency graph

+---------+ ui.xproj  |              +      |              |      |              |      |              |      |              v      |           bll.xproj |              ^      |              |      |              |      |              |      |              +      +---------> dal.xproj 

based on article , numerous examples in aspnet github account, we're wiring our dependencies within ui.startup class. article states:

register dependencies in application startup method, before doing else.

that's we're doing in ui.startup class. we'd do, though, move our startup class fourth project, can eliminate dependency of ui on dal.

desired dependency graph

      +--------------------> ui.xproj        |                         +            |                         |            |                         |            |                         |            +                         v      application.xproj +--------> bll.xproj       +                         ^            |                         |            |                         |            |                         |            |                         +            +--------------------> dal.xproj 

our current ui uses asp.net mvc , routing works, if magic, 2 simple steps:

  1. inside ui.startup, call services.addmvc() , app.usemvc().
  2. onto our controller add [route("api/[controller]")].

when run project within visual studio, can browse /api/my-controller/ , see expected action result. groovy.

in our desired setup, ui no longer have startup class, because call in application. application not responsible configuring dependencies, need configure routes.

how can configure routes point controllers within ui layer?

(graphs courtesy of asciiflow)

magic reigned supreme. worked , quite nice. had was:

  1. cut , paste startup.cs ui.xproj new application.xproj
  2. add dependency on ui new application.xproj.

voila, routes , dependency injection still work within startup.cs.

application startup.cs

public class startup {     public void configureservices(iservicecollection services)     {         services.addmvc();         this.configurepersistenceservices(services);     }      public void configure(iapplicationbuilder app, iserviceprovider serviceprovider)     {         app.usemvc();         this.configurepersistence(serviceprovider);         app.run(async context => { await context.response.writeasync("hello!"); });     }      private void configurepersistence(iserviceprovider serviceprovider)     {         var unitofwork = serviceprovider.getrequiredservice<iunitofwork>();         unitofwork.create(new todoitem("test"));         unitofwork.save();     }      private void configurepersistenceservices(iservicecollection services)     {         services             .addentityframework()             .addinmemorystore()             .adddbcontext<farmcontext>(options =>             {                 options.useinmemorystore(persist: true);             });         // todo: use per request not singleton!         services.addsingleton<iunitofwork, unitofwork>();     } } 

application project.json

  "dependencies": {     "microsoft.aspnet.server.iis": "1.0.0-beta6-*",     "microsoft.aspnet.server.weblistener": "1.0.0-beta6-*",     "microsoft.aspnet.mvc": "6.0.0-beta6-*",     "dal": "1.0.0-*",     "bll": "1.0.0-*",     "ui": "1.0.0-*",     "entityframework": "7.0.0-beta6-*",     "entityframework.sqlserver": "7.0.0-beta6-*",     "entityframework.inmemory": "7.0.0-beta6-*"   } 

ui project.json

  "dependencies": {     "microsoft.aspnet.mvc": "6.0.0-beta6-*",     "bll": "1.0.0-*"   } 

bll project.json

  "dependencies": {   } 

dal project.json

  "dependencies": {     "entityframework": "7.0.0-beta6-*",     "bll": "1.0.0-*"   } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -