模型注册
This commit is contained in:
1 parent
75d0e85218
commit
0e1620c1f9
11 files changed
+399
-41
No files matched your search
@@ -1,12 +1,37 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
|
||||
namespace MAX.Framework.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// 业务模块:把「服务注册」和「管道装配」两件事都交给模块自己,
|
||||
/// 由宿主的两行代码统一驱动(模块不需要出现在 Program.cs 里):
|
||||
/// <list type="number">
|
||||
/// <item><c>builder.AddStoreHub()</c> → 自动发现所有模块并执行 <see cref="AddModule"/>(服务注册阶段,<c>Build()</c> 之前);</item>
|
||||
/// <item><c>app.UseStoreHubModules()</c> → 自动执行 <see cref="UseModule"/>(管道阶段,<c>Build()</c> 之后、<c>Run()</c> 之前)。</item>
|
||||
/// </list>
|
||||
/// 模块实例由反射创建,因此实现类必须有无参构造函数。
|
||||
/// </summary>
|
||||
public interface IModule
|
||||
{
|
||||
void AddModule(IServiceCollection services);
|
||||
/// <summary>
|
||||
/// 模块执行顺序(数值越大越先执行),默认 <see cref="MAX.Core.Interfaces.IntPriority.Normal"/>。
|
||||
/// 两个阶段都按这个顺序执行。
|
||||
/// </summary>
|
||||
int Priority => MAX.Core.Interfaces.IntPriority.Normal;
|
||||
|
||||
/// <summary>
|
||||
/// 服务注册阶段(<c>builder.Build()</c> 之前):注册本模块的服务、选项、后台任务类型。
|
||||
/// 需要读配置时用 <see cref="ModuleContext.Configuration"/>,不要再去碰 <c>builder</c>。
|
||||
/// </summary>
|
||||
void AddModule(ModuleContext context);
|
||||
|
||||
/// <summary>
|
||||
/// 管道装配阶段(<c>builder.Build()</c> 之后、<c>app.Run()</c> 之前):
|
||||
/// 注册中间件(如 Hangfire 看板)、Map 端点、读取 <see cref="WebApplication.Services"/> 解析服务、注册启动任务等。
|
||||
/// 不需要管道装配的模块可以不实现(默认空实现)。
|
||||
/// </summary>
|
||||
void UseModule(WebApplication app)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using MAX.Core.Interfaces;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
|
||||
namespace MAX.Framework.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// 模块基类:给出 <see cref="IModule.Priority"/> 与 <see cref="IModule.UseModule"/> 的默认实现,
|
||||
/// 模块只需重写自己真正关心的阶段(只有服务注册时只写 <see cref="AddModule"/>)。
|
||||
/// </summary>
|
||||
public abstract class ModuleBase : IModule
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public virtual int Priority => IntPriority.Normal;
|
||||
|
||||
/// <inheritdoc />
|
||||
public abstract void AddModule(ModuleContext context);
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void UseModule(WebApplication app)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using MAX.Core.DependencyInjection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace MAX.Framework.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// 模块上下文:模块在「服务注册阶段」能拿到的全部宿主信息。
|
||||
/// 之所以包成一个对象,是为了以后要再补宿主的其他东西(日志、配置节、HostApplicationBuilder 等)时不用改接口签名。
|
||||
/// </summary>
|
||||
public sealed class ModuleContext
|
||||
{
|
||||
public ModuleContext(
|
||||
IServiceCollection services,
|
||||
IServiceRegistrar registrar,
|
||||
IConfiguration configuration,
|
||||
IHostEnvironment? environment = null)
|
||||
{
|
||||
Services = services;
|
||||
Registrar = registrar;
|
||||
Configuration = configuration;
|
||||
Environment = environment;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 服务集合(Microsoft.Extensions.DependencyInjection),等价于宿主里的 <c>builder.Services</c>
|
||||
/// </summary>
|
||||
public IServiceCollection Services { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 容器无关的注册器,等价于 <see cref="IStartup.Register"/> 里拿到的那个,便于以后换容器
|
||||
/// </summary>
|
||||
public IServiceRegistrar Registrar { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 应用配置,等价于宿主的 <c>builder.Configuration</c>(永不为 null;走旧的无参入口时是空配置)
|
||||
/// </summary>
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 宿主环境,等价于宿主的 <c>builder.Environment</c>;走旧的无参入口时为 null
|
||||
/// </summary>
|
||||
public IHostEnvironment? Environment { get; }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user