Is there a way to reduce the number of parameters of this constructor? All the services must be used in this Controller. The majority of them are used more than 2 times(some of them >10), with the exception of IService7 that is used only in one method.

public class MyController : CustomBaseApiController
    private readonly IService1 service1;
    private readonly IService2 service2;
    private readonly IService3 service3;
    private readonly IService4 service4;
    private readonly IService5 service5;
    private readonly IService6 service6;
    private readonly IService7 service7;
    private readonly IService8 service8;

    public MyController(IService1 service1, IService2 service2, IService3 service3, IService4 service4, IService5 service5, IService6 service6, IService7 service7, IService8 service8)
      this.service1 = service1;
      this.service2 = service2;
      this.service3 = service3;
      this.service4 = service4;
      this.service5 = service5;
      this.service6 = service6;
      this.service7 = service7;
      this.service8 = service8;

All the methods are specific for this controller and no more refactoring into different Controllers should be done. How can I refactor this in order to have fewer parameters? This Controller is heavily tested (using Moq), so, if possible, I would want to keep it easier to mock the dependencies.

  • 1
    \$\begingroup\$ How can we know? There is litterally no context here! \$\endgroup\$ – 409_Conflict Sep 14 '17 at 11:48

Create a class that will expose the services as properties. That class should also have DI set up. Register that class with interface in the container. Inject the class into your own controller.


Not the answer you're looking for? Browse other questions tagged or ask your own question.