【问题标题】:Injecting a dependency into a static class将依赖项注入静态类
【发布时间】:2010-07-12 12:11:03
【问题描述】:

使用 Windsor 容器设置静态属性依赖需要什么配置?

我目前有以下课程,我希望注入 Logger 属性。

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

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

这是我的配置,这个工具似乎自动查找实例属性没有问题,静态版本没有设置。

<facility id="logging"
          type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"
          loggingApi="nlog"
          customLoggerFactory="Castle.Services.Logging.NLogIntegration.NLogFactory"
          configFile="Configuration/nlog.config" />

【问题讨论】:

    标签: castle-windsor


    【解决方案1】:
    StuffDooer.Logger = container.Resolve<ILoggerFactory>().GetLogger(typeof(StuffDoer));
    

    如果你觉得它丑,那是正确的反应。静电和 IoC 不会混合,就像酒精和驾驶一样。

    【讨论】:

    • LOL 好吧,我可能会重新考虑我的班级设计
    • 唯一的区别是,在大多数地方,驾驶时血液中的酒精含量都有上限(这里是 0.05%)。 IOC 和静力学没有容忍的方法。
    猜你喜欢
    • 1970-01-01
    • 2015-08-18
    • 2019-08-08
    • 2011-11-16
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    相关资源
    最近更新 更多