1

I got this code smells from Sonarqube: "Constructor has 8 parameters, which is greater than the 7 authorized"

The class is not big, just under 100 lines of codes, but 4 parameters need to pass to the base class. These parameters were injected from DI, so I can not create another model and put them all into it.

I just have one solution is to use IServiceProvider in the base class, so I can reduce a lot of parameters. But in my opinion, use ServiceProvider make the code less clean

public class BaseClass : AuthenticationHandler (Microsoft class)
{
   private readonly IE e;
   private readonly IF f;
   private readonly IG g;

   protected BaseClass(IA a, IB b, IC c, ID d, IE e, IF f, IG g) 
       : base(a, b, c, d)
   {
      this.e = e;
      this.f = f;
      this.g = g;
   }

   //some method use e,f,g
}

public class ChildClass : BaseClass
{
   private readonly IH h;

   // code smell here
   public ChildClass(IA a, IB b, IC c, ID d, IE e, IF f, IG g) 
        : base(a, b, c, d, e, f, g)
   {
      this.h = h;
   }

   //some method use h
}

Is there any solution can help me reduce the parameter in this case but still keep the code clean and readable?

Thanks for attention.

Thiện Sinh
  • 507
  • 1
  • 4
  • 19

0 Answers0