添加项目文件。

This commit is contained in:
chfei committed 2026-10-26 18:21:42 +08:00
1 parent 54c7b83029
commit ef50fa0620
84 files changed
+9188

No files matched your search

@@ -0,0 +1,32 @@
using System.Collections.Generic;
namespace MAX.Core.Infrastructure;
public class Bootstrap//Bootstrap
{
public Bootstrap()
{
}
private readonly List<IBootstrap> _listUse = [];
public static Bootstrap Create()
{
return new Bootstrap();
}
public void UseMiddleware<T>() where T : IBootstrap, new()
{
T t = new();
t.Use();
_listUse.Add(t);
}
public void Builder()
{
foreach (IBootstrap item in _listUse)
{
item.Build();
}
}
}
@@ -0,0 +1,7 @@
namespace MAX.Core.Infrastructure;
public interface IBootstrap
{
void Use();
void Build();
}
@@ -0,0 +1,15 @@
using MAX.Core.Autofac;
namespace MAX.Core;
public interface IStartup : IDependencyRegistrar
{
/// <summary>
/// 优先级(优先级越高越先执行)
/// </summary>
int Priority { get; }
/// <summary>
/// 初始化
/// </summary>
void Initialize();
}
@@ -0,0 +1,103 @@
using MAX.Core.Autofac;
using MediatR;
using Microsoft.Win32;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MAX.Core.Infrastructure;
public class MediatorManager : Mediator
{
private readonly DynamicNotificationHandlerRegistry _registry;
public static MediatorManager Instance
{
get
{
return IocManager.Resolve<MediatorManager>();
}
}
public MediatorManager(ServiceFactory serviceFactory) : base(serviceFactory)
{
_registry = new DynamicNotificationHandlerRegistry();
}
protected override async Task PublishCore(IEnumerable<Func<INotification, CancellationToken, Task>> allHandlers, INotification notification, CancellationToken cancellationToken)
{
await base.PublishCore(allHandlers, notification, cancellationToken);
await _registry.Publish(notification, cancellationToken);
}
public void RegisterNotification<TNotification>(INotificationHandler<TNotification> handler) where TNotification : INotification
{
_registry.Register(handler);
}
}
class DynamicNotificationHandlerRegistry
{
public DynamicNotificationHandlerRegistry()
{
}
// Key: notification type, Value: 已注册的 handler 调用委托列表
private readonly ConcurrentDictionary<Type, List<Func<INotification, CancellationToken, Task>>>
_handlers = new();
/// <summary>插件加载时调用,注册 Handler 实例</summary>
public void Register<TNotification>(INotificationHandler<TNotification> handler)
where TNotification : INotification
{
var list = _handlers.GetOrAdd(
typeof(TNotification),
_ => new List<Func<INotification, CancellationToken, Task>>());
lock (list)
{
list.Add((n, ct) => handler.Handle((TNotification)n, ct));
}
}
/// <summary>插件卸载时调用(可选)</summary>
public void Unregister(Assembly pluginAssembly)
{
// 移除该程序集注册的所有 handler(需配合工厂模式追踪)
// 简单实现:重建列表时过滤掉目标程序集的类型
foreach (var key in _handlers.Keys)
{
if (_handlers.TryGetValue(key, out var list))
{
lock (list)
{
list.RemoveAll(f => f.Method.DeclaringType?.Assembly == pluginAssembly);
}
}
}
}
/// <summary>Publish 时由 PluginAwareMediator 调用</summary>
public async Task Publish(INotification notification, CancellationToken cancellationToken)
{
var notificationType = notification.GetType();
if (!_handlers.TryGetValue(notificationType, out var handlers))
return;
List<Func<INotification, CancellationToken, Task>> snapshot;
lock (handlers)
{
snapshot = handlers.ToList(); // 快照,避免遍历时被修改
}
foreach (var handler in snapshot)
{
await handler(notification, cancellationToken);
}
}
}
@@ -0,0 +1,42 @@
using System;
using System.Reflection;
namespace MAX.Core.Infrastructure;
/// <summary>
/// 单例,需要在当前类中定义
/// </summary>
/// <typeparam name="T"></typeparam>
public class SingleInstance<T> where T : class
{
private static readonly object lockObj = new();
private static T mySelf;//= default(T);
public static T Instance
{
get
{
lock (lockObj)
{
mySelf ??= InstanceCreator.CreateInstance<T>();
}
return mySelf;
}
}
}
public static class InstanceCreator
{
public static T CreateInstance<T>()
{
Type type = typeof(T);
try
{
return (T)type.Assembly.CreateInstance(type.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null);
}
catch (MissingMethodException ex)
{
throw new Exception($"{ex.Message}\n(单例模式下,构造函数必须为 private)");
}
}
}