Open huangyy21 opened 6 months ago
I just tried to implement custom Comparer for every list property and it works. Is there any better way?
Are types of the lists same? Can you give an example?
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; }
}
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.
@huangyy21 you can inherit both classes from the same interface and use this interface in ComparersFactory instead of having separate if for each class.
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
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.
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
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?