Skip to content

Commit

Permalink
Added the [Key] attribute to the ID property. (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgernand authored Dec 16, 2021
1 parent 689372a commit 8298bdd
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions src/Fluxera.Entity/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.CompilerServices;
using ComponentModel.Annotations;
using DomainEvents;
using Fluxera.ComponentModel.Annotations;
using Fluxera.Entity.DomainEvents;
using JetBrains.Annotations;

/// <summary>
Expand Down Expand Up @@ -39,12 +40,6 @@ public abstract class Entity<TEntity, TKey> : INotifyPropertyChanging, INotifyPr
/// </remarks>
private const int HashMultiplier = 37;

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

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

protected Entity()
{
this.Init();
Expand All @@ -53,6 +48,7 @@ protected Entity()
/// <summary>
/// The unique ID of the entity.
/// </summary>
[Key]
public virtual TKey? ID { get; set; }

/// <summary>
Expand All @@ -62,14 +58,20 @@ protected Entity()
public ICollection<IDomainEvent> DomainEvents { get; } = new List<IDomainEvent>();

/// <summary>
/// Gets a flag, if the entity instance is transient (not stored to the storage).
/// Gets a flag, if the entity instance is transient (not stored to the storage).
/// </summary>
[Ignore]
[Ignore]
public virtual bool IsTransient => Equals(this.ID, default);

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

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

public static bool operator ==(Entity<TEntity, TKey>? left, Entity<TEntity, TKey>? right)
{
if (left is null)
if(left is null)
{
return right is null;
}
Expand All @@ -90,7 +92,7 @@ public override bool Equals(object? obj)
return false;
}

if (ReferenceEquals(this, obj))
if(ReferenceEquals(this, obj))
{
return true;
}
Expand All @@ -100,16 +102,16 @@ public override bool Equals(object? obj)
return false;
}

if (this.HasSameNonDefaultIdentifierAs(other))
if(this.HasSameNonDefaultIdentifierAs(other))
{
return true;
}

// Since the IDs aren't the same, both of them must be transient to
// compare domain signatures; because if one is transient and the
// other is a persisted entity, then they cannot be the same object.
return this.GetType() == other.GetUnProxiedType()
&& this.IsTransient
return (this.GetType() == other.GetUnProxiedType())
&& this.IsTransient
&& other.IsTransient
&& this.GetEqualityComponents().SequenceEqual(other.GetEqualityComponents());
}
Expand Down Expand Up @@ -137,8 +139,8 @@ public override int GetHashCode()
}

/// <summary>
/// Some OR mappers may create dynamic proxies , so this method
/// gets into the proxied object to get its actual type.
/// Some OR mappers may create dynamic proxies , so this method
/// gets into the proxied object to get its actual type.
/// </summary>
/// <returns></returns>
public virtual Type GetUnProxiedType()
Expand All @@ -147,17 +149,17 @@ public virtual Type GetUnProxiedType()
}

/// <summary>
/// Gets all components of the entity that are used for equality (domain signature). <br/>
/// The default implementation get all properties via reflection. One
/// can at any time override this behavior with a manual or custom implementation.
/// <br/><br/>
/// To add properties to the domain signature you should decorate the appropriate property(s)
/// with [DomainSignature] and they will be compared automatically.
/// Gets all components of the entity that are used for equality (domain signature). <br />
/// The default implementation get all properties via reflection. One
/// can at any time override this behavior with a manual or custom implementation.
/// <br /><br />
/// To add properties to the domain signature you should decorate the appropriate property(s)
/// with [DomainSignature] and they will be compared automatically.
/// </summary>
/// <returns>The components to use for equality.</returns>
protected virtual IEnumerable<object?> GetEqualityComponents()
{
PropertyAccessor[] propertyAccessors = PropertyAccessor.GetPropertyAccessors(this.GetType(),
PropertyAccessor[] propertyAccessors = PropertyAccessor.GetPropertyAccessors(this.GetType(),
property =>
{
bool isDomainSignatureAttribute = property.IsDefined(typeof(DomainSignatureAttribute), true);
Expand All @@ -168,6 +170,7 @@ public virtual Type GetUnProxiedType()
throw new InvalidOperationException($"The property {property.Name} cannot belong to the domain signature.");
}
}
return isDomainSignatureAttribute;
});

Expand All @@ -182,9 +185,9 @@ protected void SetAndNotify<T>(ref T? field, T? value, [CallerMemberName] string
{
if(!Equals(field, value))
{
OnPropertyChanging(propertyName);
this.OnPropertyChanging(propertyName);
field = value;
OnPropertyChanged(propertyName);
this.OnPropertyChanged(propertyName);
}
}

Expand Down

0 comments on commit 8298bdd

Please sign in to comment.