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)
        {
        }
    }
复制代码

 

作者:心存善念
本文地址:https://www.cnblogs.com/xcsn/p/9537947.html
欢迎转载,请在明显位置给出出处及链接。

posted @   心存善念  阅读(5951)  评论(2编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
历史上的今天:
2017-08-26 Head First设计模式之单例模式
2013-08-26 如何利用ThoughtWorks.QRCode 生成二维码
点击右上角即可分享
微信分享提示