ValeraT1982 / ObjectsComparer

C# Framework provides mechanism to compare complex objects, allows to override comparison rules for specific properties and types.
MIT License
352 stars 86 forks source link

Compare two objects with more than one list property #58

Open huangyy21 opened 6 months ago

huangyy21 commented 6 months ago

Hi, Form example4, I have known how to compare objects with one list. But if my object have more than one list, what should I do?

huangyy21 commented 6 months ago

I just tried to implement custom Comparer for every list property and it works. Is there any better way?

ValeraT1982 commented 6 months ago

Are types of the lists same? Can you give an example?

huangyy21 commented 6 months ago

No, the types are different, but all have an id property. For example, the demo class below.

public class Demo
{
    public string Name { get; set; }

    public List<A> ItemA { get; set; }

    public List<B> ItemB { get; set; }
}

public class A
{
    public int Id { get; set; }

    public string AA { get; set; }
}

public class B
{
    public int Id { get; set; }

    public int BB { get; set; }
}
huangyy21 commented 6 months ago

I create a class to implement custom Comparer making use of generics, and only have to add if statement in ComparersFactory for every type of lists.

ValeraT1982 commented 6 months ago

@huangyy21 you can inherit both classes from the same interface and use this interface in ComparersFactory instead of having separate if for each class.

reponemec commented 6 months ago

In my PR #47, you don't need to implement custom comparer at all. All you need to do is this:

var settings = new ComparisonSettings();
settings.ConfigureListComparison(compareElementsByKey: true, compareUnequalLists: true);
var comparer = new Comparer<Formula>(settings);
var isEqual = comparer.Compare(formula1, formula2, out var differences);

The complete test method: https://github.com/reponemec/ObjectsComparer/blob/35cd3fc60898ff44639146efe4efb9a108a5a5f7/ObjectsComparer/ObjectsComparer.Examples/Example4/Example4Tests_BuiltInKeyComparison.cs#L119

huangyy21 commented 6 months ago

In my PR #47, you don't need to implement custom comparer at all. All you need to do is this:

var settings = new ComparisonSettings();
settings.ConfigureListComparison(compareElementsByKey: true, compareUnequalLists: true);
var comparer = new Comparer<Formula>(settings);
var isEqual = comparer.Compare(formula1, formula2, out var differences);

The complete test method: https://github.com/reponemec/ObjectsComparer/blob/35cd3fc60898ff44639146efe4efb9a108a5a5f7/ObjectsComparer/ObjectsComparer.Examples/Example4/Example4Tests_BuiltInKeyComparison.cs#L119

Thanks for reply. My memberPath needs more information, so it may be better to implement custom comparer.

reponemec commented 6 months ago

My memberPath needs more information, so it may be better to implement custom comparer.

Absolutely, however the memberPath property can also be customized using ComparisonSettings: https://github.com/reponemec/ObjectsComparer/blob/35cd3fc60898ff44639146efe4efb9a108a5a5f7/ObjectsComparer/ObjectsComparer.Tests/DifferenceConfigurationTests.cs#L98