Browse Source

Remove UnitOfWork Sync API.

pull/2464/head
Halil İbrahim Kalkan 7 years ago
parent
commit
0754254428
  1. 15
      framework/src/Volo.Abp.Uow/Volo/Abp/Uow/ChildUnitOfWork.cs
  2. 6
      framework/src/Volo.Abp.Uow/Volo/Abp/Uow/IUnitOfWork.cs
  3. 58
      framework/src/Volo.Abp.Uow/Volo/Abp/Uow/UnitOfWork.cs
  4. 6
      framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Uow/TestUnitOfWork.cs
  5. 16
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Queryable_Tests.cs
  6. 5
      framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs
  7. 13
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/MultiTenant_Filter_Tests.cs
  8. 5
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Basic_Tests_With_Int_Pk.cs
  9. 21
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Queryable_Tests.cs
  10. 6
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Specifications_Tests.cs
  11. 16
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/SoftDelete_Filter_Tests.cs
  12. 40
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/TestAppTestBase.cs
  13. 17
      framework/test/Volo.Abp.Uow.Tests/Volo/Abp/Uow/UnitOfWork_Events_Tests.cs

15
framework/src/Volo.Abp.Uow/Volo/Abp/Uow/ChildUnitOfWork.cs

@ -53,31 +53,16 @@ namespace Volo.Abp.Uow
_parent.Reserve(reservationName);
}
public void SaveChanges()
{
_parent.SaveChanges();
}
public Task SaveChangesAsync(CancellationToken cancellationToken = default)
{
return _parent.SaveChangesAsync(cancellationToken);
}
public void Complete()
{
}
public Task CompleteAsync(CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}
public void Rollback()
{
_parent.Rollback();
}
public Task RollbackAsync(CancellationToken cancellationToken = default)
{
return _parent.RollbackAsync(cancellationToken);

6
framework/src/Volo.Abp.Uow/Volo/Abp/Uow/IUnitOfWork.cs

@ -32,16 +32,10 @@ namespace Volo.Abp.Uow
void Reserve([NotNull] string reservationName);
void SaveChanges();
Task SaveChangesAsync(CancellationToken cancellationToken = default);
void Complete();
Task CompleteAsync(CancellationToken cancellationToken = default);
void Rollback();
Task RollbackAsync(CancellationToken cancellationToken = default);
void OnCompleted(Func<Task> handler);

58
framework/src/Volo.Abp.Uow/Volo/Abp/Uow/UnitOfWork.cs

@ -5,7 +5,6 @@ using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;
namespace Volo.Abp.Uow
{
@ -75,14 +74,6 @@ namespace Volo.Abp.Uow
Outer = outer;
}
public virtual void SaveChanges()
{
foreach (var databaseApi in GetAllActiveDatabaseApis())
{
(databaseApi as ISupportsSavingChanges)?.SaveChanges();
}
}
public virtual async Task SaveChangesAsync(CancellationToken cancellationToken = default)
{
foreach (var databaseApi in GetAllActiveDatabaseApis())
@ -104,30 +95,6 @@ namespace Volo.Abp.Uow
return _transactionApis.Values.ToImmutableList();
}
public virtual void Complete()
{
if (_isRolledback)
{
return;
}
PreventMultipleComplete();
try
{
_isCompleting = true;
SaveChanges();
CommitTransactions();
IsCompleted = true;
OnCompleted();
}
catch (Exception ex)
{
_exception = ex;
throw;
}
}
public virtual async Task CompleteAsync(CancellationToken cancellationToken = default)
{
if (_isRolledback)
@ -152,18 +119,6 @@ namespace Volo.Abp.Uow
}
}
public virtual void Rollback()
{
if (_isRolledback)
{
return;
}
_isRolledback = true;
RollbackAll();
}
public virtual async Task RollbackAsync(CancellationToken cancellationToken = default)
{
if (_isRolledback)
@ -235,19 +190,6 @@ namespace Volo.Abp.Uow
CompletedHandlers.Add(handler);
}
public void OnFailed(Func<Task> handler)
{
throw new NotImplementedException();
}
protected virtual void OnCompleted()
{
foreach (var handler in CompletedHandlers)
{
AsyncHelper.RunSync(handler);
}
}
protected virtual async Task OnCompletedAsync()
{
foreach (var handler in CompletedHandlers)

6
framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Uow/TestUnitOfWork.cs

@ -19,12 +19,6 @@ namespace Volo.Abp.AspNetCore.Mvc.Uow
_config = config;
}
public override void Complete()
{
ThrowExceptionIfRequested();
base.Complete();
}
public override Task CompleteAsync(CancellationToken cancellationToken = default(CancellationToken))
{
ThrowExceptionIfRequested();

16
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Queryable_Tests.cs

@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
@ -23,31 +24,34 @@ namespace Volo.Abp.EntityFrameworkCore.Repositories
}
[Fact]
public void GetBookList()
public async Task GetBookList()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
_bookRepository.Any().ShouldBeTrue();
return Task.CompletedTask;
});
}
[Fact]
public void GetPhoneInSecondDbContextList()
public async Task GetPhoneInSecondDbContextList()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
_phoneInSecondDbContextRepository.Any().ShouldBeTrue();
return Task.CompletedTask;
});
}
[Fact]
public void EfCore_Include_Extension()
public async Task EfCore_Include_Extension()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
var person = PersonRepository.Include(p => p.Phones).Single(p => p.Id == TestDataBuilder.UserDouglasId);
person.Name.ShouldBe("Douglas");
person.Phones.Count.ShouldBe(2);
return Task.CompletedTask;
});
}
}

