ASP.NET Core DI in a class library?

拥有回忆 提交于 2020-07-08 11:09:06

问题


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

}

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/54774994/asp-net-core-di-in-a-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!