ABP框架系列之十六:(Dapper-Integration-Dapper集成)
2017-11-30 15:45
399 查看
Introduction
Dapper is an object-relational mapper (ORM) for .NET. Abp.Dapper package simply integrates Dapper to ASP.NET Boilerplate. It works as secondary ORM provider with EF 6.x, EF Core or NHibernate.Installation
Before you start, you need to install Abp.Dapper and one of EF Core, EF 6.x and NHibernate ORM nuget packages to project that you want to use it.Module Registration
First you need to add DependsOn attribute for AbpDapperModule on your module where you register it.[DependsOn( typeof(AbpEntityFrameworkCoreModule), typeof(AbpDapperModule) )] public class MyModule : AbpModule { public override void Initialize() { IocManager.RegisterAssemblyByConvention(typeof(SampleApplicationModule).GetAssembly()); } }
Note that AbpDapperModule dependency should be added later than EF Core dependency.
Entity to Table Mapping
You can configure mappings. For example, Person class maps to Persons table in the following example:public class PersonMapper : ClassMapper<Person> { public PersonMapper() { Table("Persons"); Map(x => x.Roles).Ignore(); AutoMap(); } }
You should set the assemblies contains mapper classes. Excample:
[DependsOn( typeof(AbpEntityFrameworkModule), typeof(AbpDapperModule) )] public class MyModule : AbpModule { public override void Initialize() { IocManager.RegisterAssemblyByConvention(typeof(SampleApplicationModule).GetAssembly()); DapperExtensions.SetMappingAssemblies(new List<Assembly> { typeof(MyModule).GetAssembly() }); } }
Usage
After registering AbpDapperModule, you can use Generic IDapperRepository interface (instead of standard IRepository) to inject dapper repositories.public class SomeApplicationService : ITransientDependency { private readonly IDapperRepository<Person> _personDapperRepository; private readonly IRepository<Person> _personRepository; public SomeApplicationService( IRepository<Person> personRepository, IDapperRepository<Person> personDapperRepository) { _personRepository = personRepository; _personDapperRepository = personDapperRepository; } public void DoSomeStuff() { var people = _personDapperRepository.Query("select * from Persons"); } }
You can use both EF repositories and Dapper repositories at the same time, in the same transaction.
相关文章推荐
- ABP框架系列之三十八:(NHibernate-Integration-NHibernate-集成)
- ABP框架系列之二十三:(EF-MySql-Integration-EF-MySql-集成)
- ABP框架系列之四十七:(SignalR-Integration-SignalR-集成)
- ABP框架系列之二十九:(Hangfire-Integration-延迟集成)
- ABP框架系列之三:(Entity Framework Integration-实体框架集成)
- ABP框架系列之四十三:(OData-Integration-OData集成)
- ABP框架系列之四十五:(Quartz-Integration-Quartz-集成)
- ABP框架系列之五十:(Swagger-UI-集成)
- ABP架构学习系列四:集成Dapper
- ABP框架系列之九:(Abp-Session-会话)
- ABP框架系列之四十六:(Setting-Management-设置管理)
- ABP入门系列——使用ABP集成的邮件系统发送邮件
- ABP(现代ASP.NET样板开发框架)系列之14、ABP领域层——领域事件(Domain events)
- ABP框架系列之二:(Entity Framework Core-实体核心框架)
- 基于DDD的现代ASP.NET开发框架--ABP系列之1、ABP总体介绍
- ABP 样板开发框架系列
- ABP(现代ASP.NET样板开发框架)系列之10、ABP领域层——实体
- ABP框架系列之三十:(Javascript-API-Javascript-API)
- ABP框架系列之三十九:(NLayer-Architecture-多层架构)
- ABP框架系列之四十八:(Specifications-规范)