用于生成初始数据的提供程序

This commit is contained in:
hello 2024-03-02 09:50:45 +08:00
parent 14b419dbf7
commit 878caabb95
5 changed files with 113 additions and 0 deletions

View File

@ -0,0 +1,27 @@
using HelloShop.IdentityService.Entities;
using HelloShop.IdentityService.EntityFrameworks;
using HelloShop.ServiceDefaults.Infrastructure;
namespace HelloShop.IdentityService.DataSeeding
{
public class User2DataSeedingProvider : IDataSeedingProvider
{
public async Task SeedingAsync(IServiceProvider serviceProvider)
{
var dbContext = serviceProvider.GetRequiredService<IdentityServiceDbContext>();
var guestUser = dbContext.Set<User>().SingleOrDefault(x => x.UserName == "guest2");
if (guestUser is null)
{
await dbContext.Set<User>().AddAsync(new User
{
UserName = "guest2",
PasswordHash = "AQAAAAEAACcQAAAAEJ"
});
await dbContext.SaveChangesAsync();
}
}
}
}

View File

@ -0,0 +1,25 @@
using HelloShop.IdentityService.Entities;
using HelloShop.IdentityService.EntityFrameworks;
using HelloShop.ServiceDefaults.Infrastructure;
namespace HelloShop.IdentityService.DataSeeding
{
public class UserDataSeedingProvider(IdentityServiceDbContext dbContext) : IDataSeedingProvider
{
public async Task SeedingAsync(IServiceProvider serviceProvider)
{
var guestUser = dbContext.Set<User>().SingleOrDefault(x => x.UserName == "guest");
if (guestUser is null)
{
await dbContext.Set<User>().AddAsync(new User
{
UserName = "guest",
PasswordHash = "AQAAAAEAACcQAAAAEJ"
});
await dbContext.SaveChangesAsync();
}
}
}
}

View File

@ -1,5 +1,7 @@
using HelloShop.IdentityService.Constants;
using HelloShop.IdentityService.DataSeeding;
using HelloShop.IdentityService.EntityFrameworks;
using HelloShop.ServiceDefaults.Extensions;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
@ -18,6 +20,8 @@ builder.Services.AddDbContext<IdentityServiceDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString(DbConstants.ConnectionStringName));
});
builder.Services.AddDataSeedingProviders();
var app = builder.Build();
app.MapDefaultEndpoints();
@ -35,4 +39,6 @@ app.UseAuthorization();
app.MapControllers();
app.UseDataSeedingProviders();
app.Run();

View File

@ -0,0 +1,40 @@
using HelloShop.ServiceDefaults.Infrastructure;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace HelloShop.ServiceDefaults.Extensions
{
public static class DataSeedingExtensions
{
public static IServiceCollection AddDataSeedingProviders(this IServiceCollection services, Assembly? assembly = null)
{
assembly ??= Assembly.GetCallingAssembly();
var dataSeedProviders = assembly.ExportedTypes.Where(t => t.IsAssignableTo(typeof(IDataSeedingProvider)) && t.IsClass);
dataSeedProviders.ToList().ForEach(t => services.AddTransient(typeof(IDataSeedingProvider), t));
return services;
}
public static IApplicationBuilder UseDataSeedingProviders(this IApplicationBuilder app)
{
using var serviceScope = app.ApplicationServices.CreateScope();
var dataSeedingProviders = serviceScope.ServiceProvider.GetServices<IDataSeedingProvider>().OrderBy(x=>x.Order);
foreach (var dataSeedingProvider in dataSeedingProviders)
{
dataSeedingProvider.SeedingAsync(serviceScope.ServiceProvider).Wait();
}
return app;
}
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloShop.ServiceDefaults.Infrastructure
{
public interface IDataSeedingProvider
{
int Order => default;
Task SeedingAsync(IServiceProvider serviceProvider);
}
}