Abp自定义模块种子数据
模块的初始化或者系统的基本运行需要一些基础数据,可以利用ABP提供的种子数据基础上设置进行数据播种。
自定义模块
自定义模块可以定义自己的 DataSeeder, 例如数据字典 :
public interface IDataDictionaryDataSeeder {
Task SeedAsync(string name, string value);
}
定义接口实现
public class DataDictionaryDataSeeder : IDataDictionaryDataSeeder, ITransientDependency
{
public Task SeedAsync(string name, string value){}
}
定义好模块的种子数据后, 后续的处理有另种方案,但最终都是利用ABP基础设施中的 IDataSeedContributor
如果是一个大模块的技术数据播种, 可以参考ABP的 IDataSeedContributor, 自定义数据的贡献者, 自定义贡献者中注入自定义的模块功能 IDataDictionaryDataSeeder
public interface INavigationSeedContributor
{
Task SeedAsync(NavigationSeedContext context);
}
将自定义的贡献者注册到模块功能Options中
public class AbpNavigationOptions
{
public ITypeList<INavigationDefinitionProvider> DefinitionProviders { get; }
public ITypeList<INavigationSeedContributor> NavigationSeedContributors { get; }
public AbpNavigationOptions()
{
DefinitionProviders = new TypeList<INavigationDefinitionProvider>();
NavigationSeedContributors = new TypeList<INavigationSeedContributor>();
}
}
最终实现
public class NavigationDataSeedContributor : IDataSeedContributor, ITransientDependency
{
private readonly IServiceProvider _serviceProvider;
private readonly AbpNavigationOptions _options;
public List<INavigationSeedContributor> Contributors => _lazyContributors.Value;
private readonly Lazy<List<INavigationSeedContributor>> _lazyContributors;
public NavigationDataSeedContributor(
IServiceProvider serviceProvider,
IOptions<AbpNavigationOptions> options)
{
_serviceProvider = serviceProvider;
_options = options.Value;
_lazyContributors = new Lazy<List<INavigationSeedContributor>>(CreateContributors);
}
public async virtual Task SeedAsync(DataSeedContext context)
{
var seedContext = new NavigationSeedContext(menus, multiTenancySides);
foreach (var contributor in Contributors)
{
await contributor.SeedAsync(seedContext);
}
}
private List<INavigationSeedContributor> CreateContributors()
{
return _options
.NavigationSeedContributors
.Select(type => _serviceProvider.GetRequiredService(type) as INavigationSeedContributor)
.ToList();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!