Files
StoreWeb/Core/MAX.Plugins/PluginBase.cs
T

33 lines
1.3 KiB
C#

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace MAX.Plugins;
public abstract class PluginBase : IPlugin
{
public abstract string Id { get; }
public virtual int Priority => 0;
protected PluginContext Context { get; private set; } = null!;
public void ConfigureServices(IServiceCollection services, PluginContext context)
{
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(context);
if (!string.Equals(Id, context.Id, StringComparison.Ordinal))
throw new ArgumentException("插件上下文标识不匹配。", nameof(context));
if (Context is not null) throw new InvalidOperationException($"插件 {Id} 已注册。");
Context = context;
RegisterServices(services);
}
public void Configure(IApplicationBuilder app, PluginContext context)
{
if (!ReferenceEquals(Context, context))
throw new InvalidOperationException($"插件 {Id} 尚未注册或上下文不匹配。");
ConfigureApplication(app);
}
protected abstract void RegisterServices(IServiceCollection services);
protected virtual void ConfigureApplication(IApplicationBuilder app) { }
protected string RequireConfiguration(string key) => Context.RequireConfiguration(key);
}