【发布时间】: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