开发者

Injecting a dependency into a static class

开发者 https://www.devze.com 2023-01-06 16:15 出处:网络
What configuration do I need to setup a static property dependency using Windsor container? I have the following class at the moment and I would like the Logger property to be injected.

What configuration do I need to setup a static property dependency using Windsor container?

I have the following class at the moment and I would like the Logger property to be injected.

static class StuffDooer 
{
   static ILogger Logger { get; set; }

   static StuffDooer() { Logger = NullLogger.Instance; }
}

Here's my configuration although, this facility seems to automatically find instance properties not problem, the static version is not being set.

<facility id="logging"
          type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"
          loggingApi="nlog"
          customLoggerFactory="Castle.Services.Logging.NLogIntegration.NLo开发者_运维技巧gFactory"
          configFile="Configuration/nlog.config" />


StuffDooer.Logger = container.Resolve<ILoggerFactory>().GetLogger(typeof(StuffDoer));

If you think it's ugly, that is the correct reaction. Statics and and IoC don't mix, same as alcohol and driving.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号