Constructor Injection in C#/Unity?

前端 未结 3 1169
醉梦人生
醉梦人生 2020-12-05 03:21

I\'m using C# with Microsoft\'s Unity framework. I\'m not quite sure how to solve this problem. It probably has something to do with my lack of understanding DI with Unity.<

3条回答
  •  一整个雨季
    2020-12-05 04:00

    One way to solve this would be to use an injection constructor with a named registration.

    // Register timmy this way  
    Person son = new Person("Timmy");  
    container.RegisterInstance("son", son);  
    
    // OR register timmy this way  
    container.RegisterType("son", new InjectionConstructor("Timmy"));  
    
    // Either way, register bus this way.  
    container.RegisterType(new InjectionConstructor(container.Resolve("son")));  
    
    // Repeat for Joe / Train
    

提交回复
热议问题