简体   繁体   English

如何在 ASP.NET CORE 中删除多层的循环依赖

[英]How to remove circular dependency of multiple layers in ASP.NET CORE

I am having some issues with architecture in one of my applications on ASP.NET CORE .我在ASP.NET CORE上的一个应用程序中遇到了一些架构问题。 I have web app in Application folder and the business layer in Business and Context related things in Data .我在 Application 文件夹中有 Web 应用程序,在Data中有Business和 Context 相关事物的业务层。 And at last in Model I have models.最后在Model我有模型。

在此处输入图片说明

Now the problem is I use Data and Model in Business layer and then I use the Business in Application controllers .现在的问题是I use Data and Model in Business layer and then I use the Business in Application controllers But in some cases, I need to use the Data in Application to .但在某些情况下,我需要将Data in Application toData in Application to . That causes unwanted dependency architecture.这会导致不需要的依赖架构。

So what I want is the best way to use the library in here.所以我想要的是在这里使用图书馆的最佳方式。

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });
    services.AddDbContextPool<ApplicationDBContext>(options =>options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("amcConn")));
    services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDBContext>()
    .AddDefaultTokenProviders();

    services.AddSession();
    services.AddSingleton<IConfiguration>(Configuration);
    services.AddMvc(options=> {
        var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
        options.Filters.Add(new AuthorizeFilter(policy));
    }).AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver()).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

}

Can anyone suggest me how do I manage?谁能建议我如何管理?

What you describe is an indirect (transitive) dependency, not a circular one.您所描述的是间接(传递)依赖项,而不是循环依赖项。 And it is quite common for multiple layers depending on the core (Model) layers.取决于核心(模型)层,对于多层来说,这是很常见的。

Making Business depend on Data is not as clean but not a real problem or cycle either.让业务依赖于数据并不那么干净,但也不是真正的问题或循环。 If you want to solve that better, create an IStorage interface that is consumed by Business and Application and implemented by Data.如果您想更好地解决这个问题,请创建一个由业务和应用程序使用并由数据实现的 IStorage 接口。 IStorage self then belongs to the ring1 or ring2 layer. IStorage self 则属于ring1 或ring2 层。

It makes more sense when you picture those layers in the Onion architecture fashion.当您以 Onion 架构方式描绘这些层时,它会更有意义。 Outer rings depend on the inner rings.外圈取决于内圈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM