Version
Language

Dapper Integration

Because Dapper's idea is that the sql statement takes precedence, and mainly provides some extension methods for the IDbConnection interface.

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

These two properties can work well with Unit-Of-Work.

Installation

Please install and configure EF Core according to EF Core's integrated documentation.

Volo.Abp.Dapper is the main nuget package for the Dapper integration. Install it to your project (for a layered application, to your data/infrastructure layer):

Install-Package Volo.Abp.Dapper

Then add AbpDapperModule module dependency (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 implements the Person repository, which requires EF Core's DbContext (MyAppDbContext). You can inject PersonDapperRepository to call its methods.

DbConnection and DbTransaction are 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);
    }
}
Was this page helpful?
Please make a selection.
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
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.