Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

给种子数据基类SeedDataInitializerBase.SeedData添加ScopedProvider #231

Closed
gmf520 opened this issue Apr 13, 2021 · 0 comments
Closed
Labels
Breaked Changes ⚡ 更新有破坏性,对现有业务实现有较大影响 Feature 🔨 新功能,新特性 Finished ✔️ 实现并完工
Milestone

Comments

@gmf520
Copy link
Member

gmf520 commented Apr 13, 2021

您的功能请求与现有问题有关吗?请描述

给初始化种子基类SeedDataInitializerBase的SeedData添加 ScopedProvider,方便在构建种子数据时拉取需要的父数据

old:

public abstract class SeedDataInitializerBase<TEntity, TKey>
{
        public void Initialize()
        {
            TEntity[] entities = SeedData();
            SyncToDatabase(entities);
        }

        protected abstract TEntity[] SeedData();

		//...
}

new:

public abstract class SeedDataInitializerBase<TEntity, TKey>
{
        public void Initialize()
        {
            _rootProvider.ExecuteScopedWork(provider =>
            {
                TEntity[] entities = SeedData(provider);
                SyncToDatabase(entities, provider);
                _logger.LogInformation($"同步 {entities.Length} 个“{typeof(TEntity)}”种子数据到数据库");
            });
        }

        protected abstract TEntity[] SeedData(IServiceProvider scopedProvider);

		//...
}

应用:

public class CodeModuleSeedDataInitializer : SeedDataInitializerBase<CodeModule, Guid>
{
    //...
    
    /// <summary>重写以提供要初始化的种子数据</summary>
    /// <returns></returns>
    protected override CodeModule[] SeedData(IServiceProvider scopedProvider)
    {
        IRepository<CodeProject, Guid> repository = scopedProvider.GetRequiredService<IRepository<CodeProject, Guid>>();
        CodeProject project = repository.GetFirst(m => m.Name == "示例项目");
        return new[]
        {
            new CodeModule(){Name = "Identity", Display = "身份认证", Order = 1, ProjectId = project.Id},
            new CodeModule(){Name = "Auth", Display = "权限授权", Order = 2, ProjectId = project.Id},
            new CodeModule(){Name = "Infos", Display = "信息", Order = 3, ProjectId = project.Id},
        };
    }

    //...
}
@gmf520 gmf520 added Breaked Changes ⚡ 更新有破坏性,对现有业务实现有较大影响 Feature 🔨 新功能,新特性 labels Apr 13, 2021
@gmf520 gmf520 added this to the v5.0.5 milestone Apr 13, 2021
gmf520 added a commit that referenced this issue Apr 13, 2021
@gmf520 gmf520 added the Finished ✔️ 实现并完工 label Apr 13, 2021
@gmf520 gmf520 closed this as completed Apr 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaked Changes ⚡ 更新有破坏性,对现有业务实现有较大影响 Feature 🔨 新功能,新特性 Finished ✔️ 实现并完工
Projects
None yet
Development

No branches or pull requests

1 participant