mirror of
https://github.com/tribufu/tribufu-dotnet
synced 2026-02-04 10:46:34 +00:00
Update database seeder
This commit is contained in:
50
src/Tribufu.Database/DatabaseSeeder.cs
Normal file
50
src/Tribufu.Database/DatabaseSeeder.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
// Copyright (c) Tribufu. All Rights Reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Tribufu.Database.Repositories;
|
||||||
|
|
||||||
|
namespace Tribufu.Database
|
||||||
|
{
|
||||||
|
public class DatabaseSeeder
|
||||||
|
{
|
||||||
|
private readonly IServiceProvider _provider;
|
||||||
|
|
||||||
|
public DatabaseSeeder(IServiceProvider provider)
|
||||||
|
{
|
||||||
|
_provider = provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SeedAsync()
|
||||||
|
{
|
||||||
|
var repoType = typeof(IRepository<,>);
|
||||||
|
|
||||||
|
var repos = _provider
|
||||||
|
.GetServices<object>()
|
||||||
|
.Where(s =>
|
||||||
|
{
|
||||||
|
var type = s.GetType();
|
||||||
|
return type.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == repoType);
|
||||||
|
})
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
foreach (var repo in repos)
|
||||||
|
{
|
||||||
|
var method = repo.GetType().GetMethod("SeedAsync");
|
||||||
|
|
||||||
|
if (method != null)
|
||||||
|
{
|
||||||
|
var task = method.Invoke(repo, null);
|
||||||
|
|
||||||
|
if (task != null)
|
||||||
|
{
|
||||||
|
await (Task)task;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,13 +6,10 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace Tribufu.Database.Repositories
|
namespace Tribufu.Database.Repositories
|
||||||
{
|
{
|
||||||
public interface IRepository
|
public interface IRepository<T, K> where T : class
|
||||||
{
|
{
|
||||||
void SeedDefaults();
|
Task SeedAsync();
|
||||||
}
|
|
||||||
|
|
||||||
public interface IRepository<T, K> : IRepository where T : class
|
|
||||||
{
|
|
||||||
IList<T> GetAll();
|
IList<T> GetAll();
|
||||||
|
|
||||||
Task<IList<T>> GetAllAsync();
|
Task<IList<T>> GetAllAsync();
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace Tribufu.Database.Repositories
|
|||||||
_dbSet = context.Set<T>();
|
_dbSet = context.Set<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void SeedDefaults()
|
public virtual async Task SeedAsync()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
// Copyright (c) Tribufu. All Rights Reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Tribufu.Database.Repositories
|
|
||||||
{
|
|
||||||
public class RepositorySeeder
|
|
||||||
{
|
|
||||||
private readonly IEnumerable<IRepository> _repositories;
|
|
||||||
|
|
||||||
public RepositorySeeder(IEnumerable<IRepository> repositories)
|
|
||||||
{
|
|
||||||
_repositories = repositories;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Run()
|
|
||||||
{
|
|
||||||
foreach (var repo in _repositories)
|
|
||||||
{
|
|
||||||
repo.SeedDefaults();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user