zoukankan      html  css  js  c++  java
  • ABP集成Dapper

    9.2 ABP基础设施层 - 集成Dapper

    9.2.1 简介

    Dapper 是基于.NET的一种对象关系映射工具。Abp.Dapper简单的将Dapper集成到ABP。它作为第二个ORM可以与EF 6.x, EF Core 或者 Nhibernate 工作。

    9.2.2 安装

    在开始之前,你需要安装Abp.Dapper以及 EF 6.x, EF Core 或者 NHibernate 这3个当中的任意一个你想用的到项目中。

    9.2.3 注册Module

    首先你要在Module类上添加 DependsOn 特性,并且使用 AbpDapperModule 作为传入参数。这样就可以注册它到你的模块中了。

    1. [DependsOn(
    2. typeof(AbpEntityFrameworkCoreModule),
    3. typeof(AbpDapperModule)
    4. )]
    5. public class MyModule : AbpModule
    6. {
    7. public override void Initialize()
    8. {
    9. IocManager.RegisterAssemblyByConvention(typeof(SampleApplicationModule).GetAssembly());
    10. }
    11. }

    注意:依赖关系的先后顺序 AbpDapperModule 依赖应该在 EF Core依赖之后。

    9.2.4 实体与表的映射

    你可以配置映射。例如:实体 Person 与表 Persons 的映射,如下所示:

    1. public class PersonMapper : ClassMapper<Person>
    2. {
    3. public PersonMapper()
    4. {
    5. Table("Persons");
    6. Map(x => x.Roles).Ignore();
    7. AutoMap();
    8. }
    9. }

    你应该在模块类中配置包含Mapper类。例如:

    1. [DependsOn(
    2. typeof(AbpEntityFrameworkModule),
    3. typeof(AbpDapperModule)
    4. )]
    5. public class MyModule : AbpModule
    6. {
    7. public override void Initialize()
    8. {
    9. IocManager.RegisterAssemblyByConvention(typeof(SampleApplicationModule).GetAssembly());
    10. //这里会自动去扫描程序集中配置好的映射关系
    11. DapperExtensions.SetMappingAssemblies(new List<Assembly> { typeof(MyModule).GetAssembly() });
    12. }
    13. }

    9.2.5 使用

    在注册完 AbpDapperModule 后,你可以使用泛型 IDapperRepository 接口(而不是使用标准的IRepository)来注入dapper仓储。

    1. public class SomeApplicationService : ITransientDependency
    2. {
    3. private readonly IDapperRepository<Person> _personDapperRepository;
    4. private readonly IRepository<Person> _personRepository;
    5. public SomeApplicationService(
    6. IRepository<Person> personRepository,
    7. IDapperRepository<Person> personDapperRepository)
    8. {
    9. _personRepository = personRepository;
    10. _personDapperRepository = personDapperRepository;
    11. }
    12. public void DoSomeStuff()
    13. {
    14. var people = _personDapperRepository.Query("select * from Persons");
    15. }
    16. }

    这样你就可以在相同的事务下,同时使用基于EF的仓储和Dapper的仓储了。

  • 相关阅读:
    [Graph]Doubling Algorithm
    Luogu 3203 BZOJ 2002——弹飞绵羊
    BZOJ 1468——tree
    BZOJ 10628 Luogu 2633
    Mo's Algorithm
    bzoj1063: [Noi2008]道路设计
    bzoj1264: [AHOI2006]基因匹配Match
    bzoj1177: [Apio2009]Oil
    bzoj1260: [CQOI2007]涂色paint
    bzoj3674: 可持久化并查集加强版
  • 原文地址:https://www.cnblogs.com/springsnow/p/13890136.html
Copyright © 2011-2022 走看看