From 8298bdd87c3f7c39ff366ab4f4a5f58c197b0302 Mon Sep 17 00:00:00 2001 From: Matthias Gernand Date: Thu, 16 Dec 2021 04:05:28 +0100 Subject: [PATCH] Added the [Key] attribute to the ID property. (#6) --- src/Fluxera.Entity/Entity.cs | 55 +++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/src/Fluxera.Entity/Entity.cs b/src/Fluxera.Entity/Entity.cs index ff9a196..d52449e 100644 --- a/src/Fluxera.Entity/Entity.cs +++ b/src/Fluxera.Entity/Entity.cs @@ -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; /// @@ -39,12 +40,6 @@ public abstract class Entity : INotifyPropertyChanging, INotifyPr /// private const int HashMultiplier = 37; - /// - public event PropertyChangingEventHandler? PropertyChanging; - - /// - public event PropertyChangedEventHandler? PropertyChanged; - protected Entity() { this.Init(); @@ -53,6 +48,7 @@ protected Entity() /// /// The unique ID of the entity. /// + [Key] public virtual TKey? ID { get; set; } /// @@ -62,14 +58,20 @@ protected Entity() public ICollection DomainEvents { get; } = new List(); /// - /// 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). /// - [Ignore] + [Ignore] public virtual bool IsTransient => Equals(this.ID, default); + /// + public event PropertyChangedEventHandler? PropertyChanged; + + /// + public event PropertyChangingEventHandler? PropertyChanging; + public static bool operator ==(Entity? left, Entity? right) { - if (left is null) + if(left is null) { return right is null; } @@ -90,7 +92,7 @@ public override bool Equals(object? obj) return false; } - if (ReferenceEquals(this, obj)) + if(ReferenceEquals(this, obj)) { return true; } @@ -100,7 +102,7 @@ public override bool Equals(object? obj) return false; } - if (this.HasSameNonDefaultIdentifierAs(other)) + if(this.HasSameNonDefaultIdentifierAs(other)) { return true; } @@ -108,8 +110,8 @@ public override bool Equals(object? obj) // 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()); } @@ -137,8 +139,8 @@ public override int GetHashCode() } /// - /// 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. /// /// public virtual Type GetUnProxiedType() @@ -147,17 +149,17 @@ public virtual Type GetUnProxiedType() } /// - /// Gets all components of the entity that are used for equality (domain signature).
- /// The default implementation get all properties via reflection. One - /// can at any time override this behavior with a manual or custom implementation. - ///

- /// 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).
+ /// The default implementation get all properties via reflection. One + /// can at any time override this behavior with a manual or custom implementation. + ///

+ /// To add properties to the domain signature you should decorate the appropriate property(s) + /// with [DomainSignature] and they will be compared automatically. ///
/// The components to use for equality. protected virtual IEnumerable GetEqualityComponents() { - PropertyAccessor[] propertyAccessors = PropertyAccessor.GetPropertyAccessors(this.GetType(), + PropertyAccessor[] propertyAccessors = PropertyAccessor.GetPropertyAccessors(this.GetType(), property => { bool isDomainSignatureAttribute = property.IsDefined(typeof(DomainSignatureAttribute), true); @@ -168,6 +170,7 @@ public virtual Type GetUnProxiedType() throw new InvalidOperationException($"The property {property.Name} cannot belong to the domain signature."); } } + return isDomainSignatureAttribute; }); @@ -182,9 +185,9 @@ protected void SetAndNotify(ref T? field, T? value, [CallerMemberName] string { if(!Equals(field, value)) { - OnPropertyChanging(propertyName); + this.OnPropertyChanging(propertyName); field = value; - OnPropertyChanged(propertyName); + this.OnPropertyChanged(propertyName); } }