Dependency Injection with .NETCore for DAL and Connection Strings

I am new to DI templates with .NETCore and am having trouble getting connection strings to my DAL.

I followed the recommendations given in this thread through the accepted answer and the following comments.

This is my base class.

public class BaseRepository : IRepository<IDataModel>
{
    private readonly IConfiguration config;

    public BaseRepository(IConfiguration config)
    {
        this.config = config;
    }

    public string GetSQLConnectionString()
    {
        return config["Data:DefaultConnetion:ConnectionString"];
    }

This is a fragment of a repository class that inherits the base class.

public class PrivacyLevelRepository : BaseRepository, IRepository<PrivacyLevelDM>
{
    public PrivacyLevelRepository(IConfiguration config) : base(config) { }
    public void Add(PrivacyLevelDM dataModel)
    {
         ...
    }
}

This is in my startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
        services.AddScoped<IRepository<IDataModel>>(c => new BaseRepository(Configuration));
    }

However, at my service level, instantiating the repository class still asks for (IConfiguration configuration) as a parameter.

 PrivacyLevelRepository repo = new PrivacyLevelRepository();

IConfiguration DAL, Controller > BLL > DAL. . DAL , . , ?

, - , DI/IoC, .

: Entity Framework, .

Thanx .

+6
2

. , ( ), .

:

public void ConfigureServices(IServiceCollection services)
{
    // register the `Data:DefaultConnection` configuration section as
    // a configuration for the `DatabaseOptions` type
    services.Configure<DatabaseOptions>(Configuration.GetSection("Data:DefaultConnection"));

    // register your database repository
    // note that we don’t use a custom factory where we create the object ourselves
    services.AddScoped<IRepository<IDataModel>, BaseRepository>();
}

DatabaseOptions :

public class DatabaseOptions
{
    public string ConnectionString { get; set; }
}

DatabaseOptions BaseRepository:

public class BaseRepository
{
    private readonly DatabaseOptions _options;

    public BaseRepository(IOptions<DatabaseOptions> databaseOptions)
    {
         _options = databaseOptions.Value;
    }
}

, BaseRepository, :

// register the repository as well in the `ConfigureServices` method
services.AddScoped<PrivacyLevelRepository>();
public class PrivacyLevelRepository : BaseRepository, IRepository<PrivacyLevelDM>
{
    public PrivacyLevelRepository(IOptions<DatabaseOptions> databaseOptions)
        : base(databaseOptions)
    { }
}

, . , , . , PrivacyLevelRepository?

PrivacyLevelRepository repo = new PrivacyLevelRepository();
returnValue = repo.GetAllByDomainID(DomainID).ToList();
return returnValue;

, . new . ( PrivacyLevelRepository) , .

, PrivacyLevelRepository , - . . , PrivacyLevelRepository IOptions<DatabaseOptions>. , , , , . , , .

, PrivacyLevelRepository , : , ; - . , , , :

public class MyController
{
    private readonly PrivacyLevelRepository _privacyLevelRepository;

    public MyController(PrivacyLevelRepository privacyLevelRepository)
    {
         // instead of *creating* a repository, we just expect to get one
         _privacyLevelRepository = privacyLevelRepository;
    }

    public IActionResult SomeRoute()
    {
         var domainId = "whatever";
         var data = _privacyLevelRepository.GetAllByDomainID(domainId).ToList();
         return View(data);
    }
}

, - - . , ASP.NET Core , , - . ConfigureServices, , , .

, .

+5

IConfiguration . IConfiguration , ( ). IConfiguration - Locator ( ). .

, , , , , .

- , , . API- , () . , Value, .NET Core , .

, , - . . - .

PrivacyLevelRepository, GetSQLConnectionString, . , , , , - . , "", .

UPDATE

string conStr = config["Data:DefaultConnetion:ConnectionString"];

services.AddScoped<IRepository<IDataModel>>(c => new PrivacyLevelRepository(conStr));

, , , IOptions<T>, , .

+1

Source: https://habr.com/ru/post/1016093/


All Articles