gpt4 book ai didi

c# - mvc核心中没有引用基础设施的配置服务

转载 作者:太空宇宙 更新时间:2023-11-03 12:18:26 25 4
gpt4 key购买 nike

在Mvc Core中使用Clean Architecture时,Layer with Name可以是这样的:

  1. 基础设施
  2. 核心
  3. 网络

此链接对此有完整的描述:

https://learn.microsoft.com/en-us/dotnet/standard/modern-web-apps-azure-architecture/common-web-application-architectures

原则说 Web 项目不应该有任何来自基础设施类库的引用。那么如何使用DI容器来解决问题呢?

Startup.cs 中的这段代码使用 Infrastructure 来配置一些东西:

public void ConfigureProductionServices(IServiceCollection services)
{
services.AddDbContext<CatalogContext>(c =>
{
try
{
c.UseSqlServer(Configuration.GetConnectionString("CatalogConnection"));
}
catch (Exception ex)
{
var message = ex.Message;
}
});

services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("CatalogConnection")));

ConfigureServices(services);
}

public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();

services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
.
.
_services = services;
}

最佳答案

你说:

Principles says that Web project shouldn't have any reference from Infrastructure class library. So how can use a DI container to resolve the problem?

但是article you linked说:

enter image description here

Note that the solid arrows represent compile-time dependencies, while the dashed arrow represents a runtime-only dependency. Using the clean architecture, the UI layer works with interfaces defined in the Application Core at compile time, and ideally should not have any knowledge of the implementation types defined in the Infrastructure layer. At runtime, however, these implementation types will be required for the app to execute, so they will need to be present and wired up to the Application Core interfaces via dependency injection.

本文描述的是应用程序的逻辑架构,而不是应用程序的物理架构。从逻辑上讲,用户界面对基础设施层一无所知,但在物理上,用户界面层中的应用程序启动时的 DI 代码(就在运行时应用程序的最开始)将应用程序组件组合在一起。这个概念可以illustrated and explained best here .

想法是我们可以独立彼此使用程序集,而无需拖延额外的依赖项。在实践中使单元和集成测试更容易、更可靠。

关于c# - mvc核心中没有引用基础设施的配置服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48601732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com