Browse Source

Implement async transaction methods.

pull/6809/head
Halil İbrahim Kalkan 6 years ago
parent
commit
d479e70e40
  1. 12
      framework/src/Volo.Abp.Dapper/Volo/Abp/Domain/Repositories/Dapper/DapperRepository.cs
  2. 12
      framework/src/Volo.Abp.Dapper/Volo/Abp/Domain/Repositories/Dapper/IDapperRepository.cs
  3. 15
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EfCoreRepositoryExtensions.cs
  4. 47
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
  5. 2
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs
  6. 40
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs
  7. 6
      framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs
  8. 8
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs
  9. 6
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs

12
framework/src/Volo.Abp.Dapper/Volo/Abp/Domain/Repositories/Dapper/DapperRepository.cs

@ -1,4 +1,6 @@
using System.Data;
using System;
using System.Data;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Volo.Abp.EntityFrameworkCore;
@ -16,8 +18,14 @@ namespace Volo.Abp.Domain.Repositories.Dapper
_dbContextProvider = dbContextProvider;
}
[Obsolete("Use GetDbConnectionAsync method.")]
public IDbConnection DbConnection => _dbContextProvider.GetDbContext().Database.GetDbConnection();
public async Task<IDbConnection> GetDbConnectionAsync() => (await _dbContextProvider.GetDbContextAsync()).Database.GetDbConnection();
[Obsolete("Use GetDbTransactionAsync method.")]
public IDbTransaction DbTransaction => _dbContextProvider.GetDbContext().Database.CurrentTransaction?.GetDbTransaction();
public async Task<IDbTransaction> GetDbTransactionAsync() => (await _dbContextProvider.GetDbContextAsync()).Database.CurrentTransaction?.GetDbTransaction();
}
}
}

12
framework/src/Volo.Abp.Dapper/Volo/Abp/Domain/Repositories/Dapper/IDapperRepository.cs

@ -1,11 +1,19 @@
using System.Data;
using System;
using System.Data;
using System.Threading.Tasks;
namespace Volo.Abp.Domain.Repositories.Dapper
{
public interface IDapperRepository
{
[Obsolete("Use GetDbConnectionAsync method.")]
IDbConnection DbConnection { get; }
Task<IDbConnection> GetDbConnectionAsync();
[Obsolete("Use GetDbTransactionAsync method.")]
IDbTransaction DbTransaction { get; }
Task<IDbTransaction> GetDbTransactionAsync();
}
}
}

15
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EfCoreRepositoryExtensions.cs

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
@ -7,18 +8,32 @@ namespace Volo.Abp.Domain.Repositories
{
public static class EfCoreRepositoryExtensions
{
[Obsolete("Use GetDbContextAsync method.")]
public static DbContext GetDbContext<TEntity>(this IReadOnlyBasicRepository<TEntity> repository)
where TEntity : class, IEntity
{
return repository.ToEfCoreRepository().DbContext;
}
public static Task<DbContext> GetDbContextAsync<TEntity>(this IReadOnlyBasicRepository<TEntity> repository)
where TEntity : class, IEntity
{
return repository.ToEfCoreRepository().GetDbContextAsync();
}
[Obsolete("Use GetDbSetAsync method.")]
public static DbSet<TEntity> GetDbSet<TEntity>(this IReadOnlyBasicRepository<TEntity> repository)
where TEntity : class, IEntity
{
return repository.ToEfCoreRepository().DbSet;
}
public static Task<DbSet<TEntity>> GetDbSetAsync<TEntity>(this IReadOnlyBasicRepository<TEntity> repository)
where TEntity : class, IEntity
{
return repository.ToEfCoreRepository().GetDbSetAsync();
}
public static IEfCoreRepository<TEntity> ToEfCoreRepository<TEntity>(this IReadOnlyBasicRepository<TEntity> repository)
where TEntity : class, IEntity
{

47
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs

@ -1,8 +1,6 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Nito.AsyncEx;
using System;
using System.Collections.Generic;
using System.Linq;
@ -55,7 +53,7 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
private readonly IDbContextProvider<TDbContext> _dbContextProvider;
private readonly Lazy<AbpEntityOptions<TEntity>> _entityOptionsLazy;
public virtual IGuidGenerator GuidGenerator { get; set; }
public IGuidGenerator GuidGenerator { get; set; }
public IEfCoreBulkOperationProvider BulkOperationProvider { get; set; }
@ -90,7 +88,11 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
public override async Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
{
foreach (var entity in entities)
var entityArray = entities.ToArray();
var dbContext = await GetDbContextAsync();
cancellationToken = GetCancellationToken(cancellationToken);
foreach (var entity in entityArray)
{
CheckAndSetId(entity);
}
@ -99,18 +101,18 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
{
await BulkOperationProvider.InsertManyAsync<TDbContext, TEntity>(
this,
entities,
entityArray,
autoSave,
cancellationToken
);
return;
}
await DbSet.AddRangeAsync(entities);
await dbContext.Set<TEntity>().AddRangeAsync(entityArray, cancellationToken);
if (autoSave)
{
await DbContext.SaveChangesAsync();
await dbContext.SaveChangesAsync(cancellationToken);
}
}
@ -132,6 +134,8 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
public override async Task UpdateManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
{
cancellationToken = GetCancellationToken(cancellationToken);
if (BulkOperationProvider != null)
{
await BulkOperationProvider.UpdateManyAsync<TDbContext, TEntity>(
@ -144,11 +148,13 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return;
}
DbSet.UpdateRange(entities);
var dbContext = await GetDbContextAsync();
dbContext.Set<TEntity>().UpdateRange(entities);
if (autoSave)
{
await DbContext.SaveChangesAsync();
await dbContext.SaveChangesAsync(cancellationToken);
}
}
@ -166,22 +172,27 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
public override async Task DeleteManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
{
cancellationToken = GetCancellationToken(cancellationToken);
if (BulkOperationProvider != null)
{
await BulkOperationProvider.DeleteManyAsync<TDbContext, TEntity>(
this,
entities,
autoSave,
cancellationToken);
cancellationToken
);
return;
}
DbSet.RemoveRange(entities);
var dbContext = await GetDbContextAsync();
dbContext.RemoveRange(entities);
if (autoSave)
{
await DbContext.SaveChangesAsync();
await dbContext.SaveChangesAsync(cancellationToken);
}
}
@ -225,9 +236,9 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return (await GetDbSetAsync()).AsQueryable();
}
protected override Task SaveChangesAsync(CancellationToken cancellationToken)
protected override async Task SaveChangesAsync(CancellationToken cancellationToken)
{
return DbContext.SaveChangesAsync(cancellationToken);
await (await GetDbContextAsync()).SaveChangesAsync(cancellationToken);
}
public override async Task<TEntity> FindAsync(
@ -413,9 +424,11 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
await DeleteAsync(entity, autoSave, cancellationToken);
}
public async virtual Task DeleteManyAsync([NotNull] IEnumerable<TKey> ids, bool autoSave = false, CancellationToken cancellationToken = default)
public virtual async Task DeleteManyAsync(IEnumerable<TKey> ids, bool autoSave = false, CancellationToken cancellationToken = default)
{
var entities = await DbSet.Where(x => ids.Contains(x.Id)).ToListAsync();
cancellationToken = GetCancellationToken(cancellationToken);
var entities = await (await GetDbSetAsync()).Where(x => ids.Contains(x.Id)).ToListAsync(cancellationToken);
await DeleteManyAsync(entities, autoSave, cancellationToken);
}

2
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs

@ -143,7 +143,9 @@ namespace Volo.Abp.ObjectExtending
var propertyBuilder = typeBuilder.Property(property.Type, property.Name);
efCoreMapping.EntityTypeAndPropertyBuildAction?.Invoke(typeBuilder, propertyBuilder);
#pragma warning disable 618
efCoreMapping.PropertyBuildAction?.Invoke(propertyBuilder);
#pragma warning restore 618
}
}
}

40
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs

