diff --git a/docs/en/Dapper.md b/docs/en/Dapper.md new file mode 100644 index 0000000000..d8b17838eb --- /dev/null +++ b/docs/en/Dapper.md @@ -0,0 +1,61 @@ +# 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` 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](Unit-Of-Work.md). + +## Installation + +Please install and configure EF Core according to [EF Core's integrated documentation](Entity-Framework-Core.md). + +`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): + +```shell +Install-Package Volo.Abp.Dapper +``` + +Then add `AbpDapperModule` module dependency (`DependsOn` attribute) to your [module](Module-Development-Basics.md): + +````C# +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. + +```C# +public class PersonDapperRepository : DapperRepository, ITransientDependency +{ + public PersonDapperRepository(IDbContextProvider dbContextProvider) + : base(dbContextProvider) + { + } + + public virtual async Task> GetAllPersonNames() + { + return (await DbConnection.QueryAsync("select Name from People", transaction: DbTransaction)) + .ToList(); + } + + public virtual async Task UpdatePersonNames(string name) + { + return await DbConnection.ExecuteAsync("update People set Name = @NewName", new { NewName = name }, + DbTransaction); + } +} +``` diff --git a/docs/en/docs-nav.json b/docs/en/docs-nav.json index 6773c81079..85c58cd3f1 100644 --- a/docs/en/docs-nav.json +++ b/docs/en/docs-nav.json @@ -250,6 +250,10 @@ { "text": "MongoDB Integration", "path": "MongoDB.md" + }, + { + "text": "Dapper Integration", + "path": "Dapper.md" } ] }, diff --git a/docs/zh-Hans/Dapper.md b/docs/zh-Hans/Dapper.md new file mode 100644 index 0000000000..60289abd00 --- /dev/null +++ b/docs/zh-Hans/Dapper.md @@ -0,0 +1,61 @@ +# Dapper 集成 + +由于Dapper的思想是sql语句优先, 且主要为`IDbConnection`接口提供了一些扩展方法. + +Abp并没有为Dapper封装太多功能. Abp Dapper在Abp EntityFrameworkCore的基础上提供了`DapperRepository`基类, 在其中提供了Dapper需要的`IDbConnection`和`IDbTransaction`属性. + +这两个属性可以和[工作单元](Unit-Of-Work.md)很好的配合. + +## 安装 + +请先根据[EF Core的集成文档](Entity-Framework-Core.md)安装并配置好EF Core. + +`Volo.Abp.Dapper`是Dapper集成的主要nuget包. 将其安装到你的项目中(在分层应用程序中适用于 数据访问/基础设施层): + +```shell +Install-Package Volo.Abp.Dapper +``` + +然后添加 `AbpDapperModule` 模块依赖项(`DependsOn` Attribute) 到 [module](Module-Development-Basics.cn.md)(项目中的Mudole类): + +````C# +using Volo.Abp.Dapper; +using Volo.Abp.Modularity; + +namespace MyCompany.MyProject +{ + [DependsOn(typeof(AbpDapperModule))] + public class MyModule : AbpModule + { + //... + } +} +```` + +## 实现Dapper仓储 + +下面的代码实现了`Person`仓储, 它需要EF Core的`DbContext`(MyAppDbContext). 你可以注入`PersonDapperRepository`来调用它的方法. + +`DbConnection`和`DbTransaction`来自于`DapperRepository`基类. + +```C# +public class PersonDapperRepository : DapperRepository, ITransientDependency +{ + public PersonDapperRepository(IDbContextProvider dbContextProvider) + : base(dbContextProvider) + { + } + + public virtual async Task> GetAllPersonNames() + { + return (await DbConnection.QueryAsync("select Name from People", transaction: DbTransaction)) + .ToList(); + } + + public virtual async Task UpdatePersonNames(string name) + { + return await DbConnection.ExecuteAsync("update People set Name = @NewName", new { NewName = name }, + DbTransaction); + } +} +``` diff --git a/docs/zh-Hans/Entity-Framework-Core.md b/docs/zh-Hans/Entity-Framework-Core.md index e709767652..6a7f69c2aa 100644 --- a/docs/zh-Hans/Entity-Framework-Core.md +++ b/docs/zh-Hans/Entity-Framework-Core.md @@ -10,7 +10,7 @@ Install-Package Volo.Abp.EntityFrameworkCore ``` -然后添加 `AbpEntityFrameworkCoreModule` 模块依赖项(`DependsOn` Attribute) 到 [module](Module-Development-Basics.cn.md)(项目中的Mudole类): +然后添加 `AbpEntityFrameworkCoreModule` 模块依赖项(`DependsOn` Attribute) 到 [module](Module-Development-Basics.md)(项目中的Mudole类): ````C# using Volo.Abp.EntityFrameworkCore; diff --git a/docs/zh-Hans/docs-nav.json b/docs/zh-Hans/docs-nav.json index fb147ac0fe..60c4afb3d7 100644 --- a/docs/zh-Hans/docs-nav.json +++ b/docs/zh-Hans/docs-nav.json @@ -242,6 +242,10 @@ { "text": "MongoDB 集成", "path": "MongoDB.md" + }, + { + "text": "Dapper 集成", + "path": "Dapper.md" } ] },