In most arbitrary applications, there are many cross-cut problems that need to be addressed among all available layers, for example. logging, message bus, configuration. I noticed that in some classes they tend to explode the constructor if modules are introduced using IoC.
public class MyService : IService { public MyService(ILogger logger, IAppSettings settings, IEventBus eventBus...) { } }
For ordinary cases of constructor over injection, I tend to reflect problems in building blocks that are closely related to each other, so I get fewer dependencies in the class. However, this is not possible with cross-sectional concepts.
Among registration frameworks, static factories / services look very popular, like
// Application root MyLoggerService.SetFactory(log4NetFactory); // Somewhere MyLoggerService.GetLogger("name") // returns Log4NetLogger created by Log4NetFactory.
My question is: is this approach good for all kinds of cross-stuff? What are the disadvantages if the code might look like this:
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(); }
c # design-patterns dependency-injection cross-cutting-concerns factory
xvdiff
source share