mirror of https://github.com/abpframework/abp.git
19 changed files with 387 additions and 346 deletions
@ -1,85 +1,9 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using NSubstitute; |
|||
using Shouldly; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Volo.Abp.TestApp; |
|||
using Volo.Abp.TestApp.Domain; |
|||
using Xunit; |
|||
using Volo.Abp.TestApp.Testing; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.DataFiltering |
|||
{ |
|||
public class MultiTenant_Filter_Tests : EntityFrameworkCoreTestBase //TODO: This class is same of Volo.Abp.MemoryDb.DataFilters.MemoryDb_MultiTenant_Filter_Tests. Can we share source code?
|
|||
public class MultiTenant_Filter_Tests : MultiTenant_Filter_Tests<AbpEntityFrameworkCoreTestModule> |
|||
{ |
|||
private ICurrentTenant _fakeCurrentTenant; |
|||
private readonly IRepository<Person, Guid> _personRepository; |
|||
private readonly IDataFilter<IMultiTenant> _multiTenantFilter; |
|||
|
|||
public MultiTenant_Filter_Tests() |
|||
{ |
|||
_personRepository = GetRequiredService<IRepository<Person, Guid>>(); |
|||
_multiTenantFilter = GetRequiredService<IDataFilter<IMultiTenant>>(); |
|||
} |
|||
|
|||
protected override void AfterAddApplication(IServiceCollection services) |
|||
{ |
|||
_fakeCurrentTenant = Substitute.For<ICurrentTenant>(); |
|||
services.AddSingleton(_fakeCurrentTenant); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Get_Person_For_Current_Tenant() |
|||
{ |
|||
WithUnitOfWork(() => |
|||
{ |
|||
//TenantId = null
|
|||
|
|||
_fakeCurrentTenant.Id.Returns((Guid?)null); |
|||
|
|||
var people = _personRepository.ToList(); |
|||
people.Count.ShouldBe(1); |
|||
people.Any(p => p.Name == "Douglas").ShouldBeTrue(); |
|||
|
|||
//TenantId = TestDataBuilder.TenantId1
|
|||
|
|||
_fakeCurrentTenant.Id.Returns(TestDataBuilder.TenantId1); |
|||
|
|||
people = _personRepository.ToList(); |
|||
people.Count.ShouldBe(2); |
|||
people.Any(p => p.Name == TestDataBuilder.TenantId1 + "-Person1").ShouldBeTrue(); |
|||
people.Any(p => p.Name == TestDataBuilder.TenantId1 + "-Person2").ShouldBeTrue(); |
|||
|
|||
//TenantId = TestDataBuilder.TenantId2
|
|||
|
|||
_fakeCurrentTenant.Id.Returns(TestDataBuilder.TenantId2); |
|||
|
|||
people = _personRepository.ToList(); |
|||
people.Count.ShouldBe(0); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Get_All_People_When_MultiTenant_Filter_Is_Disabled() |
|||
{ |
|||
WithUnitOfWork(() => |
|||
{ |
|||
List<Person> people; |
|||
|
|||
using (_multiTenantFilter.Disable()) |
|||
{ |
|||
//Filter disabled manually
|
|||
people = _personRepository.ToList(); |
|||
people.Count.ShouldBe(3); |
|||
} |
|||
|
|||
//Filter re-enabled automatically
|
|||
people = _personRepository.ToList(); |
|||
people.Count.ShouldBe(1); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -1,71 +1,9 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using Shouldly; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.TestApp.Domain; |
|||
using Xunit; |
|||
using Volo.Abp.TestApp.Testing; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.DataFiltering |
|||
{ |
|||
public class SoftDelete_Filter_Tests : EntityFrameworkCoreTestBase |
|||
public class SoftDelete_Filter_Tests : SoftDelete_Filter_Tests<AbpEntityFrameworkCoreTestModule> |
|||
{ |
|||
private readonly IRepository<Person, Guid> _personRepository; |
|||
private readonly IDataFilter _dataFilter; |
|||
|
|||
public SoftDelete_Filter_Tests() |
|||
{ |
|||
_personRepository = GetRequiredService<IRepository<Person, Guid>>(); |
|||
_dataFilter = GetRequiredService<IDataFilter>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Not_Get_Deleted_Entities_By_Default() |
|||
{ |
|||
WithUnitOfWork(() => |
|||
{ |
|||
var people = _personRepository.ToList(); |
|||
people.Count.ShouldBe(1); |
|||
people.Any(p => p.Name == "Douglas").ShouldBeTrue(); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Get_Deleted_Entities_When_Filter_Is_Disabled() |
|||
{ |
|||
WithUnitOfWork(() => |
|||
{ |
|||
//Soft delete is enabled by default
|
|||
var people = _personRepository.ToList(); |
|||
people.Any(p => !p.IsDeleted).ShouldBeTrue(); |
|||
people.Any(p => p.IsDeleted).ShouldBeFalse(); |
|||
|
|||
using (_dataFilter.Disable<ISoftDelete>()) |
|||
{ |
|||
//Soft delete is disabled
|
|||
people = _personRepository.ToList(); |
|||
people.Any(p => !p.IsDeleted).ShouldBeTrue(); |
|||
people.Any(p => p.IsDeleted).ShouldBeTrue(); |
|||
|
|||
using (_dataFilter.Enable<ISoftDelete>()) |
|||
{ |
|||
//Soft delete is enabled again
|
|||
people = _personRepository.ToList(); |
|||
people.Any(p => !p.IsDeleted).ShouldBeTrue(); |
|||
people.Any(p => p.IsDeleted).ShouldBeFalse(); |
|||
} |
|||
|
|||
//Soft delete is disabled (restored previous state)
|
|||
people = _personRepository.ToList(); |
|||
people.Any(p => !p.IsDeleted).ShouldBeTrue(); |
|||
people.Any(p => p.IsDeleted).ShouldBeTrue(); |
|||
} |
|||
|
|||
//Soft delete is enabled (restored previous state)
|
|||
people = _personRepository.ToList(); |
|||
people.Any(p => !p.IsDeleted).ShouldBeTrue(); |
|||
people.Any(p => p.IsDeleted).ShouldBeFalse(); |
|||
}); |
|||
} |
|||
|
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,8 @@ |
|||
using Volo.Abp.TestApp.Testing; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.DomainEvents |
|||
{ |
|||
public class DomainEvents_Tests : DomainEvents_Tests<AbpEntityFrameworkCoreTestModule> |
|||
{ |
|||
} |
|||
} |
|||
@ -1,8 +0,0 @@ |
|||
using Volo.Abp.TestApp.Testing; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.Repositories |
|||
{ |
|||
public class EfCore_Repository_Basic_Tests : Repository_Basic_Tests<AbpEntityFrameworkCoreTestModule> |
|||
{ |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.TestApp.Testing; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.Repositories |
|||
{ |
|||
public class Repository_Basic_Tests : Repository_Basic_Tests<AbpEntityFrameworkCoreTestModule> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.EntityFrameworkCore.TestApp.SecondContext; |
|||
using Volo.Abp.TestApp.Testing; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.Repositories |
|||
{ |
|||
public class Repository_Queryable_Tests : Repository_Queryable_Tests<AbpEntityFrameworkCoreTestModule> |
|||
{ |
|||
private readonly IRepository<BookInSecondDbContext, Guid> _bookRepository; |
|||
private readonly IRepository<PhoneInSecondDbContext> _phoneInSecondDbContextRepository; |
|||
|
|||
public Repository_Queryable_Tests() |
|||
{ |
|||
_bookRepository = ServiceProvider.GetRequiredService<IRepository<BookInSecondDbContext, Guid>>(); |
|||
_phoneInSecondDbContextRepository = ServiceProvider.GetRequiredService<IRepository<PhoneInSecondDbContext>>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetBookList() |
|||
{ |
|||
WithUnitOfWork(() => |
|||
{ |
|||
_bookRepository.Any().ShouldBeTrue(); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetPhoneInSecondDbContextList() |
|||
{ |
|||
WithUnitOfWork(() => |
|||
{ |
|||
_phoneInSecondDbContextRepository.Any().ShouldBeTrue(); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -1,79 +0,0 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.EntityFrameworkCore.TestApp.SecondContext; |
|||
using Volo.Abp.TestApp.Domain; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.Repositories |
|||
{ |
|||
//TODO: Remove WithUnitOfWork
|
|||
//TODO: Share same abstract base class for repository tests (with mongodb and others)
|
|||
|
|||
public class Repository_Tests : EntityFrameworkCoreTestBase |
|||
{ |
|||
private readonly IRepository<Person, Guid> _personRepository; |
|||
private readonly IRepository<BookInSecondDbContext, Guid> _bookRepository; |
|||
private readonly IRepository<PhoneInSecondDbContext> _phoneInSecondDbContextRepository; |
|||
private readonly ICityRepository _cityRepository; |
|||
|
|||
public Repository_Tests() |
|||
{ |
|||
_personRepository = ServiceProvider.GetRequiredService<IRepository<Person, Guid>>(); |
|||
_bookRepository = ServiceProvider.GetRequiredService<IRepository<BookInSecondDbContext, Guid>>(); |
|||
_phoneInSecondDbContextRepository = ServiceProvider.GetRequiredService<IRepository<PhoneInSecondDbContext>>(); |
|||
_cityRepository = GetRequiredService<ICityRepository>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetPersonList() |
|||
{ |
|||
WithUnitOfWork(() => |
|||
{ |
|||
_personRepository.Any().ShouldBeTrue(); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetBookList() |
|||
{ |
|||
WithUnitOfWork(() => |
|||
{ |
|||
_bookRepository.Any().ShouldBeTrue(); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetPhoneInSecondDbContextList() |
|||
{ |
|||
WithUnitOfWork(() => |
|||
{ |
|||
_phoneInSecondDbContextRepository.Any().ShouldBeTrue(); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task GetPeopleInTheCityAsync() |
|||
{ |
|||
var people = await _cityRepository.GetPeopleInTheCityAsync("London"); |
|||
people.Count.ShouldBeGreaterThan(0); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task InsertAsync() |
|||
{ |
|||
await WithUnitOfWorkAsync(async () => |
|||
{ |
|||
var personId = Guid.NewGuid(); |
|||
|
|||
await _personRepository.InsertAsync(new Person(personId, "Adam", 42)); |
|||
|
|||
var person = await _personRepository.FindAsync(personId); |
|||
person.ShouldNotBeNull(); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -1,16 +1,17 @@ |
|||
using System; |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.EntityFrameworkCore.TestApp.SecondContext; |
|||
using Volo.Abp.TestApp.Domain; |
|||
using Volo.Abp.TestApp.Testing; |
|||
using Volo.Abp.Uow; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore |
|||
namespace Volo.Abp.EntityFrameworkCore.Transactions |
|||
{ |
|||
public class Transaction_Tests : EntityFrameworkCoreTestBase |
|||
public class Transaction_Tests : TestAppTestBase<AbpEntityFrameworkCoreTestModule> |
|||
{ |
|||
private readonly IBasicRepository<Person, Guid> _personRepository; |
|||
private readonly IBasicRepository<BookInSecondDbContext, Guid> _bookRepository; |
|||
@ -1,13 +0,0 @@ |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.TestApp.Testing |
|||
{ |
|||
public abstract class TestAppTestBase<TStartupModule> : AbpIntegratedTest<TStartupModule> |
|||
where TStartupModule : IAbpModule |
|||
{ |
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
{ |
|||
options.UseAutofac(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,86 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using NSubstitute; |
|||
using Shouldly; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Volo.Abp.TestApp.Domain; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.TestApp.Testing |
|||
{ |
|||
public abstract class MultiTenant_Filter_Tests<TStartupModule> : TestAppTestBase<TStartupModule> |
|||
where TStartupModule : IAbpModule |
|||
{ |
|||
private ICurrentTenant _fakeCurrentTenant; |
|||
private readonly IRepository<Person, Guid> _personRepository; |
|||
private readonly IDataFilter<IMultiTenant> _multiTenantFilter; |
|||
|
|||
protected MultiTenant_Filter_Tests() |
|||
{ |
|||
_personRepository = GetRequiredService<IRepository<Person, Guid>>(); |
|||
_multiTenantFilter = GetRequiredService<IDataFilter<IMultiTenant>>(); |
|||
} |
|||
|
|||
protected override void AfterAddApplication(IServiceCollection services) |
|||
{ |
|||
_fakeCurrentTenant = Substitute.For<ICurrentTenant>(); |
|||
services.AddSingleton(_fakeCurrentTenant); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Get_Person_For_Current_Tenant() |
|||
{ |
|||
WithUnitOfWork(() => |
|||
{ |
|||
//TenantId = null
|
|||
|
|||
_fakeCurrentTenant.Id.Returns((Guid?)null); |
|||
|
|||
var people = _personRepository.ToList(); |
|||
people.Count.ShouldBe(1); |
|||
people.Any(p => p.Name == "Douglas").ShouldBeTrue(); |
|||
|
|||
//TenantId = TestDataBuilder.TenantId1
|
|||
|
|||
_fakeCurrentTenant.Id.Returns(TestDataBuilder.TenantId1); |
|||
|
|||
people = _personRepository.ToList(); |
|||
people.Count.ShouldBe(2); |
|||
people.Any(p => p.Name == TestDataBuilder.TenantId1 + "-Person1").ShouldBeTrue(); |
|||
people.Any(p => p.Name == TestDataBuilder.TenantId1 + "-Person2").ShouldBeTrue(); |
|||
|
|||
//TenantId = TestDataBuilder.TenantId2
|
|||
|
|||
_fakeCurrentTenant.Id.Returns(TestDataBuilder.TenantId2); |
|||
|
|||
people = _personRepository.ToList(); |
|||
people.Count.ShouldBe(0); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Get_All_People_When_MultiTenant_Filter_Is_Disabled() |
|||
{ |
|||
WithUnitOfWork(() => |
|||
{ |
|||
List<Person> people; |
|||
|
|||
using (_multiTenantFilter.Disable()) |
|||
{ |
|||
//Filter disabled manually
|
|||
people = _personRepository.ToList(); |
|||
people.Count.ShouldBe(3); |
|||
} |
|||
|
|||
//Filter re-enabled automatically
|
|||
people = _personRepository.ToList(); |
|||
people.Count.ShouldBe(1); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.TestApp.Domain; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.TestApp.Testing |
|||
{ |
|||
public abstract class Repository_Queryable_Tests<TStartupModule> : TestAppTestBase<TStartupModule> |
|||
where TStartupModule : IAbpModule |
|||
{ |
|||
protected readonly IRepository<Person, Guid> PersonRepository; |
|||
|
|||
protected Repository_Queryable_Tests() |
|||
{ |
|||
PersonRepository = ServiceProvider.GetRequiredService<IRepository<Person, Guid>>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GetPersonList() |
|||
{ |
|||
WithUnitOfWork(() => |
|||
{ |
|||
PersonRepository.Any().ShouldBeTrue(); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,73 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using Shouldly; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.TestApp.Domain; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.TestApp.Testing |
|||
{ |
|||
public abstract class SoftDelete_Filter_Tests<TStartupModule> : TestAppTestBase<TStartupModule> |
|||
where TStartupModule : IAbpModule |
|||
{ |
|||
protected readonly IRepository<Person, Guid> PersonRepository; |
|||
protected readonly IDataFilter DataFilter; |
|||
|
|||
protected SoftDelete_Filter_Tests() |
|||
{ |
|||
PersonRepository = GetRequiredService<IRepository<Person, Guid>>(); |
|||
DataFilter = GetRequiredService<IDataFilter>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Not_Get_Deleted_Entities_By_Default() |
|||
{ |
|||
WithUnitOfWork(() => |
|||
{ |
|||
var people = PersonRepository.ToList(); |
|||
people.Count.ShouldBe(1); |
|||
people.Any(p => p.Name == "Douglas").ShouldBeTrue(); |
|||
}); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Get_Deleted_Entities_When_Filter_Is_Disabled() |
|||
{ |
|||
WithUnitOfWork(() => |
|||
{ |
|||
//Soft delete is enabled by default
|
|||
var people = PersonRepository.ToList(); |
|||
people.Any(p => !p.IsDeleted).ShouldBeTrue(); |
|||
people.Any(p => p.IsDeleted).ShouldBeFalse(); |
|||
|
|||
using (DataFilter.Disable<ISoftDelete>()) |
|||
{ |
|||
//Soft delete is disabled
|
|||
people = PersonRepository.ToList(); |
|||
people.Any(p => !p.IsDeleted).ShouldBeTrue(); |
|||
people.Any(p => p.IsDeleted).ShouldBeTrue(); |
|||
|
|||
using (DataFilter.Enable<ISoftDelete>()) |
|||
{ |
|||
//Soft delete is enabled again
|
|||
people = PersonRepository.ToList(); |
|||
people.Any(p => !p.IsDeleted).ShouldBeTrue(); |
|||
people.Any(p => p.IsDeleted).ShouldBeFalse(); |
|||
} |
|||
|
|||
//Soft delete is disabled (restored previous state)
|
|||
people = PersonRepository.ToList(); |
|||
people.Any(p => !p.IsDeleted).ShouldBeTrue(); |
|||
people.Any(p => p.IsDeleted).ShouldBeTrue(); |
|||
} |
|||
|
|||
//Soft delete is enabled (restored previous state)
|
|||
people = PersonRepository.ToList(); |
|||
people.Any(p => !p.IsDeleted).ShouldBeTrue(); |
|||
people.Any(p => p.IsDeleted).ShouldBeFalse(); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,101 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace Volo.Abp.TestApp.Testing |
|||
{ |
|||
public abstract class TestAppTestBase<TStartupModule> : AbpIntegratedTest<TStartupModule> |
|||
where TStartupModule : IAbpModule |
|||
{ |
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
{ |
|||
options.UseAutofac(); |
|||
} |
|||
|
|||
#region WithUnitOfWork
|
|||
|
|||
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; |
|||
} |
|||
} |
|||
} |
|||
|
|||
#endregion
|
|||
} |
|||
} |
|||
Loading…
Reference in new issue