类库中的ASP.NET Core DI? [英] ASP.NET Core DI in a class library?

查看:74
本文介绍了类库中的ASP.NET Core DI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Core 2.1项目,该项目引用典型的.NET Core类库的数据访问层"项目.

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

Data Access Layger需要来自ASP.NET Core项目中appsettings.json的连接字符串.

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

在ASP.NET Core Startup.cs> ConfigureService中,我具有以下内容:

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

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

我知道我可以将IDatabaseConnectionString添加到ASP.NET中的控制器的构造函数中以获取容器.但是,如何在类库中获取它呢?我不想将其从控制器一直传递下去,而只是将IDatabaseConnectionString添加到类库中的类的构造函数中是行不通的.

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?

例如,在此类中填写IDatabasConnectionString:

For example filling in the IDatabasConnectionString in this class :

public class UserFactory : FactoryBase
{
    private readonly IDatabaseConnectionString _iDatabaseConnectionString;

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

}

推荐答案

我知道我可以将IDatabaseConnectionString添加到ASP.NET中的控制器的构造函数中以获取容器.

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.

仅将IDatabaseConnectionString添加到类库中的类的构造函数中是行不通的.

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.

例如:

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


顺便说一句,就像@YeldarKurmangaliyev所说的,如果您想将其设为只读,则您的DatabaseConnectionString应该是这样的:


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

这篇关于类库中的ASP.NET Core DI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