ASP.NET Core 中的 ORM 之 Entity Framework (6)

在 Startup 中配置 MiniProfiler: 在 ConfigureServices 里面添加services.AddMiniProfiler().AddEntityFramework(), 在 Configure 里面添加app.UseMiniProfiler(); 并配置 Swagger 的 IndexStream.

public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); //Swagger services.AddSwaggerGen(options => { options.DescribeAllEnumsAsStrings(); options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "API Docs", Version = "v1", }); }); //Profiling services.AddMiniProfiler(options => options.RouteBasePath = "/profiler" ).AddEntityFramework(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); // profiling, url to see last profile check: :56775/profiler/results app.UseMiniProfiler(); } app.UseSwagger(); app.UseSwagger().UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1"); // index.html customizable downloadable here: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/src/Swashbuckle.AspNetCore.SwaggerUI/index.html // this custom html has miniprofiler integration c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("ORMDemo.EFWithRepository.SwaggerIndex.html"); }); app.UseMvc(); }

运行项目,MiniProfiler 监控页面应该已经出现在 Swagger UI 页面的左上角了。

仓储模式和工作单元模式

仓储模式(Repository)是用来解耦的(通过在数据访问层和业务逻辑层之间创建抽象层)。
但仓储只关注于单一聚合的持久化,而业务用例却常常会涉及多个聚合的更改,为了确保业务用例的一致型,我们需要引入工作单元来管理多个聚合。

工作单元模式(unit of work)的作用就是在业务用例的操作中跟踪对象的所有更改(增加、删除和更新),并将所有更改的对象保存在其维护的列表中。在业务用例的终点,通过事务,一次性提交所有更改,以确保数据的完整性和有效性。总而言之,UOW协调这些对象的持久化及并发问题。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgffdz.html