Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. public class Obj : IObj
  2. {
  3. public class Obj(string name)
  4. }
  5.  
  6. I register the following objects like this :
  7.  
  8.  
  9. public void RegisterMyObj(string name)
  10. {
  11. // Keyed with the object name
  12. builder.Register<Obj>().Named<IObj>(name).WithParameter(name).SingleInstance();
  13. }
  14.  
  15. public class ObjectsHolder : IObjectsHolder
  16. {
  17. public ObjectsHolder (List<IObj> objsToHold))
  18. }
  19.  
  20. // I want to register my ObjectsHolder in the following way:
  21. for example, this is how I want to call it from my code:
  22. RegisterObjectsHolder(string Obj1Name, string Obj2Name)
  23.  
  24. public void RegisterObjectsHolder(params string[] objectsNames)
  25. {
  26. builder.Register<IObjectsHolder>().WithParameters(// Here comes the magic code which I can't figure out.
  27. // I want objects holder to be registered with the Obj instances whose names were passed to this method,
  28. // is there a way to do this?)
  29. )
  30. }
  31.  
  32. builder.RegisterType<Service1>().Keyed<IService>("key1");
  33. builder.RegisterType<Service2>().Keyed<IService>("key2");
  34. builder.RegisterType<Service3>().Keyed<IService>("key3");
  35. builder.RegisterType<Service4>().Keyed<IService>("key4");
  36. builder.RegisterType<Service5>().Keyed<IService>("key5");
  37.  
  38. public Test(IIndex<string, IService> serviceDictionary)
  39. {
  40. var service1 = serviceDictionary["key1"];
  41. }
  42.  
  43. enum ServicesA { A1, A2, A3 }
  44. enum ServicesB { B1, B2 }
  45.  
  46. builder.RegisterType<Service1>().Keyed<IService>(ServicesA.A1);
  47. builder.RegisterType<Service2>().Keyed<IService>(ServicesA.A2);
  48. builder.RegisterType<Service3>().Keyed<IService>(ServicesA.A3);
  49. builder.RegisterType<Service4>().Keyed<IService>(ServicesB.B1);
  50. builder.RegisterType<Service5>().Keyed<IService>(ServicesB.B2);
  51.  
  52. builder.RegisterType<Service1>().Keyed<IService>(ServicesA.A1).Keyed<IService>("key1");`
  53.  
  54. var assembly = Assembly.GetExecutingAssembly();
  55. builder.RegisterAssemblyTypes(assembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerLifetimeScope();
  56.  
  57. private IUserService UserService { get; set; }
  58. public UserController(IUserService userService)
  59. {
  60. UserService = userService;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement