在前面我們基本把應用框架的基礎設施搭建完成。接下來我們就得著手處理一下種子資料的問題。
在一個基礎框架裡面,種子資料很重要,比如一些基礎資料,初始使用者等等,這些都需要初始化,否則程式啟動卻無法使用就很尷尬了。
首先定義一個種子資料介面
using Wheel.DependencyInjection;
namespace Wheel.DataSeeders
{
public interface IDataSeeder : ITransientDependency
{
Task Seed(CancellationToken cancellationToken = default);
}
}
接下來所有的種子資料實現都需要繼承這個介面。
封裝一個擴充套件方法,獲取所有IDataSeeder的實現,並執行資料初始化。
namespace Wheel.DataSeeders
{
public static class DataSeederExtensions
{
public static async Task<IApplicationBuilder> SeedData(this IApplicationBuilder app)
{
var dataSeeders = app.ApplicationServices.GetServices<IDataSeeder>();
foreach (var dataSeeder in dataSeeders)
{
await dataSeeder.Seed();
}
return app;
}
}
}
在Program中新增程式碼
var app = builder.Build();
//初始化種子資訊
await app.SeedData();
這樣就初步完成了種子資料的設定。
IdentityDataSeeder
using Microsoft.AspNetCore.Identity;
using Wheel.Domain;
using Wheel.Domain.Identity;
namespace Wheel.DataSeeders.Identity
{
public class IdentityDataSeeder : IDataSeeder
{
private readonly IBasicRepository<Role, string> _roleRepository;
private readonly IBasicRepository<User, string> _userRepository;
private readonly UserManager<User> _userManager;
private readonly IUserStore<User> _userStore;
private readonly RoleManager<Role> _roleManager;
public IdentityDataSeeder(IBasicRepository<Role, string> roleRepository, IBasicRepository<User, string> userRepository, UserManager<User> userManager, IUserStore<User> userStore, RoleManager<Role> roleManager)
{
_roleRepository = roleRepository;
_userRepository = userRepository;
_userManager = userManager;
_userStore = userStore;
_roleManager = roleManager;
}
public async Task Seed(CancellationToken cancellationToken = default)
{
if (!await _roleRepository.AnyAsync(a => a.Name == "admin"))
{
await _roleManager.CreateAsync(new Role("admin", Enums.RoleType.Admin));
}
if (!await _roleRepository.AnyAsync(a => a.Name == "user"))
{
await _roleManager.CreateAsync(new Role("user", Enums.RoleType.App));
}
if (!await _userRepository.AnyAsync(a => a.UserName == "admin"))
{
var adminUser = new User();
await _userStore.SetUserNameAsync(adminUser, "admin", cancellationToken);
var emailStore = (IUserEmailStore<User>)_userStore;
await emailStore.SetEmailAsync(adminUser, "[email protected]", cancellationToken);
await _userManager.CreateAsync(adminUser, "Wheel@2023");
await _userManager.AddToRoleAsync(adminUser, "admin");
await _userManager.UpdateAsync(adminUser);
}
}
}
}
這裡初始化一個普通User角色和管理後臺admin角色,以及一個admin角色的賬號。
LocalizationDataSeeder
using Wheel.Domain;
using Wheel.Domain.Localization;
namespace Wheel.DataSeeders.Localization
{
public class LocalizationDataSeeder : IDataSeeder
{
private readonly IBasicRepository<LocalizationCulture, int> _localizationCultureRepository;
public LocalizationDataSeeder(IBasicRepository<LocalizationCulture, int> localizationCultureRepository)
{
_localizationCultureRepository = localizationCultureRepository;
}
public async Task Seed(CancellationToken cancellationToken = default)
{
if (!(await _localizationCultureRepository.AnyAsync(cancellationToken)))
{
await _localizationCultureRepository.InsertAsync(new LocalizationCulture() { Name = "en" }, true);
await _localizationCultureRepository.InsertAsync(new LocalizationCulture() { Name = "zh-CN"}, true);
}
}
}
}
MenuDataSeeder
using Wheel.Domain;
using Wheel.Domain.Menus;
namespace Wheel.DataSeeders.Identity
{
public class MenuDataSeeder : IDataSeeder
{
private readonly IBasicRepository<Menu, Guid> _menuRepository;
public MenuDataSeeder(IBasicRepository<Menu, Guid> menuRepository)
{
_menuRepository = menuRepository;
}
public async Task Seed(CancellationToken cancellationToken = default)
{
if (!(await _menuRepository.AnyAsync(cancellationToken)))
{
await _menuRepository.InsertAsync(new Menu
{
Name = "SystemManage",
DisplayName = "系統管理",
Sort = 99,
Id = Guid.NewGuid(),
Icon = "SettingOutlined",
Path = "/System",
MenuType = Enums.MenuType.Menu,
Children = new List<Menu>
{
new Menu
{
Name = "UserManage",
DisplayName = "使用者管理",
Sort = 0,
Id = Guid.NewGuid(),
Path = "/System/User",
MenuType = Enums.MenuType.Page
},
new Menu
{
Name = "RoleManage",
DisplayName = "角色管理",
Sort = 1,
Id = Guid.NewGuid(),
Path = "/System/Role",
MenuType = Enums.MenuType.Page,
},
new Menu
{
Name = "PermissionManage",
DisplayName = "許可權管理",
Sort = 2,
Id = Guid.NewGuid(),
Path = "/System/Permission",
MenuType = Enums.MenuType.Page
},
new Menu
{
Name = "MenuManage",
DisplayName = "選單管理",
Sort = 3,
Id = Guid.NewGuid(),
Path = "/System/Menu",
MenuType = Enums.MenuType.Page
},
new Menu
{
Name = "LocalizationManage",
DisplayName = "多語言管理",
Sort = 4,
Id = Guid.NewGuid(),
Path = "/System/Localization",
MenuType = Enums.MenuType.Page
},
}
}, true, cancellationToken: cancellationToken);
}
}
}
}
這裡選單初始化基礎管理後臺頁面所需的選單。
啟動程式後,開啟資料庫
可以看到資料初始化成功。
這樣就輕輕鬆鬆完成了我們種子資料的實現。
輪子倉庫地址https://github.com/Wheel-Framework/Wheel
歡迎進群催更。