1. Net Core 的DI和Abp的DI並存
Startup中 ConfigureServices返回值改為IServiceProvider
在ConfigureServices最后調用return services.AddAbp<AppModule>();
AppModule是一個自己實現的繼承AbpModule的類,用於程序集注入和其他配置初始化。
比如
[DependsOn( typeof(DomainModule), typeof(InfrastructureModule), typeof(AbpAspNetCoreModule))] public class AppModule : AbpModule { private readonly IConfigurationRoot appConfiguration; public AppModule(IHostingEnvironment env) { appConfiguration = AppConfigurations.Get(env.ContentRootPath, env.EnvironmentName); } public override void PreInitialize() { Configuration.DefaultNameOrConnectionString = appConfiguration["Database:ConnectionString"]; // 注意此處,后面有用 Configuration.UnitOfWork.Timeout = TimeSpan.FromSeconds(5); } public override void Initialize() { IocManager.RegisterAssemblyByConvention(Assembly.GetEntryAssembly()); } }
2. Abp.EntityFrameworkCore的問題
.NET Core下使用的是EfCoreRepositoryBase類來作為Repository的基類
使用DDD+Abp的時候發現Repository的Insert沒有自動持久化到數據庫,文檔中是說會在UOW完成的時候自動調用持久化方法。
EfCoreRepositoryBase的構造函數需要IDbContextProvider,其中有兩個實現:SimpleDbContextProvider不支持UOW,UnitOfWorkDbContextProvider才支持UOW。
IDbContextProvider需要提供DBContext來完成構造,DBContext又需要DBContextOption,
所以要注冊以下依賴:
DBContextOption,DBContext,Repository,UnitOfWorkDBContextProvider。
還有一點是UnitOfWorkDBContextProvider的調用鏈中會用到Configuration.DefaultNameOrConnectionString用於建立數據庫連接,這個設置要在AbpModule實現中完成,也就是第1點中代碼注釋提到的位置,默認是"Default",所以要么把ConnectionString的鍵名改成Default,要么在AbpModule實現中修改Configuration.DefaultNameOrConnectionString