My team has developed a new service layer in our application. They created a bunch of services that implement their interfaces (E.g., ICustomerService
, IUserService
, etc). That's pretty good so far.
Here is where things get a bit strange: We have a class called "CoreService", which looks like this:
// ICoreService interface implements the interfaces of
// all services in the system 100+
public class CoreService : ICoreService
{
// I don't like these lazy instance variables. I think they are pointless
private readonly Lazy<CustomerService> _customerService;
private readonly Lazy<UserService> _userService;
public CoreService()
{
// These violate the Dependency inversion principle.
// It also news up its dependencies, which is bad.
_customerService = new CustomerService();
_userService = new UserService();
// And so forth
}
#region ICustomerService
public long? GetCustomerCount()
{
return _customerService.GetCustomerCount();
}
#endregion
#region IUserService
public User GetUser(int userId, int customerId)
{
return _userService.GetUser(userId, customerId);
}
#endregion
// ...
// And 100 other regions for all services
}
The team's reasoning is that controllers in the consuming application can easily instantiate CoreService
and use its services, and it wouldn't cause any performance problems since everything is "Lazy".
I tried to explain that this is a bad design because:
- We are violating the Dependency Inversion Principle by lazy instantiating every single dependency and their dependencies.
- As a result of #1, We are eliminating the testability of our services. We can no longer the mock dependency of our services and inject them for unit testing.
CoreService
just seems like a "God Object" anti-pattern to me.- We shouldn't even instantiate anything in our controllers. We should just inject the required dependencies of the controllers into it. (E.g., if the
CustomerController
requires five different services, just inject them via the constructor!)
Is my argument valid? Are there any other violations of best practices that I am missing here? Any input would be highly appreciated here.
Edit: I updated the title since this question is getting marked as a duplicate. This service is not necessarily a God object, it's actually a "Passthrough" or a Facade service. My apologies for the mistake.