Configuring Unity to resolve a type that takes a decorated dependency that has a parameter that varies with the type into which it is injected

后端 未结 2 1830
清酒与你
清酒与你 2021-02-05 20:15

This is a fairly straight forward decorator pattern scenario, with the complication that the decorated type has a constructor parameter that is dependent on the type into which

2条回答
  •  醉梦人生
    2021-02-05 20:51

    Have you ever thought about basing your decorators on the Unity Interception functionality? Then it would be really easy to say "intercept the calls to IThing using this Interceptor" just once.

    container.AddNewExtension();
    container.RegisterType(new Interceptor(), new InterceptionBehavior());
    

    and then it would be "inject this IThing into this and that Depender"

    container.RegisterType(new InjectionConstructor(new ResolvedParameter("myNameForThing")));
    

提交回复
热议问题