Ends in:
0 DAY
20 HRS
14 MIN
50 SEC
Ends in:
0 D
20 H
14 M
50 S

Dapper Integration

Dapper is a light-weight and simple database provider. The major benefit of using Dapper is writing T-SQL queries. It provides some extension methods for IDbConnection interface.

ABP does not encapsulate many functions for Dapper. ABP Dapper library provides a DapperRepository<TDbContext> base class based on ABP EntityFrameworkCore module, which provides the IDbConnection and IDbTransaction properties required by Dapper.

IDbConnection and IDbTransaction works well with the ABP Unit-Of-Work.

Installation

Install and configure EF Core according to EF Core's integrated documentation.

Volo.Abp.Dapper is the main nuget package for the Dapper integration.

You can find it on NuGet Gallery: https://www.nuget.org/packages/Volo.Abp.Dapper

Install it to your project (for a layered application, to your data/infrastructure layer):

Install-Package Volo.Abp.Dapper

Then add AbpDapperModule module dependency (with DependsOn attribute) to your module:

using Volo.Abp.Dapper;
using Volo.Abp.Modularity;

namespace MyCompany.MyProject
{
    [DependsOn(typeof(AbpDapperModule))]
    public class MyModule : AbpModule
    {
        //...
    }
}

Implement Dapper Repository

The following code creates the PersonRepository, which requires EF Core's DbContext (MyAppDbContext). You can inject PersonDapperRepository to your services for your database operations.

DbConnection and DbTransaction comes from the DapperRepository base class.

public class PersonDapperRepository : DapperRepository<MyAppDbContext>, ITransientDependency
{
    public PersonDapperRepository(IDbContextProvider<MyAppDbContext> dbContextProvider)
        : base(dbContextProvider)
    {
    }

    public virtual async Task<List<string>> GetAllPersonNames()
    {
        return (await DbConnection.QueryAsync<string>("select Name from People", transaction: DbTransaction))
            .ToList();
    }

    public virtual async Task<int> UpdatePersonNames(string name)
    {
        return await DbConnection.ExecuteAsync("update People set Name = @NewName", new { NewName = name },
            DbTransaction);
    }
}

Contributors


Last updated: May 20, 2020 Edit this page on GitHub

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

In this document
Community Talks

What’s New with .NET 9 & ABP 9?

21 Nov, 17:00
Online
Watch the Event
Mastering ABP Framework Book
Mastering ABP Framework

This book will help you gain a complete understanding of the framework and modern web application development techniques.

Learn More