Resolving wrapper classes in C# with the Unity IoC container

后端 未结 2 1139
离开以前
离开以前 2021-01-03 14:46

I want to use Unity resolve IService to two different implementations, to make use of a wrapper class, the equivalent of:

 IService service = new         


        
2条回答
  •  萌比男神i
    2021-01-03 15:20

    I have found out that this is trivial to do in the Castle Windsor IoC container. Just register the classes in the right order - first the wrapper, then the wrapped class, and the right thing will happen.

    e.g.

    container.Kernel.AddComponent();
    container.Kernel.AddComponent();
    

    Not only is this a lot less fuss than in Unity, it preserves one of the key advantages of IoC - that if the parameters to the DispatcherService constructor change, no other code needs to change.

    Let's hope that a future version of Unity steps up to make this scenario as simple as in Windsor.

提交回复
热议问题