Banshee
Banshee

Reputation: 15837

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;
    }

}

Upvotes: 10

Views: 7194

Answers (2)

Camilo Terevinto
Camilo Terevinto

Reputation: 32062

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;
    }
}

Upvotes: 7

Karel Kral
Karel Kral

Reputation: 5486

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.

Upvotes: 6

Related Questions