diff --git a/framework/src/Volo.Abp.Dapper/Volo/Abp/Domain/Repositories/Dapper/DapperRepository.cs b/framework/src/Volo.Abp.Dapper/Volo/Abp/Domain/Repositories/Dapper/DapperRepository.cs index cdb03f8515..39b3eed249 100644 --- a/framework/src/Volo.Abp.Dapper/Volo/Abp/Domain/Repositories/Dapper/DapperRepository.cs +++ b/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 GetDbConnectionAsync() => (await _dbContextProvider.GetDbContextAsync()).Database.GetDbConnection(); + + [Obsolete("Use GetDbTransactionAsync method.")] public IDbTransaction DbTransaction => _dbContextProvider.GetDbContext().Database.CurrentTransaction?.GetDbTransaction(); + + public async Task GetDbTransactionAsync() => (await _dbContextProvider.GetDbContextAsync()).Database.CurrentTransaction?.GetDbTransaction(); } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Dapper/Volo/Abp/Domain/Repositories/Dapper/IDapperRepository.cs b/framework/src/Volo.Abp.Dapper/Volo/Abp/Domain/Repositories/Dapper/IDapperRepository.cs index f45be08b54..8145c646a0 100644 --- a/framework/src/Volo.Abp.Dapper/Volo/Abp/Domain/Repositories/Dapper/IDapperRepository.cs +++ b/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 GetDbConnectionAsync(); + + [Obsolete("Use GetDbTransactionAsync method.")] IDbTransaction DbTransaction { get; } + + Task GetDbTransactionAsync(); } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EfCoreRepositoryExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EfCoreRepositoryExtensions.cs index 2463062cde..5049a1b61b 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EfCoreRepositoryExtensions.cs +++ b/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(this IReadOnlyBasicRepository repository) where TEntity : class, IEntity { return repository.ToEfCoreRepository().DbContext; } + public static Task GetDbContextAsync(this IReadOnlyBasicRepository repository) + where TEntity : class, IEntity + { + return repository.ToEfCoreRepository().GetDbContextAsync(); + } + + [Obsolete("Use GetDbSetAsync method.")] public static DbSet GetDbSet(this IReadOnlyBasicRepository repository) where TEntity : class, IEntity { return repository.ToEfCoreRepository().DbSet; } + public static Task> GetDbSetAsync(this IReadOnlyBasicRepository repository) + where TEntity : class, IEntity + { + return repository.ToEfCoreRepository().GetDbSetAsync(); + } + public static IEfCoreRepository ToEfCoreRepository(this IReadOnlyBasicRepository repository) where TEntity : class, IEntity { diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs index 16c8e6d745..a95ec00e34 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs +++ b/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 _dbContextProvider; private readonly Lazy> _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 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( this, - entities, + entityArray, autoSave, cancellationToken ); return; } - await DbSet.AddRangeAsync(entities); + await dbContext.Set().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 entities, bool autoSave = false, CancellationToken cancellationToken = default) { + cancellationToken = GetCancellationToken(cancellationToken); + if (BulkOperationProvider != null) { await BulkOperationProvider.UpdateManyAsync( @@ -144,11 +148,13 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore return; } - DbSet.UpdateRange(entities); + var dbContext = await GetDbContextAsync(); + + dbContext.Set().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 entities, bool autoSave = false, CancellationToken cancellationToken = default) { + cancellationToken = GetCancellationToken(cancellationToken); + if (BulkOperationProvider != null) { await BulkOperationProvider.DeleteManyAsync( 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 FindAsync( @@ -413,9 +424,11 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore await DeleteAsync(entity, autoSave, cancellationToken); } - public async virtual Task DeleteManyAsync([NotNull] IEnumerable ids, bool autoSave = false, CancellationToken cancellationToken = default) + public virtual async Task DeleteManyAsync(IEnumerable 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); } diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs index 14fcc93784..54c1909bb3 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/ObjectExtending/EfCoreObjectExtensionManagerExtensions.cs +++ b/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 } } } diff --git a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs index 0edd7e7604..3318d10d72 100644 --- a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs +++ b/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 FindAsync( + public override async Task FindAsync( Expression> 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> 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 InsertAsync( + public override async Task 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 UpdateAsync( + public override async Task 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> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default) + public override async Task> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default) { - return Task.FromResult(GetQueryable().ToList()); + return (await GetQueryableAsync()).ToList(); } - public override Task GetCountAsync(CancellationToken cancellationToken = default) + public override async Task GetCountAsync(CancellationToken cancellationToken = default) { - return Task.FromResult(GetQueryable().LongCount()); + return (await GetQueryableAsync()).LongCount(); } - public override Task> GetPagedListAsync( + public override async Task> 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 FindAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default) + public virtual async Task 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 ids, bool autoSave = false, CancellationToken cancellationToken = default) + public virtual async Task DeleteManyAsync(IEnumerable 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); } } } diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs index 8c647c92c4..8ca1228ee6 100644 --- a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs +++ b/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 DoItAsync(InputObject inputObject) + public virtual Task DoItAsync(InputObject inputObject) { - return new ResultObject + return Task.FromResult(new ResultObject { Value1 = inputObject.Value1 + "-result", Value2 = inputObject.Value2 + 1 - }; + }); } } diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs index 7bc4af2e31..977cb8522a 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs @@ -26,12 +26,12 @@ namespace Volo.Abp.EntityFrameworkCore { (ServiceProvider.GetRequiredService() 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(); } } } diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs index f94f16c390..aa1e0080b5 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Domain/ExtraProperties_Tests.cs +++ b/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; }); - } } }