这是之前推荐过的插件框架,一直以来比较多人感兴趣和咨询,有没有类似框架,今天重新整理发下。
今天给大家推荐一个开源插件架构。在介绍项目之前,我们了解下什么是插件架构,它的用处。
现有的软件开发中,业务越来越复杂,一些大型的项目版本一直在迭代,代码规模越来越大,涉及的人员也越来越多,为了降低项目的耦合度,减少开发的难度,插件框架就是其中一个解决方案。
使用插件架构可以实现并行开发、降低项目开发复杂度、有利于项目进度的把控。
01
项目简介
这是一个基于.Net Core 的 轻量级插件框架,集成非常简单,两行代码完成集成;支持新增Controller,动态路由 ;热插拔、易扩展、无需数据库。
02
技术架构
1、跨平台:这是基于.Net Core开发的系统,可以部署在Docker, Windows, Linux, Mac。
2、 运行环境: .NET Core 3.1 (+)
3、开发环境: Visual Studio Community 2019
03
项目结构
04
系统界面截图
插件列表、插件管理、插件配置
系统集成
1、NuGet安装插件
PM> Install-Package PluginCore.AspNetCore
2、Startup.cs 配置
using PluginCore.AspNetCore.Extensions;
// This method gets called by the runtime. Use this method to add services to the container.
publicvoidConfigureServices(IServiceCollection services)
{
services.AddControllers();
// 1. 添加 PluginCore
services.AddPluginCore();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
publicvoidConfigure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
// 2. 使用 PluginCore
app.UsePluginCore();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
3、访问 https://localhost/PluginCore/Admin,就可以进入管理后台。
插件开发
1、Nuget引入插件
Install-PackagePluginCore.IPlugins.AspNetCore
2、创建插件
添加 HelloWorldPlugin 类 继承 BasePlugin
public classHelloWorldPlugin : BasePlugin, IStartupXPlugin{
publicoverride (bool IsSuccess, string Message) AfterEnable()
{
Console.WriteLine($"{nameof(HelloWorldPlugin)}: {nameof(AfterEnable)}");
returnbase.AfterEnable();
}
publicoverride (bool IsSuccess, string Message) BeforeDisable()
{
Console.WriteLine($"{nameof(HelloWorldPlugin)}: {nameof(BeforeDisable)}");
returnbase.BeforeDisable();
}
publicvoidConfigureServices(IServiceCollection services)
{
}
publicvoidConfigure(IApplicationBuilder app)
{
app.UseMiddleware<SayHelloMiddleware>();
}
publicint ConfigureOrder {
get
{
return2;
}
}
publicint ConfigureServicesOrder {
get
{
return2;
}
}}
3、插件其他配置
动 态路由配置
[Route("api/plugins/[controller]")][ApiController]public classUserHelloController : ControllerBase{
privatereadonly IUserInfoService _userInfoService;
publicUserHelloController(IUserInfoService userInfoService)
{
this._userInfoService = userInfoService;
}
public ActionResult Get()
{
UserInfo userInfo = _userInfoService.FirstOrDefaultAsync(m => !m.IsDeleted).Result;
SettingsModel settingsModel = PluginSettingsModelFactory.Create<SettingsModel>("HelloWorldPlugin");
string rtn = $"用户名: {userInfo.UserName}, 创建时间: {userInfo.CreateTime.ToString()}, Hello: {settingsModel.Hello}";
return Ok(rtn);
}}
05
项目地址
https://github.com/yiyungent/PluginCore
最后推荐加入我的 , 带你从零学习:三层架构与领域驱动设计架构 !
- End -
分享一套.NetCore从入门到精通视频教程
点击下方公众号卡片,关注我
回复「 888 」,免费领取
推荐阅读
觉得好看 点个在看👇