Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass multiple interface parameters in a web API Controller contructor if I am using Dependency Injection?

I’m using dependency Injection to call my objects, so is there a neater way of adding more parameters in the constructor without making the constuctor signature long just like my case below?

   public ObjectController(IParam1 param1, IParam2 param2, IParam3 param3, 
   IParam4 param4,IParam5 param6,IParam7 param8)
    {
        _param1= param1;
        _param2 = param2;
        _param3= param3;
        _param4= param4;
        _param5= param5;
        _param6= param6;
        _param7= param7;
        _param8= param8;
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As the others have said, your controller has too much responsibility. I would recommend you break your functionality into Services/Business Logic services.

public class UserService : IUserService {
    
    IParam _param;
    IParam2 _param2;
    
    public UserService(IParam param, IParam2 param2) {
        _param = param;
        _param2 = param2;
    }
}


public class LoginService : ILoginService {
    
    IParam _param;
    IParam2 _param2;
    
    public UserService(IParam param, IParam2 param2) {
        _param = param;
        _param2 = param2;
    }
}

Then you can inject it into your controller

public Controller(IUserService userService, ILoginService loginService) { ... }

And if it it still too much I would recommend spliting your controller as well.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading