c# - Adjust Unity Dependency injection to use Lazy<T> / Func<T> -


i'm working on project uses unity dependancy injection load performance getting worse. trying adjust code utilise lazy<t> (or func<t>) i'm trying find way either register classes container using lazy<t> (or func<t>) or have sort of factory either adjust registered types or constructor not able seem find possible way this

at present have numerous service classes

public service1(iclasslogic<getreq, getres> getclass, iclasslogic<addreq, addres> addclass, iclasslogic<updatereq, updateres> updateclass, iclasslogic<deletereq, deleteres> deleteclass....){...} 

then have registrations similar to

container.registertype<iclasslogic<getreq, getres>, getclass>(); container.registertype<iclasslogic<addreq, addres>, addclass>(); container.registertype<iclasslogic<updatereq, updateres>, updateclass>(); container.registertype<iclasslogic<deletereq, deleteres>, deleteclass>(); ... 

ideally not have go change signatures

public service1(lazy<iclasslogic<getreq, getres>> getclass, lazy<iclasslogic<addreq, addres>> addclass... 

any pointers appreciated

first off tips concerning di in general:

  • you're right, not want change signature of constructor. you're ioc container (unity in case) should enable design interfaces , consumers like. otherwise isn't container.
  • when struggling starting di (and containers) advise wiring yourself. gives great insight in how works , provides flexibility. mark seemann has written lot stuff.
  • your service's dependencies seem awfully crowded. can't refactor less dependencies combining (maybe facade)?
  • making interfaces specific (not generic) makes things lot simpler. generics cause more harm good.

i've coded quick example compiles (i haven't tested it). i've used generic interface in line example used fake implementation , string types generic params (which aren't used):

if implementation of interface:

public class classlogic : iclasslogic<string, string> {     public void do()     {         // stuff     } } 

then implement provider creates implementation when needed (via given func) this:

public class classlogicprovider : iclasslogic<string, string> { private readonly func<iclasslogic<string, string>> innerlogicfactory;      public classlogicprovider(func<iclasslogic<string, string>> innerlogicfactory)     {         this.innerlogicfactory = innerlogicfactory;     }      public void do()     {         var classlogic = this.innerlogicfactory();         classlogic.do();     } } 

and wire this:

var container = new unitycontainer(); func<iclasslogic<string, string>> classlogicfunc = () =>         {             // create implementation on demand             return new classlogic();         }; container.registertype<iclasslogic<string, string>>(             new injectionfactory(c => {                 return new classlogicprovider(classlogicfunc);             })         ); 

this should give desired lazy creation when implementation needed.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -