From b761582b1d99b1334d7406084c107a66a261e5e7 Mon Sep 17 00:00:00 2001 From: Jason Webb Date: Thu, 6 Jun 2024 09:01:02 -0600 Subject: [PATCH] Removed unserializable entity events from persistence layer. They had the potential for creating problems for the serializers. --- .../EventHandlingBuilderExtensions.cs | 5 --- .../Subscribers/SubscriberFactory.cs | 45 ------------------- .../Subscribers/SubscriberFactoryOptions.cs | 19 -------- .../SubscriberNotFoundException.cs | 16 ------- Src/RCommon.Dapper/Crud/DapperRepository.cs | 10 ++--- Src/RCommon.EfCore/Crud/EFCoreRepository.cs | 9 ++-- Src/RCommon.Entities/BusinessEntity.cs | 24 +++++----- Src/RCommon.Entities/EntityChangedEvent.cs | 15 ------- Src/RCommon.Entities/EntityCreatedEvent.cs | 15 ------- Src/RCommon.Entities/EntityDeletedEvent.cs | 15 ------- Src/RCommon.Entities/EntityUpdatedEvent.cs | 15 ------- .../LocalEventsClearedEventArgs.cs | 21 --------- ...=> TransactionalEventsChangedEventArgs.cs} | 4 +- ...=> TransactionalEventsClearedEventArgs.cs} | 6 +-- Src/RCommon.Linq2Db/Crud/Linq2DbRepository.cs | 9 ++-- 15 files changed, 26 insertions(+), 202 deletions(-) delete mode 100644 Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactory.cs delete mode 100644 Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactoryOptions.cs delete mode 100644 Src/RCommon.Core/EventHandling/Subscribers/SubscriberNotFoundException.cs delete mode 100644 Src/RCommon.Entities/EntityChangedEvent.cs delete mode 100644 Src/RCommon.Entities/EntityCreatedEvent.cs delete mode 100644 Src/RCommon.Entities/EntityDeletedEvent.cs delete mode 100644 Src/RCommon.Entities/EntityUpdatedEvent.cs delete mode 100644 Src/RCommon.Entities/LocalEventsClearedEventArgs.cs rename Src/RCommon.Entities/{LocalEventsChangedEventArgs.cs => TransactionalEventsChangedEventArgs.cs} (68%) rename Src/RCommon.Entities/{EntityEvent.cs => TransactionalEventsClearedEventArgs.cs} (56%) diff --git a/Src/RCommon.Core/EventHandling/EventHandlingBuilderExtensions.cs b/Src/RCommon.Core/EventHandling/EventHandlingBuilderExtensions.cs index 485af7d6..bfd1dfe6 100644 --- a/Src/RCommon.Core/EventHandling/EventHandlingBuilderExtensions.cs +++ b/Src/RCommon.Core/EventHandling/EventHandlingBuilderExtensions.cs @@ -24,11 +24,6 @@ public static IRCommonBuilder WithEventHandling(this IRCommonBuilder builder) public static IRCommonBuilder WithEventHandling(this IRCommonBuilder builder, Action actions) where T : IEventHandlingBuilder { - Guard.Against(dataStoreName.IsNullOrEmpty(), "You must set a name for the Data Store"); - - this._services.TryAddTransient(); - this._services.Configure(options => options.Register(dataStoreName)); - // Event Handling Configurations var eventHandlingConfig = (T)Activator.CreateInstance(typeof(T), new object[] { builder }); actions(eventHandlingConfig); diff --git a/Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactory.cs b/Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactory.cs deleted file mode 100644 index 78ccc08c..00000000 --- a/Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactory.cs +++ /dev/null @@ -1,45 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RCommon.EventHandling.Subscribers -{ - public class SubscriberFactory - { - private readonly IServiceProvider _provider; - private readonly IDictionary _types; - - public SubscriberFactory(IServiceProvider provider, IOptions options) - { - _provider = provider; - _types = options.Value.Types; - } - - public ISubscriber Resolve() - where TEvent : class, ISerializableEvent - { - if (_types.TryGetValue(name, out var type)) - { - return (ISubscriber)_provider.GetRequiredService(type); - } - - throw new SubscriberNotFoundException($"Subscriber with name of {name} not found"); - } - - public TSubscriber Resolve() - where TSubscriber : class, ISubscriber - where TEvent : class, ISerializableEvent - { - if (_types.TryGetValue(name, out var type)) - { - return (TSubscriber)_provider.GetRequiredService(type); - } - - throw new SubscriberNotFoundException($"Subscriber with name of {name} not found"); - } - } -} diff --git a/Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactoryOptions.cs b/Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactoryOptions.cs deleted file mode 100644 index b2922937..00000000 --- a/Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactoryOptions.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RCommon.EventHandling.Subscribers -{ - public class SubscriberFactoryOptions - { - public IDictionary Types { get; } = new Dictionary(); - - public void Register() - where TSubscriber : class, ISubscriber - { - Types.Add(typeof(TSubscriber).AssemblyQualifiedName, typeof(TSubscriber)); - } - } -} diff --git a/Src/RCommon.Core/EventHandling/Subscribers/SubscriberNotFoundException.cs b/Src/RCommon.Core/EventHandling/Subscribers/SubscriberNotFoundException.cs deleted file mode 100644 index 33aa6f7c..00000000 --- a/Src/RCommon.Core/EventHandling/Subscribers/SubscriberNotFoundException.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RCommon.EventHandling.Subscribers -{ - public class SubscriberNotFoundException : GeneralException - { - public SubscriberNotFoundException(string message) : base(message) - { - - } - } -} diff --git a/Src/RCommon.Dapper/Crud/DapperRepository.cs b/Src/RCommon.Dapper/Crud/DapperRepository.cs index c7fbcb47..fa0a3f33 100644 --- a/Src/RCommon.Dapper/Crud/DapperRepository.cs +++ b/Src/RCommon.Dapper/Crud/DapperRepository.cs @@ -44,10 +44,8 @@ public override async Task AddAsync(TEntity entity, CancellationToken token = de { await db.OpenAsync(); } - - await db.InsertAsync(entity, cancellationToken: token); - entity.AddLocalEvent(new EntityCreatedEvent(entity)); EventTracker.AddEntity(entity); + await db.InsertAsync(entity, cancellationToken: token); } catch (ApplicationException exception) @@ -78,9 +76,8 @@ public override async Task DeleteAsync(TEntity entity, CancellationToken token = await db.OpenAsync(); } - await db.DeleteAsync(entity, cancellationToken: token); - entity.AddLocalEvent(new EntityDeletedEvent(entity)); EventTracker.AddEntity(entity); + await db.DeleteAsync(entity, cancellationToken: token); } catch (ApplicationException exception) { @@ -112,9 +109,8 @@ public override async Task UpdateAsync(TEntity entity, CancellationToken token = await db.OpenAsync(); } - await db.UpdateAsync(entity, cancellationToken: token); - entity.AddLocalEvent(new EntityUpdatedEvent(entity)); EventTracker.AddEntity(entity); + await db.UpdateAsync(entity, cancellationToken: token); } catch (ApplicationException exception) { diff --git a/Src/RCommon.EfCore/Crud/EFCoreRepository.cs b/Src/RCommon.EfCore/Crud/EFCoreRepository.cs index 2b5e844a..957a4ebe 100644 --- a/Src/RCommon.EfCore/Crud/EFCoreRepository.cs +++ b/Src/RCommon.EfCore/Crud/EFCoreRepository.cs @@ -120,26 +120,23 @@ protected override IQueryable RepositoryQuery public override async Task AddAsync(TEntity entity, CancellationToken token = default) { - await ObjectSet.AddAsync(entity, token); - entity.AddLocalEvent(new EntityCreatedEvent(entity)); EventTracker.AddEntity(entity); + await ObjectSet.AddAsync(entity, token); await SaveAsync(token); } public async override Task DeleteAsync(TEntity entity, CancellationToken token = default) { - ObjectSet.Remove(entity); - entity.AddLocalEvent(new EntityDeletedEvent(entity)); EventTracker.AddEntity(entity); + ObjectSet.Remove(entity); await SaveAsync(); } public async override Task UpdateAsync(TEntity entity, CancellationToken token = default) { - ObjectSet.Update(entity); - entity.AddLocalEvent(new EntityUpdatedEvent(entity)); EventTracker.AddEntity(entity); + ObjectSet.Update(entity); await SaveAsync(token); } diff --git a/Src/RCommon.Entities/BusinessEntity.cs b/Src/RCommon.Entities/BusinessEntity.cs index 786feada..70e2344f 100644 --- a/Src/RCommon.Entities/BusinessEntity.cs +++ b/Src/RCommon.Entities/BusinessEntity.cs @@ -20,9 +20,9 @@ public BusinessEntity() } - public event EventHandler LocalEventsAdded; - public event EventHandler LocalEventsRemoved; - public event EventHandler LocalEventsCleared; + public event EventHandler TransactionalEventAdded; + public event EventHandler TransactionalEventRemoved; + public event EventHandler TransactionalEventsCleared; /// @@ -47,42 +47,42 @@ public bool EntityEquals(IBusinessEntity other) public void AddLocalEvent(ISerializableEvent eventItem) { _localEvents.Add(eventItem); - this.OnLocalEventsAdded(new LocalEventsChangedEventArgs(this, eventItem)); + this.OnLocalEventsAdded(new TransactionalEventsChangedEventArgs(this, eventItem)); } public void RemoveLocalEvent(ISerializableEvent eventItem) { _localEvents?.Remove(eventItem); - this.OnLocalEventsRemoved(new LocalEventsChangedEventArgs(this, eventItem)); + this.OnLocalEventsRemoved(new TransactionalEventsChangedEventArgs(this, eventItem)); } public void ClearLocalEvents() { _localEvents?.Clear(); - this.OnLocalEventsCleared(new LocalEventsClearedEventArgs(this, this.LocalEvents)); + this.OnLocalEventsCleared(new TransactionalEventsClearedEventArgs(this)); } - protected void OnLocalEventsAdded(LocalEventsChangedEventArgs args) + protected void OnLocalEventsAdded(TransactionalEventsChangedEventArgs args) { - EventHandler handler = LocalEventsAdded; + EventHandler handler = TransactionalEventAdded; if (handler != null) { handler(this, args); } } - protected void OnLocalEventsRemoved(LocalEventsChangedEventArgs args) + protected void OnLocalEventsRemoved(TransactionalEventsChangedEventArgs args) { - EventHandler handler = LocalEventsRemoved; + EventHandler handler = TransactionalEventRemoved; if (handler != null) { handler(this, args); } } - protected void OnLocalEventsCleared(LocalEventsClearedEventArgs args) + protected void OnLocalEventsCleared(TransactionalEventsClearedEventArgs args) { - EventHandler handler = LocalEventsCleared; + EventHandler handler = TransactionalEventsCleared; if (handler != null) { handler(this, args); diff --git a/Src/RCommon.Entities/EntityChangedEvent.cs b/Src/RCommon.Entities/EntityChangedEvent.cs deleted file mode 100644 index de9fd56e..00000000 --- a/Src/RCommon.Entities/EntityChangedEvent.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RCommon.Entities -{ - public class EntityChangedEvent : EntityEvent - { - public EntityChangedEvent(TEntity entity) : base(entity) - { - } - } -} diff --git a/Src/RCommon.Entities/EntityCreatedEvent.cs b/Src/RCommon.Entities/EntityCreatedEvent.cs deleted file mode 100644 index c851ed71..00000000 --- a/Src/RCommon.Entities/EntityCreatedEvent.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RCommon.Entities -{ - public class EntityCreatedEvent : EntityChangedEvent - { - public EntityCreatedEvent(TEntity entity) : base(entity) - { - } - } -} diff --git a/Src/RCommon.Entities/EntityDeletedEvent.cs b/Src/RCommon.Entities/EntityDeletedEvent.cs deleted file mode 100644 index 9f729703..00000000 --- a/Src/RCommon.Entities/EntityDeletedEvent.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RCommon.Entities -{ - public class EntityDeletedEvent : EntityChangedEvent - { - public EntityDeletedEvent(TEntity entity) : base(entity) - { - } - } -} diff --git a/Src/RCommon.Entities/EntityUpdatedEvent.cs b/Src/RCommon.Entities/EntityUpdatedEvent.cs deleted file mode 100644 index b2410c4e..00000000 --- a/Src/RCommon.Entities/EntityUpdatedEvent.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RCommon.Entities -{ - public class EntityUpdatedEvent : EntityChangedEvent - { - public EntityUpdatedEvent(TEntity entity) : base(entity) - { - } - } -} diff --git a/Src/RCommon.Entities/LocalEventsClearedEventArgs.cs b/Src/RCommon.Entities/LocalEventsClearedEventArgs.cs deleted file mode 100644 index 4fcc4e1a..00000000 --- a/Src/RCommon.Entities/LocalEventsClearedEventArgs.cs +++ /dev/null @@ -1,21 +0,0 @@ -using RCommon.EventHandling; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RCommon.Entities -{ - public class LocalEventsClearedEventArgs : EventArgs - { - public LocalEventsClearedEventArgs(IBusinessEntity entity, IReadOnlyCollection localEvents) - { - Entity=entity; - LocalEvents=localEvents; - } - - public IBusinessEntity Entity { get; } - public IReadOnlyCollection LocalEvents { get; } - } -} diff --git a/Src/RCommon.Entities/LocalEventsChangedEventArgs.cs b/Src/RCommon.Entities/TransactionalEventsChangedEventArgs.cs similarity index 68% rename from Src/RCommon.Entities/LocalEventsChangedEventArgs.cs rename to Src/RCommon.Entities/TransactionalEventsChangedEventArgs.cs index 4fe67a23..5f07490d 100644 --- a/Src/RCommon.Entities/LocalEventsChangedEventArgs.cs +++ b/Src/RCommon.Entities/TransactionalEventsChangedEventArgs.cs @@ -7,9 +7,9 @@ namespace RCommon.Entities { - public class LocalEventsChangedEventArgs : EventArgs + public class TransactionalEventsChangedEventArgs : EventArgs { - public LocalEventsChangedEventArgs(IBusinessEntity entity, ISerializableEvent eventData) + public TransactionalEventsChangedEventArgs(IBusinessEntity entity, ISerializableEvent eventData) { Entity=entity; EventData=eventData; diff --git a/Src/RCommon.Entities/EntityEvent.cs b/Src/RCommon.Entities/TransactionalEventsClearedEventArgs.cs similarity index 56% rename from Src/RCommon.Entities/EntityEvent.cs rename to Src/RCommon.Entities/TransactionalEventsClearedEventArgs.cs index 7e29f496..ce52c837 100644 --- a/Src/RCommon.Entities/EntityEvent.cs +++ b/Src/RCommon.Entities/TransactionalEventsClearedEventArgs.cs @@ -7,13 +7,13 @@ namespace RCommon.Entities { - public class EntityEvent : ISyncEvent + public class TransactionalEventsClearedEventArgs : EventArgs { - public EntityEvent(TEntity entity) + public TransactionalEventsClearedEventArgs(IBusinessEntity entity) { Entity = entity; } - public TEntity Entity { get; } + public IBusinessEntity Entity { get; } } } diff --git a/Src/RCommon.Linq2Db/Crud/Linq2DbRepository.cs b/Src/RCommon.Linq2Db/Crud/Linq2DbRepository.cs index b6b05d60..573baf54 100644 --- a/Src/RCommon.Linq2Db/Crud/Linq2DbRepository.cs +++ b/Src/RCommon.Linq2Db/Crud/Linq2DbRepository.cs @@ -119,9 +119,8 @@ private IQueryable FindCore(Expression> expression) public async override Task AddAsync(TEntity entity, CancellationToken token = default) { - await DataConnection.InsertAsync(entity, token: token); - entity.AddLocalEvent(new EntityCreatedEvent(entity)); EventTracker.AddEntity(entity); + await DataConnection.InsertAsync(entity, token: token); } public async override Task AnyAsync(Expression> expression, CancellationToken token = default) @@ -136,9 +135,8 @@ public async override Task AnyAsync(ISpecification specification, public async override Task DeleteAsync(TEntity entity, CancellationToken token = default) { - await DataConnection.DeleteAsync(entity); - entity.AddLocalEvent(new EntityDeletedEvent(entity)); EventTracker.AddEntity(entity); + await DataConnection.DeleteAsync(entity); } public override IQueryable FindQuery(ISpecification specification) @@ -227,9 +225,8 @@ public async override Task GetCountAsync(Expression> e public async override Task UpdateAsync(TEntity entity, CancellationToken token = default) { - await DataConnection.UpdateAsync(entity, token: token); - entity.AddLocalEvent(new EntityUpdatedEvent(entity)); EventTracker.AddEntity(entity); + await DataConnection.UpdateAsync(entity, token: token); } } }