Composition versus inheritance for Equality & Hashcode providers

When comparing entities and aggregate roots, I use the ABC, which I borrowed from Oren Eini: Generic Equity Equality . For value objects, I was just as inventive. I used Jimmy Bogards Value Object ABC: Uniformity of Universal Value

Now my question is: should I approve the inheritance of these ABCs or should I use common equality / and compose the above behavior instead? I intend to reuse the above implementations.

I think this goes back to System.Object, which has a standard Equals implementation that makes the language bloody easy to use, but also offers interesting troubles. John Skeet describes in detail here: Matching Methods

Can anyone think of any pro or contra?

  • ABC inheritance is simpler and offers operator overloading out of the box.
  • Should I “drag” two abstract classes with me so much? This makes my inheritance schedule more complicated / adds traction.
  • Could I use DI to create equality providers?

So, to summarize, does anyone have experience using custom equality implementations for entities? My goal is to collect a more complete understanding of the implementation options and implement a solution that will extend the life of the project (reduce entropy) and improve maintainability.

+3
source share
1 answer

This does not answer your question (sorry!), But I think this is a project that is trying to help with some of the questions that you have. I wrote this material a little back, but haven’t done anything about it since then.

Essence (http://essence.codeplex.com/), System.Linq.Expression ( ) Equals/GetHashCode/CompareTo/ToString, IEqualityComparer IComparer . ( , , .)

( , , - , , - CompareTo(), Linq.Expressions 3.5 - CompareTo() , . DLR- Linq.Expressions . , emit il, .)

, .

, ( codeproject, ..), , - .

( codeplex , - , f # ( #), , .)

, # .

    // --------------------------------------------------------------------
    // USING MY ESSENCE LIBRARY:
    // --------------------------------------------------------------------
    [EssenceClass(UseIn = EssenceFunctions.All)]
    public class TestEssence : IEquatable<TestEssence>, IComparable<TestEssence>
    {
        [Essence(Order=0, Format="i={0},")]           public int MyInt           { get; set; }
        [Essence(Order=1, Format="s={0},")]           public string MyString     { get; set; }
        [Essence(Order=2, Format="d={0:yyyy-MM-dd}")] public DateTime MyDateTime { get; set; }

        public override int GetHashCode()                                { return Essence<TestEssence>.GetHashCodeStatic(this); }
        public override string ToString()                                { return Essence<TestEssence>.ToStringStatic(this); }
        public int CompareTo(TestEssence other)                          { return Essence<TestEssence>.CompareToStatic(this, other); }

        public static bool operator ==(TestEssence lhs, TestEssence rhs) { return Essence<TestEssence>.EqualsStatic(lhs, rhs); }
        public override bool Equals(object obj)                          { return this == (TestEssence)obj; }
        public bool Equals(TestEssence other)                            { return this == other; }
        public static bool operator !=(TestEssence lhs, TestEssence rhs) { return !(lhs == rhs); }
    }

    // --------------------------------------------------------------------
    // EQUIVALENT HAND WRITTEN CODE:
    // --------------------------------------------------------------------
    public class TestManual
    {
        public int MyInt;
        public string MyString;
        public DateTime MyDateTime;

        public override int GetHashCode()
        {
            var x = MyInt.GetHashCode();
            x *= Essence<TestEssence>.HashCodeMultiplier;
            x ^= (MyString == null) ? 0 : MyString.GetHashCode();
            x *= Essence<TestEssence>.HashCodeMultiplier;
            x ^= MyDateTime.GetHashCode();
            return x;
        }

        public static bool operator ==(TestManual lhs, TestManual rhs)
        {
            if (ReferenceEquals(lhs, null))
            {
                if (ReferenceEquals(rhs, null))
                    return true;
                return false;
            }
            if (ReferenceEquals(rhs, null))
                return false;
            if (ReferenceEquals(lhs, rhs))
                return true;
            if (typeof(TestManual) != rhs.GetType())
                return false;

            return lhs.MyInt == rhs.MyInt
                && lhs.MyString == rhs.MyString
                && lhs.MyDateTime == rhs.MyDateTime;
        }

        public override bool Equals(object obj)                 { return this == obj as TestManual; }
        public bool Equals(TestManual other)                    { return this == other; }
        public static bool operator !=(TestManual lhs, TestManual rhs) { return !(lhs == rhs); }

        public override string ToString()
        {
            if (MyString == null)
                return string.Format("i={0},d={1:yyyy-MM-dd}", MyInt, MyDateTime);

            return string.Format("i={0},s={1},d={2:yyyy-MM-dd}", MyInt, MyString, MyDateTime);
        }

        public int CompareTo(TestManual other)
        {
            if (other == null)
                return 1;
            if (ReferenceEquals(this, other))
                return 0;

            int result = 0;

            result = MyInt.CompareTo(other.MyInt);
            if (result != 0) return result;
            result = MyString.CompareTo(other.MyString);
            if (result != 0) return result;
            result = MyDateTime.CompareTo(other.MyDateTime);
            if (result != 0) return result;

            return result;
        }
    }

    // --------------------------------------------------------------------
    // --------------------------------------------------------------------
    // ALTERNATIVE USAGE
    // --------------------------------------------------------------------
    // --------------------------------------------------------------------

    class Simple
    {
        public Simple(int value) { Value1 = value; }
        public Simple(int value1, int value2) { Value1 = value1; Value2 = value2; }
        public readonly int Value1;
        public readonly int Value2;
    }

    [Test]
    public void TestReverseForwardString()
    {
        var _11 = new Simple(1, 1);
        var _12 = new Simple(1, 2);
        var _21 = new Simple(2, 1);
        var _22 = new Simple(2, 2);

        var items = new[] { _11, _12, _21, _22 };
        var reverseComparer = Essence<Simple>.CreateComparer("-Value1", "Value2");

        Array.Sort(items, reverseComparer);

        Assert.AreSame(_21, items[0]);
        Assert.AreSame(_22, items[1]);
        Assert.AreSame(_11, items[2]);
        Assert.AreSame(_12, items[3]);
    }

    [Test]
    public void TestReverseForwardLambda()
    {
        var _11 = new Simple(1, 1);
        var _12 = new Simple(1, 2);
        var _21 = new Simple(2, 1);
        var _22 = new Simple(2, 2);

        var items = new[] { _11, _12, _21, _22 };
        var reverseComparer = Essence<Simple>.CreateComparer(x => x.Action.ReverseNext, x => x.Member.Value1, x => x.Member.Value2);

        Array.Sort(items, reverseComparer);

        Assert.AreSame(_21, items[0]);
        Assert.AreSame(_22, items[1]);
        Assert.AreSame(_11, items[2]);
        Assert.AreSame(_12, items[3]);
    }

!

.

0

Source: https://habr.com/ru/post/1706112/


All Articles