【问题标题】:How to remove circular dependency of multiple layers in ASP.NET CORE如何去除 ASP.NET CORE 中多层的循环依赖
【发布时间】:2020-04-12 18:23:53
【问题描述】:

我在 ASP.NET CORE 上的一个应用程序中遇到了一些架构问题。我在应用程序文件夹中有 Web 应用程序,在 Business 中有业务层,在 Data 中有上下文相关的东西。最后在Model 我有模型。

现在问题是I use Data and Model in Business layer and then I use the Business in Application controllers。但在某些情况下,我需要使用Data in Application to。这会导致不需要的依赖架构。

所以我想要的是在这里使用库的最佳方式。

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

}

谁能建议我如何管理?

【问题讨论】:

  • 你的数据不应该在业务层。业务层应该只有模型或 POCO。数据应该具有直接与表映射的实体或类
  • @Gauravsa - 取决于你想要“多少架构”(可以忍受)。在大多数项目中,我看到实体类也是模型。我知道将它们分开是“更好的”(AutoMapper 万岁),但通常它是矫枉过正的。

标签: c# asp.net-core dependency-injection configuration


【解决方案1】:

您描述的是一种间接(传递)依赖,而不是循环依赖。根据核心(模型)层,多层是很常见的。

让业务依赖于数据并不那么干净,但也不是真正的问题或循环。如果您想更好地解决这个问题,请创建一个由业务和应用程序使用并由数据实现的 IStorage 接口。 IStorage self 然后属于 ring1 或 ring2 层。

当您以 Onion 架构方式描绘这些层时,它会更有意义。外 环取决于内环。

【讨论】:

    猜你喜欢
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 2019-04-30
    • 2018-03-03
    • 2013-12-10
    • 2018-02-11
    相关资源
    最近更新 更多