今天给大家推荐一个开源插件架构。在介绍项目之前,我们了解下什么是插件架构,它的用处。
现有的软件开发中,业务越来越复杂,一些大型的项目版本一直在迭代,代码规模越来越大,涉及的人员也越来越多,为了降低项目的耦合度,减少开发的难度,插件框架就是其中一个解决方案。
使用插件架构可以实现并行开发、降低项目开发复杂度、有利于项目进度的把控。
项目简介
这是一个基于.Net Core的轻量级插件框架,集成非常简单,两行代码完成集成;支持新增Controller,动态路由;热插拔、易扩展、无需数据库。
技术架构
1、跨平台:这是基于.Net Core开发的系统,可以部署在Docker, Windows, Linux, Mac。
2、运行环境: .NET Core 3.1 (+)
3、开发环境: Visual Studio Community 2019
项目结构
**后台管理系统
**
插件列表、插件管理、插件配置
**系统集成
**
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.
public void ConfigureServices(IServiceCollection services)
{services.AddControllers();// 1. 添加 PluginCoreservices.AddPluginCore();
}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseHttpsRedirection();app.UseRouting();// 2. 使用 PluginCoreapp.UsePluginCore();app.UseAuthorization();app.UseEndpoints(endpoints =>{endpoints.MapControllers();});
}
3、访问 https://localhost/PluginCore/Admin,就可以进入管理后台。
插件开发
1、Nuget引入插件
Install-Package PluginCore.IPlugins.AspNetCore
2、创建插件
添加 HelloWorldPlugin 类 继承 BasePlugin
public class HelloWorldPlugin : BasePlugin, IStartupXPlugin{public override (bool IsSuccess, string Message) AfterEnable(){Console.WriteLine($"{nameof(HelloWorldPlugin)}: {nameof(AfterEnable)}");return base.AfterEnable();}public override (bool IsSuccess, string Message) BeforeDisable(){Console.WriteLine($"{nameof(HelloWorldPlugin)}: {nameof(BeforeDisable)}");return base.BeforeDisable();}public void ConfigureServices(IServiceCollection services){}public void Configure(IApplicationBuilder app){app.UseMiddleware<SayHelloMiddleware>();}public int ConfigureOrder {get{return 2;}}public int ConfigureServicesOrder {get{return 2;}}}
3、插件其他配置
动态路由配置
[Route("api/plugins/[controller]")][ApiController]public class UserHelloController : ControllerBase{private readonly IUserInfoService _userInfoService;public UserHelloController(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);}}
项目地址:https://github.com/yiyungent/PluginCore
- End -
推荐阅读
-
最新.NetCore/C#全栈视频教程入门教程
-
一个基于.Net+Vue开发的开源权限工作流系统
-
推荐一个不到2MB的C#开发工具箱,集成了上千个常用操作类
-
推荐一个基于.NET Core 3.1开发开源的分布式任务调度系统
-
.Net服务器性能监控,应用耗时统一监控平台
专注分享编程知识、热门有用有趣的开源项目