abp+dapper+mysql_ABP框架系列之十六:(Dapper-Integration-Dapper集成)

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

{

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 { 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 _personDapperRepository;

private readonly IRepository _personRepository;

public SomeApplicationService(

IRepository personRepository,

IDapperRepository 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值