Skip to content

Commit

Permalink
Extended the item updated event. (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgernand authored Apr 11, 2022
1 parent 7a9e608 commit 5660125
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/Fluxera.Entity/DomainEvents/ItemAdded.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public sealed class ItemAdded<TAggregateRoot, TKey> : IDomainEvent
/// <param name="item">The underlying item of this event.</param>
public ItemAdded(TAggregateRoot item)
{
this.Item = item;
this.AddedItem = item;
}

/// <summary>
/// Gets the underlying item.
/// Gets the added item.
/// </summary>
public TAggregateRoot Item { get; }
public TAggregateRoot AddedItem { get; }
}
}
8 changes: 4 additions & 4 deletions src/Fluxera.Entity/DomainEvents/ItemRemoved.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ public sealed class ItemRemoved<TAggregateRoot, TKey> : IDomainEvent
/// <param name="id">The id of the underlying item of this event.</param>
public ItemRemoved(TAggregateRoot item, TKey id)
{
this.Item = item;
this.DeletedItem = item;
this.ID = id;
}

/// <summary>
/// Gets the underlying item.
/// Gets the deleted item (has no ID anymore).
/// </summary>
public TAggregateRoot Item { get; }
public TAggregateRoot DeletedItem { get; }

/// <summary>
/// Gets the id of underlying item.
/// Gets the id of deleted item.
/// </summary>
public TKey ID { get; }
}
Expand Down
17 changes: 12 additions & 5 deletions src/Fluxera.Entity/DomainEvents/ItemUpdated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@ public sealed class ItemUpdated<TAggregateRoot, TKey> : IDomainEvent
/// <summary>
/// Creates a new instance of the <see cref="ItemUpdated{TAggregateRoot,TKey}" /> type.
/// </summary>
/// <param name="item">The underlying item of this event.</param>
public ItemUpdated(TAggregateRoot item)
/// <param name="beforeUpdateItem">The underlying item of this event.</param>
/// <param name="afterUpdateItem">The underlying item of this event.</param>
public ItemUpdated(TAggregateRoot beforeUpdateItem, TAggregateRoot afterUpdateItem)
{
this.Item = item;
this.BeforeUpdateItem = beforeUpdateItem;
this.AfterUpdateItem = afterUpdateItem;
}

/// <summary>
/// Gets the underlying item.
/// Gets the item before the update.
/// </summary>
public TAggregateRoot Item { get; }
public TAggregateRoot BeforeUpdateItem { get; }

/// <summary>
/// Gets the updated item.
/// </summary>
public TAggregateRoot AfterUpdateItem { get; }
}
}
20 changes: 10 additions & 10 deletions src/Fluxera.Entity/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected Entity()
/// The unique ID of the entity.
/// </summary>
[Key]
public virtual TKey? ID { get; set; }
public virtual TKey ID { get; set; }

/// <summary>
/// The domain events of this entity.
Expand Down Expand Up @@ -68,18 +68,18 @@ public virtual bool IsTransient
}

/// <inheritdoc />
public event PropertyChangedEventHandler? PropertyChanged;
public event PropertyChangedEventHandler PropertyChanged;

/// <inheritdoc />
public event PropertyChangingEventHandler? PropertyChanging;
public event PropertyChangingEventHandler PropertyChanging;

/// <summary>
/// Checks two instances of <see cref="Entity{TEntity,TKey}" /> are equal.
/// </summary>
/// <param name="left">The left item of the operator.</param>
/// <param name="right">The left item of the operator.</param>
/// <returns>True, if the instances are equal.</returns>
public static bool operator ==(Entity<TEntity, TKey>? left, Entity<TEntity, TKey>? right)
public static bool operator ==(Entity<TEntity, TKey> left, Entity<TEntity, TKey> right)
{
if(left is null)
{
Expand All @@ -95,13 +95,13 @@ public virtual bool IsTransient
/// <param name="left">The left item of the operator.</param>
/// <param name="right">The left item of the operator.</param>
/// <returns>True, if the instances are equal.</returns>
public static bool operator !=(Entity<TEntity, TKey>? left, Entity<TEntity, TKey>? right)
public static bool operator !=(Entity<TEntity, TKey> left, Entity<TEntity, TKey> right)
{
return !(left == right);
}

/// <inheritdoc />
public override bool Equals(object? obj)
public override bool Equals(object obj)
{
if(obj is null)
{
Expand Down Expand Up @@ -142,7 +142,7 @@ public override int GetHashCode()
// so we include the value object type in the hash calculation
int hashCode = this.GetType().GetHashCode();

foreach(object? component in this.GetEqualityComponents())
foreach(object component in this.GetEqualityComponents())
{
if(component != null)
{
Expand Down Expand Up @@ -173,7 +173,7 @@ public virtual Type GetUnProxiedType()
/// with [DomainSignature] and they will be compared automatically.
/// </summary>
/// <returns>The components to use for equality.</returns>
protected virtual IEnumerable<object?> GetEqualityComponents()
protected virtual IEnumerable<object> GetEqualityComponents()
{
PropertyAccessor[] propertyAccessors = PropertyAccessor.GetPropertyAccessors(this.GetType(),
property =>
Expand All @@ -192,7 +192,7 @@ public virtual Type GetUnProxiedType()

foreach(PropertyAccessor accessor in propertyAccessors)
{
object? value = accessor.Invoke(this);
object value = accessor.Invoke(this);
yield return value;
}
}
Expand All @@ -204,7 +204,7 @@ public virtual Type GetUnProxiedType()
/// <param name="field">The field reference to set.</param>
/// <param name="value">The value to set.</param>
/// <param name="propertyName">The name of the used property.</param>
protected void SetAndNotify<T>(ref T? field, T? value, [CallerMemberName] string propertyName = null!)
protected void SetAndNotify<T>(ref T field, T value, [CallerMemberName] string propertyName = null!)
{
if(!Equals(field, value))
{
Expand Down
10 changes: 5 additions & 5 deletions src/Fluxera.Entity/PropertyAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Collections.Concurrent;
using System.Linq;
using System.Reflection;
using Guards;
using Fluxera.Guards;
using JetBrains.Annotations;

[PublicAPI]
Expand All @@ -27,7 +27,7 @@ private PropertyAccessor(string propertyName, Func<object, object> getterFunc)

private Func<object, object> GetterFunc { get; }

public object? Invoke(object target)
public object Invoke(object target)
{
return this.GetterFunc.Invoke(target);
}
Expand All @@ -40,12 +40,12 @@ public static PropertyAccessor[] GetPropertyAccessors(Type type, Predicate<Prope
.Where(predicate.Invoke)
.Select(property =>
{
MethodInfo? getMethod = property.GetMethod;
Type? declaringType = property.DeclaringType;
MethodInfo getMethod = property.GetMethod;
Type declaringType = property.DeclaringType;
Type propertyType = property.PropertyType;
Type getMethodDelegateType = typeof(Func<,>).MakeGenericType(declaringType, propertyType);
Delegate? getMethodDelegate = getMethod.CreateDelegate(getMethodDelegateType);
Delegate getMethodDelegate = getMethod.CreateDelegate(getMethodDelegateType);
MethodInfo callInnerGenericMethodWithTypes = callInnerDelegateMethod.MakeGenericMethod(declaringType, propertyType);
Func<object, object> getter = (Func<object, object>)callInnerGenericMethodWithTypes.Invoke(null, new object[] { getMethodDelegate });
Expand Down

0 comments on commit 5660125

Please sign in to comment.