0

One of our services is slowly turning into a mega-service where we have 10+ injected services; almost like a facade but not quite.

Are there some proven patterns to handle this specifically in C# / .NET Core? I feel like if we aggregate some of the smaller services it wouldn't lead us to cleaner code, in fact I fear the opposite.

As most of the methods use many of the services if we moved groups of methods out of the service then all we would be doing is creating a slightly smaller mega-service elsewhere in the code.

Code example:

public class FooService
{
    private IServiceA _serviceA;
    private IServiceB _serviceB;

    public FooService(IServiceA serviceA, IServiceB serviceB)
    {
        _serviceA = serviceA;
        _serviceB = serviceB;
    }
}

public interface IServiceA
{

}

public interface IServiceB
{

}

I would also be interested to read any research around strategies for handling this type of refactoring or how to tackle it.

bendev88
  • 75
  • 6
  • 2
    This question is probably unsuited for SO, because it is very broad and which patterns to apply depends a lot on specific code. Further more, which pattern to apply can be seen as a matter of taste and opinion. – Steven May 21 '21 at 14:06
  • 2
    Does this answer your question? [How to deal with constructor over-injection in .NET](https://stackoverflow.com/questions/4603555/how-to-deal-with-constructor-over-injection-in-net) – devNull May 21 '21 at 14:09
  • 1
    But either way, here are some pointers that might give you some ideas. Take a look at [Facade Services](https://blog.ploeh.dk/2010/02/02/RefactoringtoAggregateServices/), [Composite design pattern](https://en.wikipedia.org/wiki/Composite_pattern), and message-driven designs such as [Domain Events](https://martinfowler.com/eaaDev/DomainEvent.html) (with event handlers), [command handlers](https://blogs.cuttingedge.it/steven/p/commands/), and [query handlers](https://blogs.cuttingedge.it/steven/p/queries/). Chapter 6 and 10 of [DIPP&P](https://mng.bz/BYNl) and 10 discuss most of the above. – Steven May 21 '21 at 14:10

0 Answers0