-3

I have an API .NET 5 proyect and I have another proyect (library) that is reference from the API.

I have this appsettings.json in the API project:

 {
  "ConfigurationSettings": {
    "ConnectionStrings": {
      "DefaultConnection": "connStringDefault",
      "Other": "connStringOther"
    }
  }
}

And this class in the library:

public class ConfigurationSettings
{
    public ConnectionStrings ConnectionStrings { get; set; }
}

public class ConnectionStrings
{
    public string DefaultConnection { get; set; }

    public string Other { get; set; }
}

I want to use this class to search in the appsettings.json, like this:

string ConnString = ConfigurationSettings.ConnectionStrings.DefaultConnection;

But I need this to be with dependency injection, to use like this:

 private readonly IEmpleadoRepository _empleadoRepository;
    private readonly IMapper _mapper;
    private readonly IConfigurationSettings _config;


    public GetEmpleadoByIdHandler(IEmpleadoRepository empleadoRepository, IMapper mapper, IConfigurationSettings config)
    {
        _empleadoRepository = empleadoRepository;
        _mapper = mapper;
        _config = config;
    }

I google it and I cannot find the correct steps.

2
  • Maybe you can try using IConfiguration that you already get for free? (Microsoft.Extensions.Configuration.Abstractions) And you can map your custom ConfigurationSettings section. Commented Nov 19, 2021 at 0:07
  • With this: (Microsoft.Extensions.Configuration.Abstractions) and this other (Microsoft.Extensions.Configuration.Binder), I resolve this problem. Commented Nov 19, 2021 at 1:10

2 Answers 2

-1

Want do you need is called an options pattern. Options pattern

To use this first of all you need to create a binding between your class and your config file. Add this code into your startup class.

services.AddOptions<ConfigurationSettings>();

After it, you can use this in DI.

private readonly IEmpleadoRepository _empleadoRepository;
private readonly IMapper _mapper;
private readonly ConfigurationSettings _config;


public GetEmpleadoByIdHandler(IEmpleadoRepository empleadoRepository, 
IMapper mapper, 
IOptions<ConfigurationSettings> options)
{
    _empleadoRepository = empleadoRepository;
    _mapper = mapper;
    _config = options.Value;
}
Sign up to request clarification or add additional context in comments.

Comments

-1

With this nuget:

(Microsoft.Extensions.Configuration.Abstractions)

and this other:

(Microsoft.Extensions.Configuration.Binder)

You can inject dependency like this:

    private readonly IConfiguration _config;


    public GetEmpleadoByIdHandler(IConfiguration config)
    {
        _config = config;
    }

then you can do this:

Configuration config = new Configuration();
            _config.Bind("ConfigurationSettings", config);

            string conn = config.ConnectionStrings.MongoDB;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.