0

Hi I want to inject 2 database connection strings into the repository constructor. like

var myDb = Configuration.GetSection("ConnectionStrings:MyDb").Value;            
services.AddSingleton<ITokenRepo>(new TokenRepo(myDb));

I am passing one string here but I want to pass 2 different database strings. I can do by passing IConfiguration to the constructor but I want without that. my TokenRepo is like this

public class TokenRepo : ITokenRepo
{
    private readonly string _connectionA;
    private readonly string _connectionB;
    private readonly IErrorHandling _error;
    public TokenRepo(string connectionA, string connectionB)
    {
        _connectionA = connectionA;           
        _connectionA = connectionA;
        _error = new ErrorHandling();
    }
  "ConnectionStrings": {
"DatabaseA": "Server=sql123;Database=Dev;Persist Security Info=True;User ID=appUser;Password=123456;Application Name=ABC",
"DatabaseB": "Server=sql123;Database=DEV;Persist Security Info=True;User ID=appUser;Password=123456;Application Name=DEF"

},

3
  • 1
    why dont you parameterize TokenRepo constructor to pass both of them? there are multiple ways of handling this. what is your intention? Commented Sep 15, 2020 at 8:17
  • I want data from different databases in the TokenRepo. My token repo is like this. public class TokenRepo : ITokenRepo { private readonly string _connectionA; private readonly string _connectionB; private readonly IErrorHandling _error; public TokenRepo(string connectionA, string connectionB) { _connectionA = connectionA; _connectionA = connectionA; _error = new ErrorHandling(); } Commented Sep 15, 2020 at 12:45
  • Did mean you want to parse two db connection from one string of Configuration in the TokenRepo constructor? Post the content of ConnectionStrings:MyDb. Commented Sep 16, 2020 at 11:23

1 Answer 1

1

Is there a reason you don't want to do it like this:

var databaseA = Configuration.GetSection("ConnectionStrings:DatabaseA").Value;
var databaseB = Configuration.GetSection("ConnectionStrings:DatabaseB").Value;

services.AddSingleton<ITokenRepo>(new TokenRepo(databaseA, databaseB));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.