Browse Source

Merge pull request #13940 from abpframework/SeedInSeparateUowAsync

Add `SeedInSeparateUowAsync` method.
pull/14318/head
Halil İbrahim Kalkan 4 years ago
committed by GitHub
parent
commit
7946173996
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 32
      framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeeder.cs
  2. 14
      framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeederExtensions.cs
  3. 43
      framework/test/Volo.Abp.Data.Tests/Volo/Abp/Data/DataSeederExtensions_Tests.cs

32
framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeeder.cs

@ -1,4 +1,6 @@
using System.Threading.Tasks;
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
@ -25,13 +27,31 @@ public class DataSeeder : IDataSeeder, ITransientDependency
{
using (var scope = ServiceScopeFactory.CreateScope())
{
foreach (var contributorType in Options.Contributors)
if (context.Properties.ContainsKey(DataSeederExtensions.SeedInSeparateUow))
{
var contributor = (IDataSeedContributor)scope
.ServiceProvider
.GetRequiredService(contributorType);
var manager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
foreach (var contributorType in Options.Contributors)
{
var options = context.Properties.TryGetValue(DataSeederExtensions.SeedInSeparateUowOptions, out var uowOptions)
? (AbpUnitOfWorkOptions) uowOptions
: new AbpUnitOfWorkOptions();
var requiresNew = context.Properties.TryGetValue(DataSeederExtensions.SeedInSeparateUowRequiresNew, out var obj) && (bool) obj;
await contributor.SeedAsync(context);
using (var uow = manager.Begin(options, requiresNew))
{
var contributor = (IDataSeedContributor)scope.ServiceProvider.GetRequiredService(contributorType);
await contributor.SeedAsync(context);
await uow.CompleteAsync();
}
}
}
else
{
foreach (var contributorType in Options.Contributors)
{
var contributor = (IDataSeedContributor)scope.ServiceProvider.GetRequiredService(contributorType);
await contributor.SeedAsync(context);
}
}
}
}

14
framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeederExtensions.cs

@ -1,12 +1,26 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.Uow;
namespace Volo.Abp.Data;
public static class DataSeederExtensions
{
public const string SeedInSeparateUow = "__SeedInSeparateUow";
public const string SeedInSeparateUowOptions = "__SeedInSeparateUowOptions";
public const string SeedInSeparateUowRequiresNew = "__SeedInSeparateUowRequiresNew";
public static Task SeedAsync(this IDataSeeder seeder, Guid? tenantId = null)
{
return seeder.SeedAsync(new DataSeedContext(tenantId));
}
public static Task SeedInSeparateUowAsync(this IDataSeeder seeder, Guid? tenantId = null, AbpUnitOfWorkOptions options = null, bool requiresNew = false)
{
var context = new DataSeedContext(tenantId);
context.WithProperty(SeedInSeparateUow, true);
context.WithProperty(SeedInSeparateUowOptions, options);
context.WithProperty(SeedInSeparateUowRequiresNew, requiresNew);
return seeder.SeedAsync(context);
}
}

43
framework/test/Volo.Abp.Data.Tests/Volo/Abp/Data/DataSeederExtensions_Tests.cs

@ -0,0 +1,43 @@
using System;
using System.Data;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NSubstitute;
using Volo.Abp.Modularity;
using Volo.Abp.Testing;
using Volo.Abp.Uow;
using Xunit;
namespace Volo.Abp.Data;
public class DataSeederExtensions_Tests : AbpIntegratedTest<DataSeederExtensions_Tests.TestModule>
{
private IDataSeeder _dataSeeder;
protected override void AfterAddApplication(IServiceCollection services)
{
_dataSeeder = Substitute.For<IDataSeeder>();
services.Replace(ServiceDescriptor.Singleton(_dataSeeder));
base.AfterAddApplication(services);
}
[Fact]
public void SeedInSeparateUowAsync()
{
var tenantId = Guid.NewGuid();
_dataSeeder.SeedInSeparateUowAsync(tenantId, new AbpUnitOfWorkOptions(true, IsolationLevel.Serializable, 888), true);
_dataSeeder.Received().SeedAsync(Arg.Is<DataSeedContext>(x => x.TenantId == tenantId &&
x.Properties[DataSeederExtensions.SeedInSeparateUow].To<bool>() == true &&
x.Properties[DataSeederExtensions.SeedInSeparateUowOptions].As<AbpUnitOfWorkOptions>().IsTransactional == true &&
x.Properties[DataSeederExtensions.SeedInSeparateUowOptions].As<AbpUnitOfWorkOptions>().IsolationLevel == IsolationLevel.Serializable &&
x.Properties[DataSeederExtensions.SeedInSeparateUowOptions].As<AbpUnitOfWorkOptions>().Timeout == 888 &&
x.Properties[DataSeederExtensions.SeedInSeparateUowRequiresNew].To<bool>() == true));
}
[DependsOn(typeof(AbpDataModule))]
public class TestModule : AbpModule
{
}
}
Loading…
Cancel
Save