c# - Unity Application Block, How pass a parameter to Injection Factory? -


here have

  container.registertype<iusermanager, usermanagermock>();   container.registertype<iuser, usermock>(                 new injectionfactory(                     (c) => c.resolve<iusermanager>().getuser("john"))); 

and it

container.resolve<iprofile>(); 

i want pass name parameter factory able resolve user object name; this:

 container.resolve<iprofile>("jonh"); 

how can change type registration case?

while di frameworks have advanced features these types of registrations, rather change design of application solve such problem. keeps di configuration simple , makes code easier understand. creation of objects depend on context (thread, request, whatever) or have lifetime must managed explicitly, define factories. factories make these things more explicit.

in situation, want fetch profile user. typically have factory for. here's example of this:

// definition public interface iprofilefactory {     iprofile createprofileforuser(string username); }  // usage var profile = container.resolve<iprofilefactory>()     .createprofileforuser("john");  // registration container.registertype<iprofilefactory, profilefactory>();  // mock implementation public class profilefactory : iprofilefactory {     public iprofile createprofileforuser(string username)     {         iuser user = container.resolve<iusermanager>()             .getuser(username);          return new userprofile(user);     } } 

i hope helps.


Comments