How do I use the Decorator Pattern with Unity without explicitly specifying every parameter in the InjectionConstructor

后端 未结 7 706
一整个雨季
一整个雨季 2020-12-01 03:09

This helpful article from David Haydn (EDIT: scam link removed, it could have been this article) shows how you can use the InjectionConstructor class to help y

相关标签:
7条回答
  • 2020-12-01 03:39

    The most succinct answer which works great is mentioned in another stackoverflow post by Mark Seeman. It is concise, and does not require me to use named registrations or suggest that I use Unity extensions. Consider an interface called ILogger with two implementations namely Log4NetLogger and a decorator implementation called DecoratorLogger. You can register the DecoratorLogger against the ILogger interface as follows:

    container.RegisterType<ILogger, DecoratorLogger>(
        new InjectionConstructor(
            new ResolvedParameter<Log4NetLogger>()));
    
    0 讨论(0)
提交回复
热议问题