1

I'm using dependency injection for all of my service classes. The majority of the services that I'm injecting dependencies into have several common dependencies that they all use, such as ILogService, IMessageService (for displaying messages to the user), IFileSystemService (abstracting the File class so that I can unit test things that rely on reading files), IDateTimeService (abstracting DateTime so that I can inject dates during unit testing), IConfigurationService (so that I can inject application configuration settings during unit testing), and so on. The result is that my service classes all end up with a bunch of constructor arguments, since most of them have a minimum of four or five common dependencies, plus whatever their specific dependencies are (IOrderService, ICustomerService, etc.)

I've been considering throwing all of my commonly needed dependencies into a facade like IEnvironmentService to cut down on the dependencies, but that seems like it would violate the single responsibility principle since that new service would have methods to handle logging, messaging, reading/writing files, and so on (plus just the fact that it would have so many methods seems bad).

What's the recommended way of handling this situation? I'm trying to prevent my constructors from having a ton of parameters, but also not violate single-responsibility.

Ben Rubin
  • 6,909
  • 7
  • 35
  • 82
  • If you inject an `ILogService` in most your components, you should ask yourself: [Do I Log too Much?](https://stackoverflow.com/questions/9892137/windsor-pulling-transient-objects-from-the-container). – Steven May 11 '16 at 14:34
  • Why do many classes require all of these dependencies? Does these classes contain a lot of responsibilities? – Yacoub Massad May 11 '16 at 16:19
  • A lot of my classes need to log, display messages/prompts, access program configuration, use DateTime, and have external dependencies such as file systems and web services. Even though most of my classes only have a few public methods, they still have a lot of these dependencies. – Ben Rubin May 11 '16 at 17:10
  • In terms of logging too much, my service classes do not catch or log exceptions. They let exceptions bubble up to the caller. I am logging when a particular action takes place within the service, so that if the program crashes while it's deployed, I can look at the log and see exactly where something went wrong. – Ben Rubin May 11 '16 at 17:15

0 Answers0