Search code examples
c#design-patterns.net-coresonarqubecoding-style

How to reduce too many parameters from constructor


I used 9 parameters in the constructor in a command service class, but sonarqube shows an error for too many parameters. Can anyone suggest a solution or design pattern solve this issue?

public CustomerCommandService(A a, B b, C c, D d, E e, F f, G g, H h, I i){
//some code here
}

Solution

  • Have you considered using a Struct to contain your different Params and then passing this structure to your constructor

    public struct CustomerParams
    {
        A a;
        B b;
        ...
    }
    public CustomerCommandService(CustomerParams cp)
    {
    
    }