c# - Implementing Dependency injection across multiple solutions -
we have landscape 15 solutions, each solution contains many projects of own. solutions however, share mutual project called "model". since each solution however, wires own object graphs, causes registrations model project duplicated 15 times.
what's best way prevent duplication?
example:
solution-a
model
public class business relationship { public account() { var a=resolve<iemailer>(); } }
the add-on of above code in constructor forces me register dependencies in start-up projects of solutions if refer above class. solution need account
class not iemailer
still need inject iemailer
in solution.
registering in start-up project thing. mutual place called composition root , allows minimize number of references between projects, explained here.
another thing should prevent letting code (anything except composition root) depend on di library or abstraction on di library. instead of calling resolve
within constructors, allow dependency class has injected constructor of class. example:
public class business relationship { private readonly iemailer emailer; public account(iemailer emailer) { this.emailer = emailer; } }
this has many advantages on calling container within code.
do note though container meant build object graphs of services. if account
entity, resolving container not usual , advised thing do.
about multiple-solution problem you're having: since shared project you're using, might prevent referencing project, create independent project own release cycle. other projects can in case depend on assembly publish (for instance using own local nuget server).
but besides this, since reusable project, create sure assembly becomes di friendly library. if bootstrapping should done, , want prevent repeating across solutions, create separate bootstrapping-project. bootstrapping-project refers reusable library , references unity container. way library still stays independent used di library, while prevent duplicating bootstrapping logic throughout solutions.
c# dependency-injection unity-container
No comments:
Post a Comment