.NET Core + Abp踩坑和填坑記錄(1)


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM