类库中的ASP.NET Core DI?

编程入门 行业动态 更新时间:2024-10-26 11:26:43
本文介绍了类库中的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?

本文发布于:2023-11-13 21:43:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类库   ASP   NET   Core   DI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!