mirror of
https://github.com/nsnail/NetAdmin.git
synced 2025-04-19 21:02:49 +08:00
perf: ⚡ 事件总线精简优化
This commit is contained in:
parent
c9e570f11d
commit
e82a172598
@ -32,7 +32,6 @@ global using Gurion.DataEncryption;
|
|||||||
global using Gurion.DataValidation;
|
global using Gurion.DataValidation;
|
||||||
global using Gurion.DependencyInjection;
|
global using Gurion.DependencyInjection;
|
||||||
global using Gurion.DynamicApiController;
|
global using Gurion.DynamicApiController;
|
||||||
global using Gurion.EventBus;
|
|
||||||
global using Gurion.SpecificationDocument;
|
global using Gurion.SpecificationDocument;
|
||||||
global using Gurion.UnifyResult;
|
global using Gurion.UnifyResult;
|
||||||
global using Mapster;
|
global using Mapster;
|
||||||
@ -58,6 +57,7 @@ global using NetAdmin.Infrastructure.Configuration.Options.SubNodes.Redis;
|
|||||||
global using NetAdmin.Infrastructure.Configuration.Options.SubNodes.Upload;
|
global using NetAdmin.Infrastructure.Configuration.Options.SubNodes.Upload;
|
||||||
global using NetAdmin.Infrastructure.Constant;
|
global using NetAdmin.Infrastructure.Constant;
|
||||||
global using NetAdmin.Infrastructure.Enums;
|
global using NetAdmin.Infrastructure.Enums;
|
||||||
|
global using NetAdmin.Infrastructure.EventBus;
|
||||||
global using NetAdmin.Infrastructure.Exceptions;
|
global using NetAdmin.Infrastructure.Exceptions;
|
||||||
global using NetAdmin.Infrastructure.Extensions;
|
global using NetAdmin.Infrastructure.Extensions;
|
||||||
global using NetAdmin.Infrastructure.Languages;
|
global using NetAdmin.Infrastructure.Languages;
|
||||||
|
@ -15,11 +15,6 @@ public sealed record CacheStatisticsRsp : DataAbstraction
|
|||||||
, new("redis_version:(.+)", RegexOptions.Compiled) //
|
, new("redis_version:(.+)", RegexOptions.Compiled) //
|
||||||
];
|
];
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="CacheStatisticsRsp" /> class.
|
|
||||||
/// </summary>
|
|
||||||
public CacheStatisticsRsp() { }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="CacheStatisticsRsp" /> class.
|
/// Initializes a new instance of the <see cref="CacheStatisticsRsp" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -7,11 +7,6 @@ namespace NetAdmin.Domain.Dto.Sys.Cache;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed record GetEntryRsp : DataAbstraction
|
public sealed record GetEntryRsp : DataAbstraction
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="GetEntryRsp" /> class.
|
|
||||||
/// </summary>
|
|
||||||
public GetEntryRsp() { }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 缓存值
|
/// 缓存值
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -7,11 +7,6 @@ namespace NetAdmin.Domain.Dto.Sys.Menu;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed record MetaInfo : DataAbstraction
|
public sealed record MetaInfo : DataAbstraction
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="MetaInfo" /> class.
|
|
||||||
/// </summary>
|
|
||||||
public MetaInfo() { }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="MetaInfo" /> class.
|
/// Initializes a new instance of the <see cref="MetaInfo" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
21
src/backend/NetAdmin/NetAdmin.Domain/Events/EventData.cs
Normal file
21
src/backend/NetAdmin/NetAdmin.Domain/Events/EventData.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
namespace NetAdmin.Domain.Events;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 事件数据
|
||||||
|
/// </summary>
|
||||||
|
public abstract record EventData<T> : DataAbstraction, IEventData<T>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="EventData{T}" /> class.
|
||||||
|
/// </summary>
|
||||||
|
protected EventData(T payLoad)
|
||||||
|
{
|
||||||
|
PayLoad = payLoad;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public DateTime CreatedTime { get; init; } = DateTime.Now;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public T PayLoad { get; init; }
|
||||||
|
}
|
@ -1,12 +0,0 @@
|
|||||||
namespace NetAdmin.Domain.Events;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 泛型事件源接口
|
|
||||||
/// </summary>
|
|
||||||
public interface IEventSourceGeneric<out T> : IEventSource
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 事件承载(携带)数据
|
|
||||||
/// </summary>
|
|
||||||
T Data { get; }
|
|
||||||
}
|
|
@ -3,36 +3,9 @@ namespace NetAdmin.Domain.Events;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 种子数据插入完毕事件
|
/// 种子数据插入完毕事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed record SeedDataInsertedEvent : DataAbstraction, IEventSource
|
public sealed record SeedDataInsertedEvent : EventData<int>
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="SeedDataInsertedEvent" /> class.
|
|
||||||
/// </summary>
|
|
||||||
public SeedDataInsertedEvent(int insertedCount, bool isConsumeOnce = false)
|
|
||||||
{
|
|
||||||
IsConsumeOnce = isConsumeOnce;
|
|
||||||
InsertedCount = insertedCount;
|
|
||||||
CreatedTime = DateTime.Now;
|
|
||||||
EventId = nameof(SeedDataInsertedEvent);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public DateTime CreatedTime { get; }
|
public SeedDataInsertedEvent(int payLoad) //
|
||||||
|
: base(payLoad) { }
|
||||||
/// <inheritdoc />
|
|
||||||
public string EventId { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public bool IsConsumeOnce { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public CancellationToken CancellationToken { get; init; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 插入数量
|
|
||||||
/// </summary>
|
|
||||||
public int InsertedCount { get; set; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public object Payload { get; init; }
|
|
||||||
}
|
}
|
@ -12,7 +12,6 @@ public sealed record SqlCommandAfterEvent : SqlCommandBeforeEvent
|
|||||||
: base(e)
|
: base(e)
|
||||||
{
|
{
|
||||||
ElapsedMilliseconds = (long)((double)e.ElapsedTicks / Stopwatch.Frequency * 1_000);
|
ElapsedMilliseconds = (long)((double)e.ElapsedTicks / Stopwatch.Frequency * 1_000);
|
||||||
EventId = nameof(SqlCommandAfterEvent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -24,6 +23,6 @@ public sealed record SqlCommandAfterEvent : SqlCommandBeforeEvent
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return string.Format(CultureInfo.InvariantCulture, "SQL-{0}: {2} ms {1}", Id, Sql, ElapsedMilliseconds);
|
return string.Format(CultureInfo.InvariantCulture, "SQL-{0}: {2} ms {1}", Id, PayLoad, ElapsedMilliseconds);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,13 +8,8 @@ public record SqlCommandBeforeEvent : SqlCommandEvent
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="SqlCommandBeforeEvent" /> class.
|
/// Initializes a new instance of the <see cref="SqlCommandBeforeEvent" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SqlCommandBeforeEvent(CommandBeforeEventArgs e)
|
public SqlCommandBeforeEvent(CommandBeforeEventArgs e) //
|
||||||
{
|
: base(e.Command.ParameterFormat().RemoveWrapped(), e.Identifier) { }
|
||||||
Identifier = e.Identifier;
|
|
||||||
Sql = e.Command.ParameterFormat().RemoveWrapped();
|
|
||||||
EventId = nameof(SqlCommandBeforeEvent);
|
|
||||||
CreatedTime = DateTime.Now;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
|
@ -3,31 +3,17 @@ namespace NetAdmin.Domain.Events;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sql命令事件
|
/// Sql命令事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract record SqlCommandEvent : DataAbstraction, IEventSource
|
public abstract record SqlCommandEvent : EventData<string>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="SqlCommandEvent" /> class.
|
/// Initializes a new instance of the <see cref="SqlCommandEvent" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected SqlCommandEvent(bool isConsumeOnce = false)
|
protected SqlCommandEvent(string payLoad, Guid identifier) //
|
||||||
|
: base(payLoad)
|
||||||
{
|
{
|
||||||
IsConsumeOnce = isConsumeOnce;
|
Identifier = identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public bool IsConsumeOnce { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public CancellationToken CancellationToken { get; init; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public DateTime CreatedTime { get; protected init; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public string EventId { get; protected init; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public object Payload { get; init; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 标识符缩写
|
/// 标识符缩写
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -37,9 +23,4 @@ public abstract record SqlCommandEvent : DataAbstraction, IEventSource
|
|||||||
/// 标识符,可将 CommandBefore 与 CommandAfter 进行匹配
|
/// 标识符,可将 CommandBefore 与 CommandAfter 进行匹配
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected Guid Identifier { get; init; }
|
protected Guid Identifier { get; init; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 关联的Sql语句
|
|
||||||
/// </summary>
|
|
||||||
protected string Sql { get; init; }
|
|
||||||
}
|
}
|
@ -9,10 +9,7 @@ public sealed record SyncStructureAfterEvent : SyncStructureBeforeEvent
|
|||||||
/// Initializes a new instance of the <see cref="SyncStructureAfterEvent" /> class.
|
/// Initializes a new instance of the <see cref="SyncStructureAfterEvent" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SyncStructureAfterEvent(SyncStructureBeforeEventArgs e) //
|
public SyncStructureAfterEvent(SyncStructureBeforeEventArgs e) //
|
||||||
: base(e)
|
: base(e) { }
|
||||||
{
|
|
||||||
EventId = nameof(SyncStructureAfterEvent);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
|
@ -8,11 +8,9 @@ public record SyncStructureBeforeEvent : SqlCommandEvent
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="SyncStructureBeforeEvent" /> class.
|
/// Initializes a new instance of the <see cref="SyncStructureBeforeEvent" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SyncStructureBeforeEvent(SyncStructureBeforeEventArgs e)
|
public SyncStructureBeforeEvent(SyncStructureBeforeEventArgs e) //
|
||||||
|
: base(null, e.Identifier)
|
||||||
{
|
{
|
||||||
Identifier = e.Identifier;
|
|
||||||
EventId = nameof(SyncStructureBeforeEvent);
|
|
||||||
CreatedTime = DateTime.Now;
|
|
||||||
EntityTypes = e.EntityTypes;
|
EntityTypes = e.EntityTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,36 +5,9 @@ namespace NetAdmin.Domain.Events.Sys;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 请求日志事件
|
/// 请求日志事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed record RequestLogEvent : DataAbstraction, IEventSourceGeneric<CreateRequestLogReq>
|
public sealed record RequestLogEvent : EventData<CreateRequestLogReq>
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="RequestLogEvent" /> class.
|
|
||||||
/// </summary>
|
|
||||||
public RequestLogEvent(CreateRequestLogReq data, bool isConsumeOnce = false, object payload = null, DateTime createdTime = default
|
|
||||||
, CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
Data = data;
|
|
||||||
IsConsumeOnce = isConsumeOnce;
|
|
||||||
Payload = payload;
|
|
||||||
CreatedTime = createdTime;
|
|
||||||
CancellationToken = cancellationToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public CancellationToken CancellationToken { get; }
|
public RequestLogEvent(CreateRequestLogReq payLoad) //
|
||||||
|
: base(payLoad) { }
|
||||||
/// <inheritdoc />
|
|
||||||
public DateTime CreatedTime { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public CreateRequestLogReq Data { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public string EventId => nameof(RequestLogEvent);
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public bool IsConsumeOnce { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public object Payload { get; }
|
|
||||||
}
|
}
|
@ -5,36 +5,9 @@ namespace NetAdmin.Domain.Events.Sys;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户被创建事件
|
/// 用户被创建事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed record UserCreatedEvent : DataAbstraction, IEventSourceGeneric<UserInfoRsp>
|
public sealed record UserCreatedEvent : EventData<UserInfoRsp>
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="UserCreatedEvent" /> class.
|
|
||||||
/// </summary>
|
|
||||||
public UserCreatedEvent(UserInfoRsp data, DateTime createdTime = default, bool isConsumeOnce = false, object payload = null
|
|
||||||
, CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
Data = data;
|
|
||||||
CancellationToken = cancellationToken;
|
|
||||||
CreatedTime = createdTime;
|
|
||||||
IsConsumeOnce = isConsumeOnce;
|
|
||||||
Payload = payload;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public CancellationToken CancellationToken { get; }
|
public UserCreatedEvent(UserInfoRsp payLoad) //
|
||||||
|
: base(payLoad) { }
|
||||||
/// <inheritdoc />
|
|
||||||
public DateTime CreatedTime { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public UserInfoRsp Data { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public string EventId => nameof(UserCreatedEvent);
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public bool IsConsumeOnce { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public object Payload { get; }
|
|
||||||
}
|
}
|
@ -5,36 +5,9 @@ namespace NetAdmin.Domain.Events.Sys;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户被更新事件
|
/// 用户被更新事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed record UserUpdatedEvent : DataAbstraction, IEventSourceGeneric<UserInfoRsp>
|
public sealed record UserUpdatedEvent : EventData<UserInfoRsp>
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="UserUpdatedEvent" /> class.
|
|
||||||
/// </summary>
|
|
||||||
public UserUpdatedEvent(UserInfoRsp data, DateTime createdTime = default, bool isConsumeOnce = false, object payload = null
|
|
||||||
, CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
Data = data;
|
|
||||||
CancellationToken = cancellationToken;
|
|
||||||
CreatedTime = createdTime;
|
|
||||||
IsConsumeOnce = isConsumeOnce;
|
|
||||||
Payload = payload;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public CancellationToken CancellationToken { get; }
|
public UserUpdatedEvent(UserInfoRsp payLoad) //
|
||||||
|
: base(payLoad) { }
|
||||||
/// <inheritdoc />
|
|
||||||
public DateTime CreatedTime { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public UserInfoRsp Data { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public string EventId => nameof(UserUpdatedEvent);
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public bool IsConsumeOnce { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public object Payload { get; }
|
|
||||||
}
|
}
|
@ -5,36 +5,9 @@ namespace NetAdmin.Domain.Events.Sys;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 验证码创建事件
|
/// 验证码创建事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed record VerifyCodeCreatedEvent : DataAbstraction, IEventSourceGeneric<QueryVerifyCodeRsp>
|
public sealed record VerifyCodeCreatedEvent : EventData<QueryVerifyCodeRsp>
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="VerifyCodeCreatedEvent" /> class.
|
|
||||||
/// </summary>
|
|
||||||
public VerifyCodeCreatedEvent(QueryVerifyCodeRsp data, DateTime createdTime = default, bool isConsumeOnce = false, object payload = null
|
|
||||||
, CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
Data = data;
|
|
||||||
CancellationToken = cancellationToken;
|
|
||||||
CreatedTime = createdTime;
|
|
||||||
IsConsumeOnce = isConsumeOnce;
|
|
||||||
Payload = payload;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public CancellationToken CancellationToken { get; }
|
public VerifyCodeCreatedEvent(QueryVerifyCodeRsp payLoad) //
|
||||||
|
: base(payLoad) { }
|
||||||
/// <inheritdoc />
|
|
||||||
public DateTime CreatedTime { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public QueryVerifyCodeRsp Data { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public string EventId => nameof(VerifyCodeCreatedEvent);
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public bool IsConsumeOnce { get; }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public object Payload { get; }
|
|
||||||
}
|
}
|
@ -4,7 +4,7 @@
|
|||||||
<ProjectReference Include="../NetAdmin.Infrastructure/NetAdmin.Infrastructure.csproj"/>
|
<ProjectReference Include="../NetAdmin.Infrastructure/NetAdmin.Infrastructure.csproj"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CronExpressionDescriptor" Version="2.36.0"/>
|
<PackageReference Include="CronExpressionDescriptor" Version="2.38.0"/>
|
||||||
<PackageReference Include="Cronos" Version="0.9.0"/>
|
<PackageReference Include="Cronos" Version="0.9.0"/>
|
||||||
<PackageReference Include="NetAdmin.CsvHelper" Version="1.0.0"/>
|
<PackageReference Include="NetAdmin.CsvHelper" Version="1.0.0"/>
|
||||||
<PackageReference Include="Yitter.IdGenerator" Version="1.0.14"/>
|
<PackageReference Include="Yitter.IdGenerator" Version="1.0.14"/>
|
||||||
|
@ -133,7 +133,11 @@ public static class ServiceCollectionExtensions
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static IServiceCollection AddEventBus(this IServiceCollection me)
|
public static IServiceCollection AddEventBus(this IServiceCollection me)
|
||||||
{
|
{
|
||||||
return me.AddEventBus(builder => builder.AddSubscribers(App.Assemblies.ToArray()));
|
foreach (var type in App.EffectiveTypes.Where(x => typeof(IEventSubscriber).IsAssignableFrom(x) && x.IsClass && !x.IsAbstract)) {
|
||||||
|
_ = me.AddSingleton(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
return me.AddSingleton<IEventPublisher>(new DefaultEventPublisher());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -10,55 +10,50 @@ public sealed class SqlProfiler(ILogger<SqlProfiler> logger) : IEventSubscriber
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sql命令执行后
|
/// Sql命令执行后
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[EventSubscribe(nameof(SqlCommandAfterEvent))]
|
[EventSubscribe]
|
||||||
public Task CommandAfterAsync(EventHandlerExecutingContext context)
|
public Task CommandAfterAsync(SqlCommandAfterEvent @event)
|
||||||
{
|
{
|
||||||
var source = context.Source as SqlCommandAfterEvent;
|
logger.Info(@event);
|
||||||
logger.Info(source);
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sql命令执行前
|
/// Sql命令执行前
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[EventSubscribe(nameof(SqlCommandBeforeEvent))]
|
[EventSubscribe]
|
||||||
public Task CommandBeforeAsync(EventHandlerExecutingContext context)
|
public Task CommandBeforeAsync(SqlCommandBeforeEvent @event)
|
||||||
{
|
{
|
||||||
var source = context.Source as SqlCommandBeforeEvent;
|
logger.Debug(@event);
|
||||||
logger.Debug(source);
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 种子数据插入完毕
|
/// 种子数据插入完毕
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[EventSubscribe(nameof(SeedDataInsertedEvent))]
|
[EventSubscribe]
|
||||||
public Task SeedDataInsertedEventAsync(EventHandlerExecutingContext context)
|
public Task SeedDataInsertedEventAsync(SeedDataInsertedEvent @event)
|
||||||
{
|
{
|
||||||
var source = context.Source as SeedDataInsertedEvent;
|
logger.Info(@event);
|
||||||
logger.Info(source);
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 同步数据库结构之后
|
/// 同步数据库结构之后
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[EventSubscribe(nameof(SyncStructureAfterEvent))]
|
[EventSubscribe]
|
||||||
public Task SyncStructureAfterAsync(EventHandlerExecutingContext context)
|
public Task SyncStructureAfterAsync(SyncStructureAfterEvent @event)
|
||||||
{
|
{
|
||||||
var source = context.Source as SyncStructureAfterEvent;
|
logger.Info(@event);
|
||||||
logger.Info(source);
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 同步数据库结构之前
|
/// 同步数据库结构之前
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[EventSubscribe(nameof(SyncStructureBeforeEvent))]
|
[EventSubscribe]
|
||||||
public Task SyncStructureBeforeAsync(EventHandlerExecutingContext context)
|
public Task SyncStructureBeforeAsync(SyncStructureBeforeEvent @event)
|
||||||
{
|
{
|
||||||
var source = context.Source as SyncStructureBeforeEvent;
|
logger.Info(@event);
|
||||||
logger.Info(source);
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
using System.Threading.Channels;
|
||||||
|
|
||||||
|
namespace NetAdmin.Infrastructure.EventBus;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 事件发布器默认实现
|
||||||
|
/// </summary>
|
||||||
|
public sealed class DefaultEventPublisher : IEventPublisher
|
||||||
|
{
|
||||||
|
private readonly Channel<object> _eventChannel;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="DefaultEventPublisher" /> class.
|
||||||
|
/// </summary>
|
||||||
|
public DefaultEventPublisher()
|
||||||
|
{
|
||||||
|
_eventChannel = Channel.CreateUnbounded<object>();
|
||||||
|
_ = new TaskFactory<Task>().StartNew( //
|
||||||
|
async state => {
|
||||||
|
var subscribers = (List<MethodInfo>)state;
|
||||||
|
await foreach (var msg in _eventChannel.Reader.ReadAllAsync()) {
|
||||||
|
_ = Parallel.ForEach( //
|
||||||
|
subscribers.Where(x => x.GetParameters().FirstOrDefault()?.ParameterType == msg.GetType())
|
||||||
|
, (x, _) => x.Invoke(App.GetService(x.DeclaringType), [msg]));
|
||||||
|
}
|
||||||
|
}, App.EffectiveTypes.Where(x => typeof(IEventSubscriber).IsAssignableFrom(x) && x.IsClass && !x.IsAbstract).SelectMany(x => x.GetMethods(BindingFlags.Instance | BindingFlags.Public).Where(y => y.IsDefined(typeof(EventSubscribeAttribute)))).ToList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task PublishAsync<T>(IEventData<T> eventData)
|
||||||
|
{
|
||||||
|
await _eventChannel.Writer.WriteAsync(eventData).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
namespace NetAdmin.Infrastructure.EventBus;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 事件处理程序特性
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
|
||||||
|
public sealed class EventSubscribeAttribute : Attribute;
|
@ -0,0 +1,17 @@
|
|||||||
|
namespace NetAdmin.Infrastructure.EventBus;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 事件数据接口
|
||||||
|
/// </summary>
|
||||||
|
public interface IEventData<T>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 事件发生时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime CreatedTime { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 负载
|
||||||
|
/// </summary>
|
||||||
|
public T PayLoad { get; init; }
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
namespace NetAdmin.Infrastructure.EventBus;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 事件发布器接口
|
||||||
|
/// </summary>
|
||||||
|
public interface IEventPublisher
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 发布一条消息
|
||||||
|
/// </summary>
|
||||||
|
Task PublishAsync<T>(IEventData<T> eventData);
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace NetAdmin.Infrastructure.EventBus;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 事件订阅器接口
|
||||||
|
/// </summary>
|
||||||
|
public interface IEventSubscriber;
|
@ -5,7 +5,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="NetAdmin.FreeSql.DbContext" Version="1.1.1" Label="refs"/>
|
<PackageReference Include="NetAdmin.FreeSql.DbContext" Version="1.1.1" Label="refs"/>
|
||||||
<PackageReference Include="NetAdmin.FreeSql.Provider.Sqlite" Version="1.1.1" Label="refs"/>
|
<PackageReference Include="NetAdmin.FreeSql.Provider.Sqlite" Version="1.1.1" Label="refs"/>
|
||||||
<PackageReference Include="Gurion" Version="1.2.8" Label="refs"/>
|
<PackageReference Include="Gurion" Version="1.2.9" Label="refs"/>
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.0"/>
|
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.0"/>
|
||||||
<PackageReference Include="Minio" Version="6.0.4"/>
|
<PackageReference Include="Minio" Version="6.0.4"/>
|
||||||
<PackageReference Include="NSExt" Version="2.3.3"/>
|
<PackageReference Include="NSExt" Version="2.3.3"/>
|
||||||
|
@ -10,11 +10,6 @@ public sealed class CaptchaService : ServiceBase<ICaptchaService>, ICaptchaServi
|
|||||||
private static readonly Assembly _currAsm = Assembly.GetAssembly(typeof(CaptchaService));
|
private static readonly Assembly _currAsm = Assembly.GetAssembly(typeof(CaptchaService));
|
||||||
private static readonly string _currAsmName = _currAsm.FullName![.._currAsm.FullName.IndexOf(',')];
|
private static readonly string _currAsmName = _currAsm.FullName![.._currAsm.FullName.IndexOf(',')];
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="CaptchaService" /> class.
|
|
||||||
/// </summary>
|
|
||||||
public CaptchaService() { }
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task<GetCaptchaRsp> GetCaptchaImageAsync()
|
public async Task<GetCaptchaRsp> GetCaptchaImageAsync()
|
||||||
{
|
{
|
||||||
|
@ -10,8 +10,8 @@ public sealed class ApiSynchronizer(ILogger<ApiSynchronizer> logger) : IEventSub
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 同步Api接口
|
/// 同步Api接口
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[EventSubscribe(nameof(SyncStructureAfterEvent))]
|
[EventSubscribe]
|
||||||
public async Task SyncApiAsync(EventHandlerExecutingContext _)
|
public async Task SyncApiAsync(SyncStructureAfterEvent _)
|
||||||
{
|
{
|
||||||
var logService = App.GetService<IApiService>();
|
var logService = App.GetService<IApiService>();
|
||||||
await logService.SyncAsync().ConfigureAwait(false);
|
await logService.SyncAsync().ConfigureAwait(false);
|
||||||
|
@ -8,24 +8,16 @@ namespace NetAdmin.SysComponent.Host.Subscribers;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class CacheCleaner : IEventSubscriber
|
public sealed class CacheCleaner : IEventSubscriber
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="CacheCleaner" /> class.
|
|
||||||
/// </summary>
|
|
||||||
public CacheCleaner() { }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户缓存清理
|
/// 用户缓存清理
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[EventSubscribe(nameof(UserUpdatedEvent))]
|
[EventSubscribe]
|
||||||
public Task RemoveUserInfoAsync(EventHandlerExecutingContext context)
|
#pragma warning disable CA1822
|
||||||
|
public Task RemoveUserInfoAsync(UserUpdatedEvent @event)
|
||||||
|
#pragma warning restore CA1822
|
||||||
{
|
{
|
||||||
if (context.Source is not UserUpdatedEvent userUpdatedEvent) {
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
var cache = App.GetService<IUserCache>();
|
var cache = App.GetService<IUserCache>();
|
||||||
cache.Service.UserToken = ContextUserToken.Create(userUpdatedEvent.Data.Id, userUpdatedEvent.Data.Token, userUpdatedEvent.Data.UserName
|
cache.Service.UserToken = ContextUserToken.Create(@event.PayLoad.Id, @event.PayLoad.Token, @event.PayLoad.UserName, @event.PayLoad.DeptId);
|
||||||
, userUpdatedEvent.Data.DeptId);
|
|
||||||
return cache.RemoveUserInfoAsync();
|
return cache.RemoveUserInfoAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -12,18 +12,16 @@ public sealed class EmailCodeSender(ILogger<EmailCodeSender> logger) : IEventSub
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送邮件
|
/// 发送邮件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[EventSubscribe(nameof(VerifyCodeCreatedEvent))]
|
[EventSubscribe]
|
||||||
public async Task SendEmailAsync(EventHandlerExecutingContext context)
|
public async Task SendEmailAsync(VerifyCodeCreatedEvent @event)
|
||||||
{
|
{
|
||||||
if (context.Source is not VerifyCodeCreatedEvent verifyCodeCreatedEvent ||
|
if (@event.PayLoad.DeviceType != VerifyCodeDeviceTypes.Email) {
|
||||||
verifyCodeCreatedEvent.Data.DeviceType != VerifyCodeDeviceTypes.Email) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送...
|
// 发送...
|
||||||
var verifyCodeService = App.GetService<IVerifyCodeService>();
|
var verifyCodeService = App.GetService<IVerifyCodeService>();
|
||||||
_ = await verifyCodeService.SetVerifyCodeStatusAsync(
|
_ = await verifyCodeService.SetVerifyCodeStatusAsync(@event.PayLoad.Adapt<SetVerifyCodeStatusReq>() with { Status = VerifyCodeStatues.Sent })
|
||||||
verifyCodeCreatedEvent.Data.Adapt<SetVerifyCodeStatusReq>() with { Status = VerifyCodeStatues.Sent })
|
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
logger.Info($"{nameof(IVerifyCodeService)}.{nameof(IVerifyCodeService.SetVerifyCodeStatusAsync)} {Ln.已处理完毕}");
|
logger.Info($"{nameof(IVerifyCodeService)}.{nameof(IVerifyCodeService.SetVerifyCodeStatusAsync)} {Ln.已处理完毕}");
|
||||||
}
|
}
|
||||||
|
@ -18,18 +18,16 @@ public sealed class OperationLogger : IEventSubscriber
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 保存请求日志到数据库
|
/// 保存请求日志到数据库
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[EventSubscribe(nameof(RequestLogEvent))]
|
[EventSubscribe]
|
||||||
public async Task OperationEventDbRecordAsync(EventHandlerExecutingContext context)
|
#pragma warning disable CA1822
|
||||||
|
public async Task OperationEventDbRecordAsync(RequestLogEvent @event)
|
||||||
|
#pragma warning restore CA1822
|
||||||
{
|
{
|
||||||
if (context.Source is not RequestLogEvent operationEvent) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_requestLogs.Count > Numbers.REQUEST_LOG_BUFF_SIZE) {
|
if (_requestLogs.Count > Numbers.REQUEST_LOG_BUFF_SIZE) {
|
||||||
await WriteToDbAsync().ConfigureAwait(false);
|
await WriteToDbAsync().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_requestLogs.Enqueue(operationEvent.Data);
|
_requestLogs.Enqueue(@event.PayLoad);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,14 +79,12 @@ public sealed class OperationLogger : IEventSubscriber
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 保存请求日志到数据库
|
/// 保存请求日志到数据库
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[EventSubscribe(nameof(RequestLogEvent))]
|
[EventSubscribe]
|
||||||
public async Task OperationEventDbRecordAsync(EventHandlerExecutingContext context)
|
#pragma warning disable CA1822
|
||||||
|
public async Task OperationEventDbRecordAsync(RequestLogEvent @event)
|
||||||
|
#pragma warning restore CA1822
|
||||||
{
|
{
|
||||||
if (context.Source is not RequestLogEvent operationEvent) {
|
_ = await App.GetService<IRequestLogCache>().CreateAsync(@event.PayLoad).ConfigureAwait(false);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = await App.GetService<IRequestLogCache>().CreateAsync(operationEvent.Data).ConfigureAwait(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
@ -12,18 +12,16 @@ public sealed class SmsCodeSender(ILogger<SmsCodeSender> logger) : IEventSubscri
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送短信
|
/// 发送短信
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[EventSubscribe(nameof(VerifyCodeCreatedEvent))]
|
[EventSubscribe]
|
||||||
public async Task SendSmsAsync(EventHandlerExecutingContext context)
|
public async Task SendSmsAsync(VerifyCodeCreatedEvent @event)
|
||||||
{
|
{
|
||||||
if (context.Source is not VerifyCodeCreatedEvent verifyCodeCreatedEvent ||
|
if (@event.PayLoad.DeviceType != VerifyCodeDeviceTypes.Mobile) {
|
||||||
verifyCodeCreatedEvent.Data.DeviceType != VerifyCodeDeviceTypes.Mobile) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送...
|
// 发送...
|
||||||
var verifyCodeService = App.GetService<IVerifyCodeService>();
|
var verifyCodeService = App.GetService<IVerifyCodeService>();
|
||||||
_ = await verifyCodeService.SetVerifyCodeStatusAsync(
|
_ = await verifyCodeService.SetVerifyCodeStatusAsync(@event.PayLoad.Adapt<SetVerifyCodeStatusReq>() with { Status = VerifyCodeStatues.Sent })
|
||||||
verifyCodeCreatedEvent.Data.Adapt<SetVerifyCodeStatusReq>() with { Status = VerifyCodeStatues.Sent })
|
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
logger.Info($"{nameof(IVerifyCodeService)}.{nameof(IVerifyCodeService.SetVerifyCodeStatusAsync)} {Ln.已处理完毕}");
|
logger.Info($"{nameof(IVerifyCodeService)}.{nameof(IVerifyCodeService.SetVerifyCodeStatusAsync)} {Ln.已处理完毕}");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user