5
framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/Repository_Basic_Tests.cs

@ -12,12 +12,13 @@ namespace Volo.Abp.MongoDB.Repositories
public class Repository_Basic_Tests : Repository_Basic_Tests<AbpMongoDbTestModule>
{
[Fact]
public void Linq_Queries()
public async Task Linq_Queries()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
PersonRepository.FirstOrDefault(p => p.Name == "Douglas").ShouldNotBeNull();
PersonRepository.Count().ShouldBeGreaterThan(0);
return Task.CompletedTask;
});
}

13
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/MultiTenant_Filter_Tests.cs

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
@ -33,9 +34,9 @@ namespace Volo.Abp.TestApp.Testing
}
[Fact]
public void Should_Get_Person_For_Current_Tenant()
public async Task Should_Get_Person_For_Current_Tenant()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
//TenantId = null
@ -60,13 +61,15 @@ namespace Volo.Abp.TestApp.Testing
people = _personRepository.ToList();
people.Count.ShouldBe(0);
return Task.CompletedTask;
});
}
[Fact]
public void Should_Get_All_People_When_MultiTenant_Filter_Is_Disabled()
public async Task Should_Get_All_People_When_MultiTenant_Filter_Is_Disabled()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
List<Person> people;
@ -80,6 +83,8 @@ namespace Volo.Abp.TestApp.Testing
//Filter re-enabled automatically
people = _personRepository.ToList();
people.Count.ShouldBe(1);
return Task.CompletedTask;
});
}
}

5
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Basic_Tests_With_Int_Pk.cs

@ -19,13 +19,14 @@ namespace Volo.Abp.TestApp.Testing
}
[Fact]
public virtual void FirstOrDefault()
public virtual async Task FirstOrDefault()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
var entity = EntityWithIntPkRepository.FirstOrDefault(e => e.Name == "Entity1");
entity.ShouldNotBeNull();
entity.Name.ShouldBe("Entity1");
return Task.CompletedTask;
});
}

21
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Queryable_Tests.cs

@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Domain.Repositories;
@ -20,43 +21,47 @@ namespace Volo.Abp.TestApp.Testing
}
[Fact]
public void Any()
public async Task Any()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
PersonRepository.Any().ShouldBeTrue();
return Task.CompletedTask;
});
}
[Fact]
public void Single()
public async Task Single()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
var person = PersonRepository.Single(p => p.Id == TestDataBuilder.UserDouglasId);
person.Name.ShouldBe("Douglas");
return Task.CompletedTask;
});
}
[Fact]
public void WithDetails()
public async Task WithDetails()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
var person = PersonRepository.WithDetails().Single(p => p.Id == TestDataBuilder.UserDouglasId);
person.Name.ShouldBe("Douglas");
person.Phones.Count.ShouldBe(2);
return Task.CompletedTask;
});
}
[Fact]
public void WithDetails_Explicit()
public async Task WithDetails_Explicit()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
var person = PersonRepository.WithDetails(p => p.Phones).Single(p => p.Id == TestDataBuilder.UserDouglasId);
person.Name.ShouldBe("Douglas");
person.Phones.Count.ShouldBe(2);
return Task.CompletedTask;
});
}
}

6
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Specifications_Tests.cs

@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Modularity;
@ -21,11 +22,12 @@ namespace Volo.Abp.TestApp.Testing
}
[Fact]
public void SpecificationWithRepository_Test()
public async Task SpecificationWithRepository_Test()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
CityRepository.Count(new CitySpecification().ToExpression()).ShouldBe(1);
return Task.CompletedTask;
});
}
}

16
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/SoftDelete_Filter_Tests.cs

