From 70b206b5228187da2ecbd7592c522ef63ef77ed3 Mon Sep 17 00:00:00 2001 From: MrXhh <2791341417@qq.com> Date: Thu, 13 Feb 2025 17:49:55 +0800 Subject: [PATCH 1/2] refactor: generic-type-constraint --- Src/Coravel/Events/Dispatcher.cs | 6 +++--- Src/Coravel/Events/Interfaces/IDispatcher.cs | 3 +-- Src/Coravel/Events/Interfaces/IListener.cs | 11 ++++++++++- Src/Coravel/Queuing/Interfaces/IQueue.cs | 3 +-- Src/Coravel/Queuing/Queue.cs | 2 +- .../Scheduling/Stubs/DispatcherStub.cs | 2 +- 6 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Src/Coravel/Events/Dispatcher.cs b/Src/Coravel/Events/Dispatcher.cs index 93ddc176..a1ab80d9 100644 --- a/Src/Coravel/Events/Dispatcher.cs +++ b/Src/Coravel/Events/Dispatcher.cs @@ -48,9 +48,8 @@ public IEventSubscription Register() where TEvent : IEvent /// Broadcasts an event to be handled by its subscribed listeners. /// /// - /// /// - public async Task Broadcast(TEvent toBroadcast) where TEvent : IEvent + public async Task Broadcast(IEvent toBroadcast) { if (this._events.TryGetValue(toBroadcast.GetType(), out var listeners)) { @@ -59,10 +58,11 @@ public async Task Broadcast(TEvent toBroadcast) where TEvent : IEvent await using (var scope = this._scopeFactory.CreateAsyncScope()) { var obj = scope.ServiceProvider.GetService(listenerType); - if (obj is IListener listener) + if (obj is IListener listener) { await listener.HandleAsync(toBroadcast); } + // can delete else { // Depending on what assemblies the events, listeners and calling assmebly are - the cast // above doesn't work (even though the type really does implement the interface). diff --git a/Src/Coravel/Events/Interfaces/IDispatcher.cs b/Src/Coravel/Events/Interfaces/IDispatcher.cs index 1a344dbe..b4ec5feb 100644 --- a/Src/Coravel/Events/Interfaces/IDispatcher.cs +++ b/Src/Coravel/Events/Interfaces/IDispatcher.cs @@ -10,7 +10,6 @@ public interface IDispatcher /// /// Dispatches and broadcasts the event to all subscribed listeners. /// - /// - Task Broadcast(TEvent toBroadcast) where TEvent : IEvent; + Task Broadcast(IEvent toBroadcast); } } \ No newline at end of file diff --git a/Src/Coravel/Events/Interfaces/IListener.cs b/Src/Coravel/Events/Interfaces/IListener.cs index 09208bf2..dd584ed1 100644 --- a/Src/Coravel/Events/Interfaces/IListener.cs +++ b/Src/Coravel/Events/Interfaces/IListener.cs @@ -2,8 +2,17 @@ namespace Coravel.Events.Interfaces { - public interface IListener where TEvent : IEvent + public interface IListener : IListener + where TEvent : IEvent { Task HandleAsync(TEvent broadcasted); + + Task IListener.HandleAsync(IEvent broadcasted) + => HandleAsync((TEvent)broadcasted); + } + + public interface IListener + { + internal Task HandleAsync(IEvent broadcasted); } } \ No newline at end of file diff --git a/Src/Coravel/Queuing/Interfaces/IQueue.cs b/Src/Coravel/Queuing/Interfaces/IQueue.cs index a57dd74c..a2ba6a90 100644 --- a/Src/Coravel/Queuing/Interfaces/IQueue.cs +++ b/Src/Coravel/Queuing/Interfaces/IQueue.cs @@ -38,8 +38,7 @@ public interface IQueue /// /// Queue an event to be broadcasted. /// - /// - void QueueBroadcast(TEvent toBroadcast) where TEvent : IEvent; + void QueueBroadcast(IEvent toBroadcast); /// /// Queue an invocable that will be given the payload supplied to this method. diff --git a/Src/Coravel/Queuing/Queue.cs b/Src/Coravel/Queuing/Queue.cs index 92c8cab8..49fb7aa6 100644 --- a/Src/Coravel/Queuing/Queue.cs +++ b/Src/Coravel/Queuing/Queue.cs @@ -71,7 +71,7 @@ public Guid QueueAsyncTask(Func asyncTask) return job.Guid; } - public void QueueBroadcast(TEvent toBroadcast) where TEvent : IEvent + public void QueueBroadcast(IEvent toBroadcast) { this.QueueAsyncTask(async () => await this._dispatcher.Broadcast(toBroadcast)); } diff --git a/Src/UnitTests/CoravelUnitTests/Scheduling/Stubs/DispatcherStub.cs b/Src/UnitTests/CoravelUnitTests/Scheduling/Stubs/DispatcherStub.cs index 28199cd1..f0c55966 100644 --- a/Src/UnitTests/CoravelUnitTests/Scheduling/Stubs/DispatcherStub.cs +++ b/Src/UnitTests/CoravelUnitTests/Scheduling/Stubs/DispatcherStub.cs @@ -5,7 +5,7 @@ namespace CoravelUnitTests.Scheduling.Stubs { public class DispatcherStub : IDispatcher { - public Task Broadcast(TEvent toBroadcast) where TEvent : IEvent + public Task Broadcast(IEvent toBroadcast) { return Task.CompletedTask; } From 05cc12ef8c067ecb2a7be4414eef9b3686886be5 Mon Sep 17 00:00:00 2001 From: MrXhh <2791341417@qq.com> Date: Fri, 14 Feb 2025 09:33:08 +0800 Subject: [PATCH 2/2] refactor: Dictionary Comparer --- Src/Coravel.Mailer/MailServiceRegistration.cs | 4 ++-- Src/Coravel/Events/Dispatcher.cs | 13 ++++--------- .../Scheduling/Schedule/Mutex/InMemoryMutex.cs | 3 ++- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/Src/Coravel.Mailer/MailServiceRegistration.cs b/Src/Coravel.Mailer/MailServiceRegistration.cs index 00bfa4e2..4dcf48cd 100644 --- a/Src/Coravel.Mailer/MailServiceRegistration.cs +++ b/Src/Coravel.Mailer/MailServiceRegistration.cs @@ -25,10 +25,10 @@ public static IServiceCollection AddMailer(this IServiceCollection services, ICo { string mailerType = config.GetValue("Coravel:Mail:Driver", "FileLog"); - var strategies = new Dictionary(); + var strategies = new Dictionary(StringComparer.OrdinalIgnoreCase); strategies.Add("SMTP", () => AddSmtpMailer(services, config)); strategies.Add("FILELOG", () => AddFileLogMailer(services, config)); - strategies[mailerType.ToUpper()].Invoke(); + strategies[mailerType].Invoke(); return services; } diff --git a/Src/Coravel/Events/Dispatcher.cs b/Src/Coravel/Events/Dispatcher.cs index a1ab80d9..0de0f7ca 100644 --- a/Src/Coravel/Events/Dispatcher.cs +++ b/Src/Coravel/Events/Dispatcher.cs @@ -13,8 +13,8 @@ namespace Coravel.Events /// public class Dispatcher : IDispatcher, IEventRegistration { - private IServiceScopeFactory _scopeFactory; - private Dictionary> _events; + private readonly IServiceScopeFactory _scopeFactory; + private readonly Dictionary> _events; public Dispatcher(IServiceScopeFactory scopeFactory) { @@ -29,16 +29,11 @@ public Dispatcher(IServiceScopeFactory scopeFactory) /// public IEventSubscription Register() where TEvent : IEvent { - var listeners = new List(); var eventType = typeof(TEvent); - if (this._events.ContainsKey(eventType)) + if(!this._events.TryGetValue(eventType, out var listeners)) { - listeners = this._events[eventType]; - } - else - { - this._events.Add(eventType, listeners); + this._events.Add(eventType, listeners = new List()); } return new EventSubscription(listeners); diff --git a/Src/Coravel/Scheduling/Schedule/Mutex/InMemoryMutex.cs b/Src/Coravel/Scheduling/Schedule/Mutex/InMemoryMutex.cs index 039e577b..f0e55622 100644 --- a/Src/Coravel/Scheduling/Schedule/Mutex/InMemoryMutex.cs +++ b/Src/Coravel/Scheduling/Schedule/Mutex/InMemoryMutex.cs @@ -9,10 +9,11 @@ public class InMemoryMutex : IMutex { private IUtcTime _utcTime; private readonly object _lock = new object(); - private Dictionary _mutexCollection = new Dictionary(); + private readonly Dictionary _mutexCollection; public InMemoryMutex() { this._utcTime = new SystemUtcTime(); + _mutexCollection = new Dictionary(); } ///