Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Obj : IObj
- {
- public class Obj(string name)
- }
- I register the following objects like this :
- public void RegisterMyObj(string name)
- {
- // Keyed with the object name
- builder.Register<Obj>().Named<IObj>(name).WithParameter(name).SingleInstance();
- }
- public class ObjectsHolder : IObjectsHolder
- {
- public ObjectsHolder (List<IObj> objsToHold))
- }
- // I want to register my ObjectsHolder in the following way:
- for example, this is how I want to call it from my code:
- RegisterObjectsHolder(string Obj1Name, string Obj2Name)
- public void RegisterObjectsHolder(params string[] objectsNames)
- {
- builder.Register<IObjectsHolder>().WithParameters(// Here comes the magic code which I can't figure out.
- // I want objects holder to be registered with the Obj instances whose names were passed to this method,
- // is there a way to do this?)
- )
- }
- builder.RegisterType<Service1>().Keyed<IService>("key1");
- builder.RegisterType<Service2>().Keyed<IService>("key2");
- builder.RegisterType<Service3>().Keyed<IService>("key3");
- builder.RegisterType<Service4>().Keyed<IService>("key4");
- builder.RegisterType<Service5>().Keyed<IService>("key5");
- public Test(IIndex<string, IService> serviceDictionary)
- {
- var service1 = serviceDictionary["key1"];
- }
- enum ServicesA { A1, A2, A3 }
- enum ServicesB { B1, B2 }
- builder.RegisterType<Service1>().Keyed<IService>(ServicesA.A1);
- builder.RegisterType<Service2>().Keyed<IService>(ServicesA.A2);
- builder.RegisterType<Service3>().Keyed<IService>(ServicesA.A3);
- builder.RegisterType<Service4>().Keyed<IService>(ServicesB.B1);
- builder.RegisterType<Service5>().Keyed<IService>(ServicesB.B2);
- builder.RegisterType<Service1>().Keyed<IService>(ServicesA.A1).Keyed<IService>("key1");`
- var assembly = Assembly.GetExecutingAssembly();
- builder.RegisterAssemblyTypes(assembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerLifetimeScope();
- private IUserService UserService { get; set; }
- public UserController(IUserService userService)
- {
- UserService = userService;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement