ほとんどの任意のアプリケーションでは、ロギング、メッセージ バス、構成など、利用可能なすべてのレイヤー間で対処する必要がある横断的な問題が多数あります。私が気付いたのは、一部のクラスでは、モジュールが IoC を使用して注入されている場合、コンストラクターを完全に爆破する傾向があるということです。
public class MyService : IService
{
public MyService(ILogger logger, IAppSettings settings, IEventBus eventBus...)
{
}
}
コンストラクターの過剰注入の通常のケースでは、クラス内の依存関係が少なくなるように、関係を密接に関連するビルディング ブロックに屈折させる傾向があります。しかし、これは横断的な概念では不可能です。
ロギング フレームワークの中で、静的ファクトリ/サービスは非常に人気があるようです。
// Application root
MyLoggerService.SetFactory(log4NetFactory);
// Somewhere
MyLoggerService.GetLogger("name") // returns Log4NetLogger created by Log4NetFactory.
私の質問は次のとおりです。このアプローチは、あらゆる種類の分野横断的なものに適していますか? コードが次のようになる場合の欠点は何ですか。
public class MyService : IService
{
private readonly IReallyNeedThat _dependency;
public MyService(IReallyNeedThat dependency)
{
_dependency = dependency;
}
private readonly ILogger _logger = LoggerService.GetLogger("MyService");
private readonly IEventBus _eventBus = EventBusService.GetEventBus();
private readonly IConfiguration _configuration = ConfigurationService.GetConfiguration(Level.Roaming)
private readonly IExceptionHandler _exceptionHandler = ExceptionPolicy.GetHandler();
private readonly ITracer _tracer = TraceManager.GetDebugTracer();
}