コード例 #1
0
ファイル: Dwarf.cs プロジェクト: GreenfieldVentures/Dwarf
        /// <summary>
        /// Dwarf comparison is done via the Id property
        /// </summary>
        public override bool Equals(object obj)
        {
            if (obj is IDwarf)
            {
                var type = DwarfHelper.DeProxyfy(GetType());

                if (type != DwarfHelper.DeProxyfy(obj))
                {
                    return(false);
                }

                if (!type.Implements <ICompositeId>())
                {
                    //Should both objects not be stored they'll both have null as Id, thus we check all the properties to find out if they might be the same object
                    //We also dubbelcheck all unique properties (if existant) to make sure they're NOT equal
                    if (!Id.HasValue && !((IDwarf)obj).Id.HasValue)
                    {
                        var propertiesMatch = DwarfHelper.GetDBProperties(type).Where(ep => ep.GetValue(this) != null && ep.GetValue(obj) != null).All(pi => pi.GetValue(this).Equals(pi.GetValue(obj)));

                        var uniqueProps = DwarfHelper.GetUniqueDBProperties <T>(type).ToList();

                        if (uniqueProps.Any())
                        {
                            var uniquePropertiesMatch = uniqueProps.All(ep => ep.GetValue(this) == null ? ep.GetValue(obj) == null : ep.GetValue(this).Equals(ep.GetValue(obj)));

                            return(uniquePropertiesMatch && propertiesMatch);
                        }

                        return(propertiesMatch);
                    }

                    return(Id == ((IDwarf)obj).Id);
                }

                return(Cfg.PKProperties[type].All(ep => ep.GetValue(this) == null ? ep.GetValue(obj) == null : ep.GetValue(this).Equals(ep.GetValue(obj))));
            }

            return(false);
        }