Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core DI in a class library?

I have a ASP.NET Core 2.1 project that references a "Data Access Layer" project of typ .NET Core Class Library.

The Data Access Layger needs connection string from the appsettings.json in the ASP.NET Core project.

I have created a simple container like this :

public class DatabaseConnectionString : IDatabaseConnectionString
{
    private readonly string _connectionString;

    public DatabaseConnectionString(string connectionString)
    {
        _connectionString = connectionString;
    }

    public string ConnectionString {
        get { return _connectionString; }
        set {  }
    }
}

In the ASP.NET Core Startup.cs > ConfigureService I have this :

services.AddScoped<IDatabaseConnectionString>(p => new DatabaseConnectionString(Configuration.GetConnectionString("DefaultConnection")));

I know that I can add the IDatabaseConnectionString to a constructor of a controller in ASP.NET to get the container. But How do I get it while in the class library? I dont want to pass it all the way down from the controller and just adding the IDatabaseConnectionString to the constructor of a class in the class library do not work.

I probably need a service where I can ask to create a object of a class and let the service fill in the constructor interfaces with the correct objects?

For example filling in the IDatabasConnectionString in this class :

public class UserFactory : FactoryBase
{
    private readonly IDatabaseConnectionString _iDatabaseConnectionString;

    public UserFactory(IDatabaseConnectionString connectionString)
    {
        _iDatabaseConnectionString = connectionString;
    }

}
like image 474
Banshee Avatar asked Feb 19 '19 21:02

Banshee


People also ask

Can a class library use dependency injection?

Dependency Injection is configured at the Composition Root, basically the application entry point. If you do not have control over the application entry point you can not force anyone to use dependency injection with your class library.

What is .NET Core class library?

NET Core A class library defines the types and methods which are referred to by an application. If your class library targets . NET Standard 3.1, it can be called by any . NET implementation (including . NET Framework) that supports .

What is the difference between class library .NET standard and class library .NET Core )?

NET Core, you can build cross-platform console apps and ASP.NET Core Web applications and cloud services. . NET Standard: This is the set of fundamental APIs (commonly referred to as base class library or BCL) that all . NET implementations must implement.

How do I create a class library in .NET Core?

Right-click on the solution in Solution Explorer and select Add > New Project. On the Add a new project page, enter library in the search box. Choose C# or Visual Basic from the Language list, and then choose All platforms from the Platform list. Choose the Class Library template, and then choose Next.


2 Answers

I know that I can add the IDatabaseConnectionString to a constructor of a controller in ASP.NET to get the container.

No, that's not needed and it would be wrong.

just adding the IDatabaseConnectionString to the constructor of a class in the class library do not work.

It doesn't work because you need to create the service that will use the connection string and add it to the services container.

For example:

public class Repository: IRepository
{
    public Repository(IDatabaseConnectionString databaseConnectionString)
    {
        _databaseConnectionString = databaseConnectionString;
    }
}

public class ServiceThatRequiresDatabase : IServiceThatRequiresDatabase
{
    public ServiceThatRequiresDatabase(IRepository repository)
    {
        _repository = repository;
    }
}

// ...
services.AddScoped<IRepository, Repository>();
services.AddScoped<IServiceThatRequiresDatabase, ServiceThatRequiresDatabase>();


public class HomeController : Controller
{
    public HomeController(IServiceThatRequiresDatabase service)
    {
        _service = service;
    }
}

By the way, as @YeldarKurmangaliyev said, your DatabaseConnectionString should be like this if you want to make it read-only:

public class DatabaseConnectionString : IDatabaseConnectionString
{
    public string ConnectionString { get; }

    public DatabaseConnectionString(string connectionString)
    {
        ConnectionString = connectionString;
    }
}
like image 97
Camilo Terevinto Avatar answered Oct 17 '22 13:10

Camilo Terevinto


There is no difference between controller and class from a class library. You need to

  1. Define a class in a class library and inject IDatabaseConnectionString into it. Your UserFactory is the right way.

  2. register the UserFactory for DI

    serviceCollection.AddScoped<IUserFactory, UserFactory>();
    
  3. Resolve the UserFactory by the DI. For example, use the UserFactory as the constructor parameter in some controller. Everything is connected by DI automatically.

    public MyController(IUserFactory userFactory)
    {
        _userFactory = myUserFactory;
    }
    

Here is the good explanation for understanding Composition root.

like image 45
Karel Kral Avatar answered Oct 17 '22 13:10

Karel Kral