Skip to content

Commit

Permalink
Removed unserializable entity events from persistence layer. They had…
Browse files Browse the repository at this point in the history
… the potential for creating problems for the serializers.
  • Loading branch information
jasonmwebb-lv committed Jun 6, 2024
1 parent 4eb0323 commit b761582
Show file tree
Hide file tree
Showing 15 changed files with 26 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ public static IRCommonBuilder WithEventHandling<T>(this IRCommonBuilder builder)
public static IRCommonBuilder WithEventHandling<T>(this IRCommonBuilder builder, Action<T> actions)
where T : IEventHandlingBuilder
{
Guard.Against<UnsupportedDataStoreException>(dataStoreName.IsNullOrEmpty(), "You must set a name for the Data Store");

this._services.TryAddTransient<IDataStoreFactory, DataStoreFactory>();
this._services.Configure<DataStoreFactoryOptions>(options => options.Register<TDbContext>(dataStoreName));

// Event Handling Configurations
var eventHandlingConfig = (T)Activator.CreateInstance(typeof(T), new object[] { builder });
actions(eventHandlingConfig);
Expand Down
45 changes: 0 additions & 45 deletions Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactory.cs

This file was deleted.

This file was deleted.

This file was deleted.

10 changes: 3 additions & 7 deletions Src/RCommon.Dapper/Crud/DapperRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TEntity>(entity));
EventTracker.AddEntity(entity);
await db.InsertAsync(entity, cancellationToken: token);

}
catch (ApplicationException exception)
Expand Down Expand Up @@ -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<TEntity>(entity));
EventTracker.AddEntity(entity);
await db.DeleteAsync(entity, cancellationToken: token);
}
catch (ApplicationException exception)
{
Expand Down Expand Up @@ -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<TEntity>(entity));
EventTracker.AddEntity(entity);
await db.UpdateAsync(entity, cancellationToken: token);
}
catch (ApplicationException exception)
{
Expand Down
9 changes: 3 additions & 6 deletions Src/RCommon.EfCore/Crud/EFCoreRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,23 @@ protected override IQueryable<TEntity> RepositoryQuery

public override async Task AddAsync(TEntity entity, CancellationToken token = default)
{
await ObjectSet.AddAsync(entity, token);
entity.AddLocalEvent(new EntityCreatedEvent<TEntity>(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<TEntity>(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<TEntity>(entity));
EventTracker.AddEntity(entity);
ObjectSet.Update(entity);
await SaveAsync(token);
}

Expand Down
24 changes: 12 additions & 12 deletions Src/RCommon.Entities/BusinessEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public BusinessEntity()
}


public event EventHandler<LocalEventsChangedEventArgs> LocalEventsAdded;
public event EventHandler<LocalEventsChangedEventArgs> LocalEventsRemoved;
public event EventHandler<LocalEventsClearedEventArgs> LocalEventsCleared;
public event EventHandler<TransactionalEventsChangedEventArgs> TransactionalEventAdded;
public event EventHandler<TransactionalEventsChangedEventArgs> TransactionalEventRemoved;
public event EventHandler<TransactionalEventsClearedEventArgs> TransactionalEventsCleared;


/// <inheritdoc/>
Expand All @@ -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<LocalEventsChangedEventArgs> handler = LocalEventsAdded;
EventHandler<TransactionalEventsChangedEventArgs> handler = TransactionalEventAdded;
if (handler != null)
{
handler(this, args);
}
}

protected void OnLocalEventsRemoved(LocalEventsChangedEventArgs args)
protected void OnLocalEventsRemoved(TransactionalEventsChangedEventArgs args)
{
EventHandler<LocalEventsChangedEventArgs> handler = LocalEventsRemoved;
EventHandler<TransactionalEventsChangedEventArgs> handler = TransactionalEventRemoved;
if (handler != null)
{
handler(this, args);
}
}

protected void OnLocalEventsCleared(LocalEventsClearedEventArgs args)
protected void OnLocalEventsCleared(TransactionalEventsClearedEventArgs args)
{
EventHandler<LocalEventsClearedEventArgs> handler = LocalEventsCleared;
EventHandler<TransactionalEventsClearedEventArgs> handler = TransactionalEventsCleared;
if (handler != null)
{
handler(this, args);
Expand Down
15 changes: 0 additions & 15 deletions Src/RCommon.Entities/EntityChangedEvent.cs

This file was deleted.

15 changes: 0 additions & 15 deletions Src/RCommon.Entities/EntityCreatedEvent.cs

This file was deleted.

15 changes: 0 additions & 15 deletions Src/RCommon.Entities/EntityDeletedEvent.cs

This file was deleted.

15 changes: 0 additions & 15 deletions Src/RCommon.Entities/EntityUpdatedEvent.cs

This file was deleted.

21 changes: 0 additions & 21 deletions Src/RCommon.Entities/LocalEventsClearedEventArgs.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

namespace RCommon.Entities
{
public class EntityEvent<TEntity> : ISyncEvent
public class TransactionalEventsClearedEventArgs : EventArgs
{
public EntityEvent(TEntity entity)
public TransactionalEventsClearedEventArgs(IBusinessEntity entity)
{
Entity = entity;
}

public TEntity Entity { get; }
public IBusinessEntity Entity { get; }
}
}
9 changes: 3 additions & 6 deletions Src/RCommon.Linq2Db/Crud/Linq2DbRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ private IQueryable<TEntity> FindCore(Expression<Func<TEntity, bool>> expression)

public async override Task AddAsync(TEntity entity, CancellationToken token = default)
{
await DataConnection.InsertAsync(entity, token: token);
entity.AddLocalEvent(new EntityCreatedEvent<TEntity>(entity));
EventTracker.AddEntity(entity);
await DataConnection.InsertAsync(entity, token: token);
}

public async override Task<bool> AnyAsync(Expression<Func<TEntity, bool>> expression, CancellationToken token = default)
Expand All @@ -136,9 +135,8 @@ public async override Task<bool> AnyAsync(ISpecification<TEntity> specification,

public async override Task DeleteAsync(TEntity entity, CancellationToken token = default)
{
await DataConnection.DeleteAsync(entity);
entity.AddLocalEvent(new EntityDeletedEvent<TEntity>(entity));
EventTracker.AddEntity(entity);
await DataConnection.DeleteAsync(entity);
}

public override IQueryable<TEntity> FindQuery(ISpecification<TEntity> specification)
Expand Down Expand Up @@ -227,9 +225,8 @@ public async override Task<long> GetCountAsync(Expression<Func<TEntity, bool>> e

public async override Task UpdateAsync(TEntity entity, CancellationToken token = default)
{
await DataConnection.UpdateAsync(entity, token: token);
entity.AddLocalEvent(new EntityUpdatedEvent<TEntity>(entity));
EventTracker.AddEntity(entity);
await DataConnection.UpdateAsync(entity, token: token);
}
}
}

0 comments on commit b761582

Please sign in to comment.