Add WithUnitOfWork methods to simplify working with UOW in tests

pull/1148/head
Halil ibrahim Kalkan 7 years ago
parent 1716fb3b0d
commit 2352ca90e2

@ -1,7 +1,6 @@
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Identity;
using Volo.Abp.Uow;
using Xunit;
namespace MyCompanyName.MyProjectName.Samples
@ -15,12 +14,10 @@ namespace MyCompanyName.MyProjectName.Samples
{
private readonly IIdentityUserRepository _identityUserRepository;
private readonly IdentityUserManager _identityUserManager;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public SampleDomainTests()
{
_identityUserRepository = GetRequiredService<IIdentityUserRepository>();
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
_identityUserManager = GetRequiredService<IdentityUserManager>();
}
@ -32,16 +29,14 @@ namespace MyCompanyName.MyProjectName.Samples
/* Need to manually start Unit Of Work because
* FirstOrDefaultAsync should be executed while db connection / context is available.
*/
using (var uow = _unitOfWorkManager.Begin())
await WithUnitOfWorkAsync(async () =>
{
adminUser = await _identityUserRepository
.FindByNormalizedUserNameAsync("ADMIN");
await _identityUserManager.SetEmailAsync(adminUser, "newemail@abp.io");
await _identityUserRepository.UpdateAsync(adminUser);
await uow.CompleteAsync();
}
});
adminUser = await _identityUserRepository.FindByNormalizedUserNameAsync("ADMIN");
adminUser.Email.ShouldBe("newemail@abp.io");

@ -1,11 +1,10 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using MyCompanyName.MyProjectName.Users;
using Shouldly;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;
using Xunit;
namespace MyCompanyName.MyProjectName.EntityFrameworkCore.Samples
@ -18,12 +17,10 @@ namespace MyCompanyName.MyProjectName.EntityFrameworkCore.Samples
public class SampleRepositoryTests : MyProjectNameEntityFrameworkCoreTestBase
{
private readonly IRepository<AppUser, Guid> _appUserRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public SampleRepositoryTests()
{
_appUserRepository = GetRequiredService<IRepository<AppUser, Guid>>();
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
}
[Fact]
@ -32,7 +29,7 @@ namespace MyCompanyName.MyProjectName.EntityFrameworkCore.Samples
/* Need to manually start Unit Of Work because
* FirstOrDefaultAsync should be executed while db connection / context is available.
*/
using (var uow = _unitOfWorkManager.Begin())
await WithUnitOfWorkAsync(async () =>
{
//Act
var adminUser = await _appUserRepository
@ -41,9 +38,7 @@ namespace MyCompanyName.MyProjectName.EntityFrameworkCore.Samples
//Assert
adminUser.ShouldNotBeNull();
await uow.CompleteAsync();
}
});
}
}
}

@ -4,7 +4,6 @@ using MyCompanyName.MyProjectName.Users;
using MongoDB.Driver.Linq;
using Shouldly;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;
using Xunit;
namespace MyCompanyName.MyProjectName.MongoDb.Samples
@ -17,12 +16,10 @@ namespace MyCompanyName.MyProjectName.MongoDb.Samples
public class SampleRepositoryTests : MyProjectNameMongoDbTestBase
{
private readonly IRepository<AppUser, Guid> _appUserRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public SampleRepositoryTests()
{
_appUserRepository = GetRequiredService<IRepository<AppUser, Guid>>();
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
}
[Fact]
@ -31,7 +28,7 @@ namespace MyCompanyName.MyProjectName.MongoDb.Samples
/* Need to manually start Unit Of Work because
* FirstOrDefaultAsync should be executed while db connection / context is available.
*/
using (var uow = _unitOfWorkManager.Begin())
await WithUnitOfWorkAsync(async () =>
{
//Act
var adminUser = await _appUserRepository
@ -40,9 +37,7 @@ namespace MyCompanyName.MyProjectName.MongoDb.Samples
//Assert
adminUser.ShouldNotBeNull();
await uow.CompleteAsync();
}
});
}
}
}

@ -1,5 +1,9 @@
using Volo.Abp;
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.Modularity;
using Volo.Abp.Uow;
namespace MyCompanyName.MyProjectName
{
@ -10,5 +14,85 @@ namespace MyCompanyName.MyProjectName
{
options.UseAutofac();
}
protected virtual void WithUnitOfWork(Action action)
{
WithUnitOfWork(new UnitOfWorkOptions(), action);
}
protected virtual void WithUnitOfWork(UnitOfWorkOptions 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 UnitOfWorkOptions(), func);
}
protected virtual async Task WithUnitOfWorkAsync(UnitOfWorkOptions options, Func<Task> action)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin(options))
{
await action();
await uow.CompleteAsync();
}
}
}
protected virtual TResult WithUnitOfWork<TResult>(Func<TResult> func)
{
return WithUnitOfWork(new UnitOfWorkOptions(), func);
}
protected virtual TResult WithUnitOfWork<TResult>(UnitOfWorkOptions 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 UnitOfWorkOptions(), func);
}
protected virtual async Task<TResult> WithUnitOfWorkAsync<TResult>(UnitOfWorkOptions options, Func<Task<TResult>> func)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin(options))
{
var result = await func();
await uow.CompleteAsync();
return result;
}
}
}
}
}

Loading…
Cancel
Save