@ -1,4 +1,3 @@
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
@ -169,27 +168,28 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
await TriggerDomainEventsAsync(entity);
}
public override Task<TEntity> FindAsync(
public override async Task<TEntity> FindAsync(
Expression<Func<TEntity, bool>> predicate,
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
return Task.FromResult(GetQueryable().Where(predicate).SingleOrDefault());
return (await GetQueryableAsync()).Where(predicate).SingleOrDefault();
}
public async override Task DeleteAsync(
public override async Task DeleteAsync(
Expression<Func<TEntity, bool>> predicate,
bool autoSave = false,
CancellationToken cancellationToken = default)
{
var entities = GetQueryable().Where(predicate).ToList();
var entities = (await GetQueryableAsync()).Where(predicate).ToList();
foreach (var entity in entities)
{
await DeleteAsync(entity, autoSave, cancellationToken);
}
}
public async override Task<TEntity> InsertAsync(
public override async Task<TEntity> InsertAsync(
TEntity entity,
bool autoSave = false,
CancellationToken cancellationToken = default)
@ -201,7 +201,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
return entity;
}
public async override Task<TEntity> UpdateAsync(
public override async Task<TEntity> UpdateAsync(
TEntity entity,
bool autoSave = false,
CancellationToken cancellationToken = default)
@ -225,7 +225,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
return entity;
}
public async override Task DeleteAsync(
public override async Task DeleteAsync(
TEntity entity,
bool autoSave = false,
CancellationToken cancellationToken = default)
@ -243,27 +243,27 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
}
}
public override Task<List<TEntity>> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default)
public override async Task<List<TEntity>> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default)
{
return Task.FromResult(GetQueryable().ToList());
return (await GetQueryableAsync()).ToList();
}
public override Task<long> GetCountAsync(CancellationToken cancellationToken = default)
public override async Task<long> GetCountAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult(GetQueryable().LongCount());
return (await GetQueryableAsync()).LongCount();
}
public override Task<List<TEntity>> GetPagedListAsync(
public override async Task<List<TEntity>> GetPagedListAsync(
int skipCount,
int maxResultCount,
string sorting,
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
return Task.FromResult(GetQueryable()
return (await GetQueryableAsync())
.OrderBy(sorting)
.PageBy(skipCount, maxResultCount)
.ToList());
.ToList();
}
}
@ -307,9 +307,9 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
return entity;
}
public virtual Task<TEntity> FindAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default)
public virtual async Task<TEntity> FindAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default)
{
return Task.FromResult(GetQueryable().FirstOrDefault(e => e.Id.Equals(id)));
return (await GetQueryableAsync()).FirstOrDefault(e => e.Id.Equals(id));
}
public virtual async Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default)
@ -317,10 +317,10 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
await DeleteAsync(x => x.Id.Equals(id), autoSave, cancellationToken);
}
public virtual async Task DeleteManyAsync([NotNull] IEnumerable<TKey> ids, bool autoSave = false, CancellationToken cancellationToken = default)
public virtual async Task DeleteManyAsync(IEnumerable<TKey> ids, bool autoSave = false, CancellationToken cancellationToken = default)
{
var entities = await AsyncExecuter.ToListAsync(GetQueryable().Where(x => ids.Contains(x.Id)));
DeleteManyAsync(entities, autoSave, cancellationToken);
var entities = await AsyncExecuter.ToListAsync((await GetQueryableAsync()).Where(x => ids.Contains(x.Id)), cancellationToken);
await DeleteManyAsync(entities, autoSave, cancellationToken);
}
}
}

6
framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs

@ -54,13 +54,13 @@ namespace Volo.Abp.Auditing
public class MyAuditedObject1 : IMyAuditedObject
{
public async virtual Task<ResultObject> DoItAsync(InputObject inputObject)
public virtual Task<ResultObject> DoItAsync(InputObject inputObject)
{
return new ResultObject
return Task.FromResult(new ResultObject
{
Value1 = inputObject.Value1 + "-result",
Value2 = inputObject.Value2 + 1
};
});
}
}

8
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs

@ -26,12 +26,12 @@ namespace Volo.Abp.EntityFrameworkCore
{
(ServiceProvider.GetRequiredService<IThirdDbContext>() is TestAppDbContext).ShouldBeTrue();
using (_unitOfWorkManager.Begin())
using (var uow = _unitOfWorkManager.Begin())
{
(_dummyRepository.GetDbContext() is IThirdDbContext).ShouldBeTrue();
(_dummyRepository.GetDbContext() is TestAppDbContext).ShouldBeTrue();
((await _dummyRepository.GetDbContextAsync()) is IThirdDbContext).ShouldBeTrue();
((await _dummyRepository.GetDbContextAsync()) is TestAppDbContext).ShouldBeTrue();
await _unitOfWorkManager.Current.CompleteAsync();
await uow.CompleteAsync();
}
}
}

6
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs

@ -44,15 +44,13 @@ namespace Volo.Abp.EntityFrameworkCore.Domain
[Fact]
public async Task An_Extra_Property_Configured_As_Extension2()
{
await WithUnitOfWorkAsync(() =>
await WithUnitOfWorkAsync(async () =>
{
var entityEntry = CityRepository.GetDbContext().Attach(new City(Guid.NewGuid(), "NewYork"));
var entityEntry = (await CityRepository.GetDbContextAsync()).Attach(new City(Guid.NewGuid(), "NewYork"));
var indexes = entityEntry.Metadata.GetIndexes().ToList();
indexes.ShouldNotBeEmpty();
indexes.ShouldContain(x => x.IsUnique);
return Task.CompletedTask;
});
}
}
}

Loading…
Cancel
Save