mirror of https://github.com/abpframework/abp.git
committed by
GitHub
10 changed files with 220 additions and 2 deletions
@ -0,0 +1,24 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<Import Project="..\..\..\common.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netstandard2.0</TargetFramework> |
||||
|
<AssemblyName>Volo.Abp.Dapper</AssemblyName> |
||||
|
<PackageId>Volo.Abp.Dapper</PackageId> |
||||
|
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
||||
|
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
||||
|
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
||||
|
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\Volo.Abp.EntityFrameworkCore\Volo.Abp.EntityFrameworkCore.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Dapper" Version="1.60.6" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,13 @@ |
|||||
|
using Volo.Abp.Domain; |
||||
|
using Volo.Abp.EntityFrameworkCore; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp.Dapper |
||||
|
{ |
||||
|
[DependsOn( |
||||
|
typeof(AbpDddDomainModule), |
||||
|
typeof(AbpEntityFrameworkCoreModule))] |
||||
|
public class AbpDapperModule : AbpModule |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
using System.Data; |
||||
|
using Microsoft.EntityFrameworkCore; |
||||
|
using Microsoft.EntityFrameworkCore.Storage; |
||||
|
using Volo.Abp.EntityFrameworkCore; |
||||
|
using Volo.Abp.Uow; |
||||
|
|
||||
|
namespace Volo.Abp.Domain.Repositories.Dapper |
||||
|
{ |
||||
|
public class DapperRepository<TDbContext> : IDapperRepository, IUnitOfWorkEnabled |
||||
|
where TDbContext : IEfCoreDbContext |
||||
|
{ |
||||
|
private readonly IDbContextProvider<TDbContext> _dbContextProvider; |
||||
|
|
||||
|
public DapperRepository(IDbContextProvider<TDbContext> dbContextProvider) |
||||
|
{ |
||||
|
_dbContextProvider = dbContextProvider; |
||||
|
} |
||||
|
|
||||
|
public IDbConnection DbConnection => _dbContextProvider.GetDbContext().Database.GetDbConnection(); |
||||
|
|
||||
|
public IDbTransaction DbTransaction => _dbContextProvider.GetDbContext().Database.CurrentTransaction?.GetDbTransaction(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
using System.Data; |
||||
|
|
||||
|
namespace Volo.Abp.Domain.Repositories.Dapper |
||||
|
{ |
||||
|
public interface IDapperRepository |
||||
|
{ |
||||
|
IDbConnection DbConnection { get; } |
||||
|
|
||||
|
IDbTransaction DbTransaction { get; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp2.2</TargetFramework> |
||||
|
<AssemblyName>Volo.Abp.Dapper.Tests</AssemblyName> |
||||
|
<PackageId>Volo.Abp.Dapper.Tests</PackageId> |
||||
|
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> |
||||
|
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
||||
|
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
||||
|
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.Dapper\Volo.Abp.Dapper.csproj" /> |
||||
|
<ProjectReference Include="..\Volo.Abp.EntityFrameworkCore.Tests\Volo.Abp.EntityFrameworkCore.Tests.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" /> |
||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.4" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,17 @@ |
|||||
|
using Volo.Abp.Autofac; |
||||
|
using Volo.Abp.EntityFrameworkCore; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp.Dapper |
||||
|
{ |
||||
|
[DependsOn( |
||||
|
typeof(AbpEntityFrameworkCoreTestModule), |
||||
|
typeof(AbpDapperModule), |
||||
|
typeof(AbpAutofacModule))] |
||||
|
public class AbpDapperTestModule : AbpModule |
||||
|
{ |
||||
|
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
namespace Volo.Abp.Dapper |
||||
|
{ |
||||
|
public abstract class DapperTestBase : AbpIntegratedTest<AbpDapperTestModule> |
||||
|
{ |
||||
|
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
||||
|
{ |
||||
|
options.UseAutofac(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Dapper; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Domain.Repositories.Dapper; |
||||
|
using Volo.Abp.EntityFrameworkCore; |
||||
|
using Volo.Abp.TestApp.EntityFrameworkCore; |
||||
|
|
||||
|
namespace Volo.Abp.Dapper.Repositories |
||||
|
{ |
||||
|
public class PersonDapperRepository : DapperRepository<TestAppDbContext>, ITransientDependency |
||||
|
{ |
||||
|
public PersonDapperRepository(IDbContextProvider<TestAppDbContext> 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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,52 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.TestApp; |
||||
|
using Volo.Abp.Uow; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Dapper.Repositories |
||||
|
{ |
||||
|
public class PersonDapperRepository_Tests : DapperTestBase |
||||
|
{ |
||||
|
[Fact] |
||||
|
public async Task GetAllPersonNames_Test() |
||||
|
{ |
||||
|
var allNames = await GetRequiredService<PersonDapperRepository>().GetAllPersonNames(); |
||||
|
allNames.ShouldNotBeEmpty(); |
||||
|
allNames.ShouldContain(x => x == "Douglas"); |
||||
|
allNames.ShouldContain(x => x == "John-Deleted"); |
||||
|
allNames.ShouldContain(x => x == $"{TestDataBuilder.TenantId1}-Person1"); |
||||
|
allNames.ShouldContain(x => x == $"{TestDataBuilder.TenantId1}-Person2"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task UpdatePersonNames_Test() |
||||
|
{ |
||||
|
var personDapperRepository = GetRequiredService<PersonDapperRepository>(); |
||||
|
await personDapperRepository.UpdatePersonNames("test"); |
||||
|
|
||||
|
var allNames = await personDapperRepository.GetAllPersonNames(); |
||||
|
allNames.ShouldNotBeEmpty(); |
||||
|
allNames.ShouldAllBe(x => x == "test"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Dapper_Transaction_Test() |
||||
|
{ |
||||
|
var unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>(); |
||||
|
var personDapperRepository = GetRequiredService<PersonDapperRepository>(); |
||||
|
|
||||
|
using (var uow = unitOfWorkManager.Begin(new UnitOfWorkOptions |
||||
|
{ |
||||
|
IsTransactional = true |
||||
|
})) |
||||
|
{ |
||||
|
await personDapperRepository.UpdatePersonNames("test"); |
||||
|
await uow.RollbackAsync(); |
||||
|
} |
||||
|
|
||||
|
var allNames = await personDapperRepository.GetAllPersonNames(); |
||||
|
allNames.ShouldAllBe(x => x != "test"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue