Browse Source

Resolved #924: Create a data seed system

pull/926/head
Halil ibrahim Kalkan 8 years ago
parent
commit
0c3e45f0ea
  1. 27
      framework/src/Volo.Abp.Data/Volo/Abp/Data/AbpDataModule.cs
  2. 1
      framework/src/Volo.Abp.Data/Volo/Abp/Data/DataFilter.cs
  3. 48
      framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeedContext.cs
  4. 9
      framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeedContributorList.cs
  5. 12
      framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeedOptions.cs
  6. 37
      framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeeder.cs
  7. 13
      framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeederExtensions.cs
  8. 9
      framework/src/Volo.Abp.Data/Volo/Abp/Data/IDataSeeder.cs

27
framework/src/Volo.Abp.Data/Volo/Abp/Data/AbpDataModule.cs

@ -1,10 +1,17 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
namespace Volo.Abp.Data
{
public class AbpDataModule : AbpModule
{
public override void PreConfigureServices(ServiceConfigurationContext context)
{
AutoAddDataSeedContributors(context.Services);
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
@ -13,5 +20,23 @@ namespace Volo.Abp.Data
context.Services.AddSingleton(typeof(IDataFilter<>), typeof(DataFilter<>));
}
private static void AutoAddDataSeedContributors(IServiceCollection services)
{
var contributors = new List<Type>();
services.OnRegistred(context =>
{
if (typeof(IDataSeedContributor).IsAssignableFrom(context.ImplementationType))
{
contributors.Add(context.ImplementationType);
}
});
services.Configure<DataSeedOptions>(options =>
{
options.Contributors.AddIfNotContains(contributors);
});
}
}
}

1
framework/src/Volo.Abp.Data/Volo/Abp/Data/DataFilter.cs

@ -8,6 +8,7 @@ using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Data
{
//TODO: Create a Volo.Abp.Data.Filtering namespace?
public class DataFilter : IDataFilter, ISingletonDependency
{
private readonly ConcurrentDictionary<Type, object> _filters;

48
framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeedContext.cs

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
namespace Volo.Abp.Data
{
public class DataSeedContext
{
public Guid? TenantId { get; set; }
/// <summary>
/// Gets/sets a key-value on the <see cref="Properties"/>.
/// </summary>
/// <param name="name">Name of the property</param>
/// <returns>
/// Returns the value in the <see cref="Properties"/> dictionary by given <see cref="name"/>.
/// Returns null if given <see cref="name"/> is not present in the <see cref="Properties"/> dictionary.
/// </returns>
[CanBeNull]
public object this[string name]
{
get => Properties.GetOrDefault(name);
set => Properties[name] = value;
}
/// <summary>
/// Can be used to get/set custom properties.
/// </summary>
[NotNull]
public Dictionary<string, object> Properties { get; }
public DataSeedContext(Guid? tenantId = null)
{
TenantId = tenantId;
Properties = new Dictionary<string, object>();
}
/// <summary>
/// Sets a property in the <see cref="Properties"/> dictionary.
/// This is a shortcut for nested calls on this object.
/// </summary>
public virtual DataSeedContext WithProperty(string key, object value)
{
Properties[key] = value;
return this;
}
}
}

9
framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeedContributorList.cs

@ -0,0 +1,9 @@
using Volo.Abp.Collections;
namespace Volo.Abp.Data
{
public class DataSeedContributorList : TypeList<IDataSeedContributor>
{
}
}

12
framework/src/Volo.Abp.Data/Volo/Abp/Data/DataSeedOptions.cs

@ -0,0 +1,12 @@
namespace Volo.Abp.Data
{
public class DataSeedOptions
{
public DataSeedContributorList Contributors { get; }
public DataSeedOptions()
{
Contributors = new DataSeedContributorList();
}
}
}

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

@ -0,0 +1,37 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Data
{
//TODO: Create a Volo.Abp.Data.Seeding namespace?
public class DataSeeder : IDataSeeder, ITransientDependency
{
protected IHybridServiceScopeFactory ServiceScopeFactory { get; }
protected DataSeedOptions Options { get; }
public DataSeeder(
IOptions<DataSeedOptions> options,
IHybridServiceScopeFactory serviceScopeFactory)
{
ServiceScopeFactory = serviceScopeFactory;
Options = options.Value;
}
public async Task SeedAsync(DataSeedContext context)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
foreach (var contributorType in Options.Contributors)
{
var contributor = (IDataSeedContributor) scope
.ServiceProvider
.GetRequiredService(contributorType);
await contributor.SeedAsync(context);
}
}
}
}
}

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

@ -0,0 +1,13 @@
using System;
using System.Threading.Tasks;
namespace Volo.Abp.Data
{
public static class DataSeederExtensions
{
public static Task SeedAsync(this IDataSeeder seeder, Guid? tenantId = null)
{
return seeder.SeedAsync(new DataSeedContext(tenantId));
}
}
}

9
framework/src/Volo.Abp.Data/Volo/Abp/Data/IDataSeeder.cs

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace Volo.Abp.Data
{
public interface IDataSeeder
{
Task SeedAsync(DataSeedContext context);
}
}
Loading…
Cancel
Save