ABP架构学习系列四:集成Dapper

  之前,一直想集成Dapper到项目中,但是一直没成功,今天把abp升级到最新版,然后按教程来,就可以了,呵呵

    现在,基于上一篇的源码进行升级和集成dapper,将abp升级到3.8.2

   官方升级文档:https://github.com/ABPFrameWorkGroup/AbpDocument2Chinese/blob/master/Markdown/Abp/9.5ABP%E5%9F%BA%E7%A1%80%E8%AE%BE%E6%96%BD%E5%B1%82-%E9%9B%86%E6%88%90Dapper.md

一、注册Module

  首先,你要在Module类上添加 DependsOn 特性,并且使用 AbpDapperModule 作为传入参数。依赖关系的先后顺序 AbpDapperModule 依赖应该在 EF Core依赖之后。

  修改ZmBlog.Infrastructure的ZmBlogDataModule 如下:

namespace ZmBlog.EntityFramework
{
    [DependsOn(typeof(ZmBlogCoreModule), typeof(AbpEntityFrameworkModule), typeof(AbpDapperModule))]
    public class ZmBlogDataModule : AbpModule
    {
        public override void PreInitialize()
        {
            Database.SetInitializer(new CreateDatabaseIfNotExists<ZmBlogDbContext>());

            Configuration.DefaultNameOrConnectionString = "DefaultConnection";
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

            DapperExtensions.DapperExtensions.SetMappingAssemblies(new List<Assembly> { typeof(ZmBlogDataModule).GetAssembly() });
        }
    }
}

二、配置实体与表的映射

  和ef一样,需要配置映射关系

public class PersonMapper : ClassMapper<Category>
    {
        public PersonMapper()
        {
            Table("Categories");
            AutoMap();
        }
    }

PS: 如果同时使用ef,它的映射最好使用ToTable指定表名,不然ef会自动转换最后一个单词为复数

 

三、使用

  1.在应用层使用

     private readonly IRepository<Category, string> _categoryRepository; 
        private readonly IDapperRepository<Category, string> _categoryDapperRepository; 
        private readonly ICategoryDapperRepository _categoryDapperRepo; 
 

        public CategoryApp(IRepository<Category, string> categoryRepository, IDapperRepository<Category, string> categoryDapperRepository, ICategoryDapperRepository categoryDapperRepository2)
        {
            _categoryRepository = categoryRepository;
            _categoryDapperRepository = categoryDapperRepository;
            _categoryDapperRepo = categoryDapperRepository2;
        }

  2.DapperRepository的封装

  封装的仓储基类

namespace ZmBlog.EntityFramework.EntityFramework.DapperRepositories
{
    public abstract class DapperRepository<TEntity, TPrimaryKey> : DapperEfRepositoryBase<ZmBlogDbContext, TEntity, TPrimaryKey> where TEntity : class, IEntity<TPrimaryKey>
    {
        public DapperRepository(IActiveTransactionProvider activeTransactionProvider) : base(activeTransactionProvider)
        {
        }
    }

    public abstract class DapperRepository<TEntity> : DapperEfRepositoryBase<ZmBlogDbContext, TEntity, string> where TEntity : class, IEntity<string>
    {
        public DapperRepository(IActiveTransactionProvider activeTransactionProvider) : base(activeTransactionProvider)
        {
        }
    }
}

  定义仓储接口和实现

//领域层
public interface ICategoryDapperRepository : IDapperRepository<Category, string>
    {
    }
//基础设施层
public class CategoryDapperRepository : DapperRepository<Category, string>, ICategoryDapperRepository
    {
        public CategoryDapperRepository(IActiveTransactionProvider activeTransactionProvider) : base(activeTransactionProvider)
        {
        }
    }

 

posted @ 2018-08-26 17:27  心存善念  阅读(5930)  评论(2编辑  收藏  举报