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

Ignore members without overriding Comparer<T> #12

Closed ianmann56 closed 4 years ago

ianmann56 commented 4 years ago

Allow caller to set fields on which the comparer should ignore. This could be by member name, by member type or by lambda that takes the MemberInfo of the member and a boolean that represents whether or not to ignore the field.

Example interface may look like this:

Assume class Person

public class Person
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string State { get; set; }

    [SomeCustomAttribute]
    public List<Person> Friends { get; set; }
}

Exclude members by name

Comparer<Person> comparer = new Comparer<Person>();
comparer.IgnoreMember("Age");
// Person's age will not be considered when calling comparer.CalculateDifferences(...).

Exclude members by type

Comparer<Person> comparer = new Comparer<Person>();
comparer.IgnoreMember(typeof(string));
// Person's age, name and state will not be considered when calling comparer.CalculateDifferences(...).

Exclude members based on lambda

Comparer<Person> comparer = new Comparer<Person>();
comparer.IgnoreMember(memberInfo =>
                                        memberInfo.IsDefined(typeof(SomeCustomAttributeAttribute)));
// Person's list of friends will not be considered when calling comparer.CalculateDifferences(...).

Extra

Consider implementing this and the overriding ValueComarers in a builder pattern:

Comparer<Person> comparer = new Comparer<Person>()
                                                    .WithValueOverride(...)
                                                    .WithValueOverride(...)
                                                    .WithMemborIgnorance(...);
ValeraT1982 commented 4 years ago

@ianbro thanks for the ideas. Sounds like a good enhancements. I'll think about implementing it in the next version.

ianmann56 commented 4 years ago

Are you accepting PR's if I fork and implement it myself?

ValeraT1982 commented 4 years ago

Yes. Sound like a reasonable change.

ValeraT1982 commented 4 years ago

Ignore member methods were added in version 1.4.0

I'm still thinking about adding **With*** methods. Haven't decided yet. Feel free to provide any thoughts about it.

ianmann56 commented 4 years ago

Ignore member methods were added in version 1.4.0

I'm still thinking about adding **With*** methods. Haven't decided yet. Feel free to provide any thoughts about it.

See: https://github.com/ValeraT1982/ObjectsComparer/pull/13#issuecomment-557281492

ValeraT1982 commented 4 years ago

@ianmann56 I added override/ignore by member filter as you described. Please have a look. If it's what you want I'll publish new version with this change.

ianmann56 commented 4 years ago

@ianmann56 I added override/ignore by member filter as you described. Please have a look. If it's what you want I'll publish new version with this change.

I saw it. Perfect! Thank you so much and sorry for not making the request more clear in the first place.