@ -23,12 +23,13 @@ namespace Volo.Abp.TestApp.Testing
}
[Fact]
public void Should_Not_Get_Deleted_Entities_Linq()
public async Task Should_Not_Get_Deleted_Entities_Linq()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
var person = PersonRepository.FirstOrDefault(p => p.Name == "John-Deleted");
person.ShouldBeNull();
return Task.CompletedTask;
});
}
@ -43,20 +44,21 @@ namespace Volo.Abp.TestApp.Testing
}
[Fact]
public void Should_Not_Get_Deleted_Entities_By_Default_ToList()
public async Task Should_Not_Get_Deleted_Entities_By_Default_ToList()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
var people = PersonRepository.ToList();
people.Count.ShouldBe(1);
people.Any(p => p.Name == "Douglas").ShouldBeTrue();
return Task.CompletedTask;
});
}
[Fact]
public void Should_Get_Deleted_Entities_When_Filter_Is_Disabled()
public async Task Should_Get_Deleted_Entities_When_Filter_Is_Disabled()
{
WithUnitOfWork(() =>
await WithUnitOfWorkAsync(() =>
{
//Soft delete is enabled by default
var people = PersonRepository.ToList();
@ -88,6 +90,8 @@ namespace Volo.Abp.TestApp.Testing
people = PersonRepository.ToList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeFalse();
return Task.CompletedTask;
});
}
}

40
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/TestAppTestBase.cs

@ -16,26 +16,6 @@ namespace Volo.Abp.TestApp.Testing
#region WithUnitOfWork
protected virtual void WithUnitOfWork(Action action)
{
WithUnitOfWork(new AbpUnitOfWorkOptions(), action);
}
protected virtual void WithUnitOfWork(AbpUnitOfWorkOptions options, Action action)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin(options))
{
action();
uow.Complete();
}
}
}
protected virtual Task WithUnitOfWorkAsync(Func<Task> func)
{
return WithUnitOfWorkAsync(new AbpUnitOfWorkOptions(), func);
@ -56,26 +36,6 @@ namespace Volo.Abp.TestApp.Testing
}
}
protected virtual TResult WithUnitOfWork<TResult>(Func<TResult> func)
{
return WithUnitOfWork(new AbpUnitOfWorkOptions(), func);
}
protected virtual TResult WithUnitOfWork<TResult>(AbpUnitOfWorkOptions options, Func<TResult> func)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin(options))
{
var result = func();
uow.Complete();
return result;
}
}
}
protected virtual Task<TResult> WithUnitOfWorkAsync<TResult>(Func<Task<TResult>> func)
{
return WithUnitOfWorkAsync(new AbpUnitOfWorkOptions(), func);

17
framework/test/Volo.Abp.Uow.Tests/Volo/Abp/Uow/UnitOfWork_Events_Tests.cs

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;
@ -15,7 +16,7 @@ namespace Volo.Abp.Uow
}
[Fact]
public void Should_Trigger_Complete_On_Success()
public async Task Should_Trigger_Complete_On_Success()
{
var completed = false;
var disposed = false;
@ -25,7 +26,7 @@ namespace Volo.Abp.Uow
uow.OnCompleted(async () => completed = true);
uow.Disposed += (sender, args) => disposed = true;
uow.Complete();
await uow.CompleteAsync();
completed.ShouldBeTrue();
}
@ -34,7 +35,7 @@ namespace Volo.Abp.Uow
}
[Fact]
public void Should_Trigger_Complete_On_Success_In_Child_Uow()
public async Task Should_Trigger_Complete_On_Success_In_Child_Uow()
{
var completed = false;
var disposed = false;
@ -46,7 +47,7 @@ namespace Volo.Abp.Uow
childUow.OnCompleted(async () => completed = true);
uow.Disposed += (sender, args) => disposed = true;
childUow.Complete();
await childUow.CompleteAsync();
completed.ShouldBeFalse(); //Parent has not been completed yet!
disposed.ShouldBeFalse();
@ -55,7 +56,7 @@ namespace Volo.Abp.Uow
completed.ShouldBeFalse(); //Parent has not been completed yet!
disposed.ShouldBeFalse();
uow.Complete();
await uow.CompleteAsync();
completed.ShouldBeTrue(); //It's completed now!
disposed.ShouldBeFalse(); //But not disposed yet!
@ -110,7 +111,7 @@ namespace Volo.Abp.Uow
[InlineData(true)]
[InlineData(false)]
[Theory]
public void Should_Trigger_Failed_If_Rolled_Back(bool callComplete)
public async Task Should_Trigger_Failed_If_Rolled_Back(bool callComplete)
{
var completed = false;
var failed = false;
@ -122,11 +123,11 @@ namespace Volo.Abp.Uow
uow.Failed += (sender, args) => { failed = true; args.IsRolledback.ShouldBeTrue(); };
uow.Disposed += (sender, args) => disposed = true;
uow.Rollback();
await uow.RollbackAsync();
if (callComplete)
{
uow.Complete();
await uow.CompleteAsync();
}
}

Loading…
Cancel
Save