更新框架

This commit is contained in:
chfei committed 2026-10-27 14:35:03 +08:00
1 parent ef50fa0620
commit 84b28f9672
72 files changed
+2222 -282

No files matched your search

@@ -1,85 +0,0 @@
using Autofac;
using MAX.Core.DependencyInjection;
using MAX.Core.Helpers;
using MAX.Core.Infrastructure;
using MAX.Core.Reflections;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MAX.Core.Autofac;
public static class AutofaceMiddlewareExtensions
{
public static Bootstrap UseAutofac(this Bootstrap engine)
{
engine.UseMiddleware<AutofaceMiddleware>();
return engine;
}
}
internal class AutofaceMiddleware : IBootstrap
{
private ContainerBuilder _builder;
public void Build()
{
IocManager.Container = _builder.Build();
}
public void Use()
{
_builder = new ContainerBuilder();
IocManager.ContainerBuilder = _builder;
RegisterDependencies(_builder);
}
/// <summary>
/// 注册IOC
/// </summary>
/// <param name="builder"></param>
/// <param name="cnetConfig"></param>
private void RegisterDependencies(ContainerBuilder builder)
{
#region 注册所有IOC接口
IEnumerable<IDependencyRegistrar> ListDependency = ReflectionHelper.GetInstancesByInterface<IDependencyRegistrar>(AppTypeFinder.Assemblies.ToArray());
foreach (IDependencyRegistrar item in ListDependency)
{
item.Register(builder);
}
//ISingletonDependency
Type singtype = typeof(ISingletonDependency);
builder.RegisterAssemblyTypes(AppTypeFinder.Assemblies.ToArray())
.Where(a =>
singtype.IsAssignableFrom(a)
&& a.IsClass
&& !a.IsAbstract
&& !typeof(INoDependency).IsAssignableFrom(a)
)
.AsImplementedInterfaces()
.AsSelf()
.PropertiesAutowired(new CustomPropertySelector())
.SingleInstance();
//ITransientDependency
Type transienttype = typeof(ITransientDependency);
builder.RegisterAssemblyTypes(AppTypeFinder.Assemblies.ToArray())
.Where(a =>
transienttype.IsAssignableFrom(a)
&& a.IsClass
&& !a.IsAbstract
&& !typeof(INoDependency).IsAssignableFrom(a)
)
.AsImplementedInterfaces()
.AsSelf()
.PropertiesAutowired(new CustomPropertySelector())
.InstancePerLifetimeScope();
#endregion
}
}
@@ -0,0 +1,15 @@
using Autofac;
namespace MAX.Core.Infrastructure;
/// <summary>
/// 将通知发布器的服务解析委托给当前 Autofac 作用域,无须引入额外适配包。
/// </summary>
internal sealed class AutofacNotificationServiceProvider(ILifetimeScope scope) : IServiceProvider
{
public object? GetService(Type serviceType)
{
ArgumentNullException.ThrowIfNull(serviceType);
return scope.ResolveOptional(serviceType);
}
}
@@ -1,32 +0,0 @@
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();
}
}
}
@@ -1,7 +0,0 @@
namespace MAX.Core.Infrastructure;
public interface IBootstrap
{
void Use();
void Build();
}
@@ -1,18 +1,15 @@
using MAX.Core.Autofac;
using MediatR;
using Microsoft.Win32;
using MAX.Core.Autofac;
using MAX.Notification;
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
public class MediatorManager : NotificationPublisher
{
private readonly DynamicNotificationHandlerRegistry _registry;
@@ -24,11 +21,11 @@ public class MediatorManager : Mediator
}
}
public MediatorManager(ServiceFactory serviceFactory) : base(serviceFactory)
public MediatorManager(IServiceProvider serviceProvider) : base(serviceProvider)
{
_registry = new DynamicNotificationHandlerRegistry();
}
protected override async Task PublishCore(IEnumerable<Func<INotification, CancellationToken, Task>> allHandlers, INotification notification, CancellationToken cancellationToken)
protected override async Task PublishCore(IEnumerable<NotificationHandlerExecutor> allHandlers, INotification notification, CancellationToken cancellationToken)
{
await base.PublishCore(allHandlers, notification, cancellationToken);
@@ -47,7 +44,7 @@ class DynamicNotificationHandlerRegistry
public DynamicNotificationHandlerRegistry()
{
}
// Key: notification type, Value: 已注册的 handler 调用委托列表
// 按通知类型保存动态注册的处理器调用委托。
private readonly ConcurrentDictionary<Type, List<Func<INotification, CancellationToken, Task>>>
_handlers = new();
@@ -82,7 +79,7 @@ class DynamicNotificationHandlerRegistry
}
}
/// <summary>Publish 时由 PluginAwareMediator 调用</summary>
/// <summary>发布通知时调用动态处理器。</summary>
public async Task Publish(INotification notification, CancellationToken cancellationToken)
{
var notificationType = notification.GetType();
+4 -1
View File
@@ -8,7 +8,10 @@
<ItemGroup>
<PackageReference Include="Autofac" Version="8.3.0" />
<PackageReference Include="MediatR" Version="9.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MAX.Notification\MAX.Notification.csproj" />
</ItemGroup>
</Project>
+8 -21
View File
@@ -1,4 +1,4 @@
/***
/***
* _ooOoo_
* o8888888o
* 88" . "88
@@ -20,7 +20,7 @@
*
* .............................................
* 佛祖保佑 永无BUG
* 快加工资 不改需求
* 快加工资 不改需求
*/
@@ -28,12 +28,8 @@ using Autofac;
using MAX.Core.DependencyInjection;
using MAX.Core.Infrastructure;
using MAX.Core.Reflections;
using MediatR;
using System;
using System.Collections.Generic;
using MAX.Notification;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace MAX.Core;
@@ -50,10 +46,13 @@ public class Startup : IStartup
{
builder.RegisterInstance(LazyServiceProvider.Instance).As<ILazyServiceProvider>().AsSelf().SingleInstance();
#region 注册MediatR
#region 注册通知服务
Assembly[] assemblies = AppTypeFinder.Assemblies.ToArray();
builder.RegisterType<MediatorManager>().As<IMediator>().AsSelf().SingleInstance();
builder.Register(ctx => new MediatorManager(
new AutofacNotificationServiceProvider(ctx.Resolve<ILifetimeScope>())))
.As<IPublisher>().AsSelf().InstancePerLifetimeScope();
builder.RegisterAssemblyTypes(assemblies)
.AsClosedTypesOf(typeof(INotificationHandler<>))
@@ -61,18 +60,6 @@ public class Startup : IStartup
.AsSelf()
.InstancePerLifetimeScope();
builder.RegisterAssemblyTypes(assemblies)
.AsClosedTypesOf(typeof(IRequestHandler<>))
.AsImplementedInterfaces()
.AsSelf()
.InstancePerLifetimeScope();
builder.Register<ServiceFactory>(ctx =>
{
var c = ctx.Resolve<IComponentContext>();
return t => c.Resolve(t);
}).SingleInstance();
#endregion
}
}