mirror of https://github.com/abpframework/abp.git
8 changed files with 155 additions and 1 deletions
@ -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; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.Collections; |
|||
|
|||
namespace Volo.Abp.Data |
|||
{ |
|||
public class DataSeedContributorList : TypeList<IDataSeedContributor> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
namespace Volo.Abp.Data |
|||
{ |
|||
public class DataSeedOptions |
|||
{ |
|||
public DataSeedContributorList Contributors { get; } |
|||
|
|||
public DataSeedOptions() |
|||
{ |
|||
Contributors = new DataSeedContributorList(); |
|||
} |
|||
} |
|||
} |
|||
@ -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); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -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)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Data |
|||
{ |
|||
public interface IDataSeeder |
|||
{ |
|||
Task SeedAsync(DataSeedContext context); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue