Dependency injection with a static logger, static helper class

前端 未结 4 762
[愿得一人]
[愿得一人] 2020-11-30 04:10

I have a static class which calls a static Logger class,

e.g

static class DoesStuffStatic
{
  public static void DoStuff()
  {
    try
    {
      //         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 04:35

    You can't inject a static logger. You have to either change it to an instance logger (if you can), or wrap it in an instance logger (that will call the static). Also it is fairly hard to inject anything to a static class (because you don't control the static constructor in any way) - that's why I tend to pass all the objects I want to inject as parameters.

提交回复
热议问题