diff --git a/src/WeihanLi.Common/Event/EventBase.cs b/src/WeihanLi.Common/Event/EventBase.cs index f76790a9..70e7a214 100644 --- a/src/WeihanLi.Common/Event/EventBase.cs +++ b/src/WeihanLi.Common/Event/EventBase.cs @@ -52,16 +52,22 @@ protected EventBase(string eventId, DateTimeOffset eventAt) } } -internal interface IEventWrapper +internal interface IEvent { EventProperties Properties { get; } - object Data { get; } + object? Data { get; } } -internal sealed class EventWrapper : IEventWrapper +public interface IEvent +{ + EventProperties Properties { get; } + T Data { get; } +} + +internal sealed class EventWrapper : IEvent, IEvent { public required T Data { get; init; } - object IEventWrapper.Data => Data!; + object? IEvent.Data => (object?)Data; public required EventProperties Properties { get; init; } } diff --git a/src/WeihanLi.Common/Event/EventQueueInMemory.cs b/src/WeihanLi.Common/Event/EventQueueInMemory.cs index c9d90a68..c6cb9122 100644 --- a/src/WeihanLi.Common/Event/EventQueueInMemory.cs +++ b/src/WeihanLi.Common/Event/EventQueueInMemory.cs @@ -9,7 +9,7 @@ namespace WeihanLi.Common.Event; public sealed class EventQueueInMemory : IEventQueue { - private readonly ConcurrentDictionary> _eventQueues = new(); + private readonly ConcurrentDictionary> _eventQueues = new(); public ICollection GetQueues() => _eventQueues.Keys; @@ -35,7 +35,7 @@ public bool Enqueue(string queueName, TEvent @event, EventProperties? pr Data = @event, Properties = properties }; - var queue = _eventQueues.GetOrAdd(queueName, _ => new ConcurrentQueue()); + var queue = _eventQueues.GetOrAdd(queueName, _ => new ConcurrentQueue()); queue.Enqueue(internalEvent); return true; } diff --git a/src/WeihanLi.Common/Event/EventStoreInMemory.cs b/src/WeihanLi.Common/Event/EventStoreInMemory.cs index a453a2d9..e537bb5b 100644 --- a/src/WeihanLi.Common/Event/EventStoreInMemory.cs +++ b/src/WeihanLi.Common/Event/EventStoreInMemory.cs @@ -8,24 +8,19 @@ namespace WeihanLi.Common.Event; public sealed class EventStoreInMemory : IEventStore { - private readonly ConcurrentDictionary _events = new(); + private readonly ConcurrentDictionary _events = new(); - public int SaveEvents(ICollection events) + public Task SaveEventsAsync(ICollection events) { if (events.IsNullOrEmpty()) - return 0; + return Task.FromResult(0); - return events.Count(@event => _events.TryAdd(@event.EventId, @event)); + return Task.FromResult(events.Count(@event => _events.TryAdd(@event.Properties.EventId, @event))); } - public Task SaveEventsAsync(ICollection events) => Task.FromResult(SaveEvents(events)); - - public int DeleteEvents(ICollection eventIds) + private int DeleteEvents(ICollection eventIds) { - if (eventIds.IsNullOrEmpty()) - return 0; - - return eventIds.Count(eventId => _events.TryRemove(eventId, out _)); + return eventIds.IsNullOrEmpty() ? 0 : eventIds.Count(eventId => _events.TryRemove(eventId, out _)); } public Task DeleteEventsAsync(ICollection eventIds) => Task.FromResult(DeleteEvents(eventIds)); diff --git a/src/WeihanLi.Common/Event/IEventStore.cs b/src/WeihanLi.Common/Event/IEventStore.cs index 65d5cb55..822b4080 100644 --- a/src/WeihanLi.Common/Event/IEventStore.cs +++ b/src/WeihanLi.Common/Event/IEventStore.cs @@ -5,8 +5,6 @@ namespace WeihanLi.Common.Event; public interface IEventStore { - int SaveEvents(ICollection events); - Task SaveEventsAsync(ICollection events); - int DeleteEvents(ICollection eventIds); + Task SaveEventsAsync(ICollection events); Task DeleteEventsAsync(ICollection eventIds); } diff --git a/src/WeihanLi.Common/Extensions/JsonSerializeExtension.cs b/src/WeihanLi.Common/Extensions/JsonSerializeExtension.cs index 210a061e..8e8cafc3 100644 --- a/src/WeihanLi.Common/Extensions/JsonSerializeExtension.cs +++ b/src/WeihanLi.Common/Extensions/JsonSerializeExtension.cs @@ -21,7 +21,7 @@ public static class JsonSerializeExtension public static JsonSerializerSettings SerializerSettingsWith(Action? action) { - if (null == action) + if (action is null) return DefaultSerializerSettings; var serializerSettings = GetDefaultSerializerSettings();