简体   繁体   中英

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 . I have web app in Application folder and the business layer in Business and Context related things in Data . And at last in Model I have models.

在此处输入图片说明

Now the problem is 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 . 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 self then belongs to the ring1 or ring2 layer.

It makes more sense when you picture those layers in the Onion architecture fashion. Outer rings depend on the inner rings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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