C# Best way to compare fields of class -
i need have possibility of comparison of product, advancedproduct (and other classes inherit class product) how better realize hierarchical check of fields? example, want check 2 advancedproduct classes, first check fields basic class product check additional fields of advancedproduct , return in form (???) changes between them (maybe class pchanges???). whether there suitable template? how make this, make rather flexibly subsequent use?
public class product { public string id; public string name; public product(string id, string name) { this.id = id; this.name = name; } } public class advancedproduct : product { public string currentversion; public advancedproduct(string id, string name, string version) : base(id, name) { } } public class pchanges { public bool namechanged = false; public bool versionchanged = false; public pchanges() { } } public class productcomparer { pchanges changes = new pchanges(); public productcomparer() { } public pchanges compare(advancedproduct p1, advancedproduct p2) { if (p1.name != p2.name) changes.namechanged = true; if (p1.currentversion != p2.currentversion) changes.versionchanged = true; return changes; } }
based on uzzy's answer, looks can extended track change. bad practice, yes, small app should enough. example:
public class productcomparer : iequalitycomparer<product>{ private pchanges change; public pchanges changes{ { return change; } } public bool equals(product p1, product p2){ pchanges newchange = new pchanges(); bool equal = true; if(p1.name != p2.name){ newchange.namechange = true; equal = false; } this.change = newchange; return equal; } }
edit:
i misread requirement of extendable field comparison. if case, decorator pattern best you. assuming every other product class should inherited product class.
public class productcomparer{ public virtual void trackchange(product p1, product p2, ref pchange change){ if(p1.name != p2.name){ change.namechange = true; } // other base validation } } public class advancedproductcomparer : productcomparer{ public advancedproductcomparer(productcomparer basecomparer){ this.basecomparer = basecomparer; } productcomparer basecomparer; public override void trackchange(product p1, product p2, ref pchange change){ basecomparer.compare(p1, p2, ref change); if( ((advancedproduct)p1).currentversion != ((advancedproduct)p2).currentversion){ change.currentversion = true; } } } public class productcomparerservice{ public productcomparerservice(productcomparer comparer){ this.comparer = comparer; } productcomparer comparer; private pchanges change; public pchanges changes{ { return change; } } public bool equals(product p1, product p2){ pchanges newchange = new pchanges(); comparer.compare(p1,p2, ref newchange); this.change = newchange; return (newchange.currentversion || newchange.namechange); } }
the usage:
productcomparer pcompare = new productcomparer(); advancedproductcomparer advcompare = new advancedproductcomparer(pcompare); productcomparerservice service = new productcomparerservice(advcompare); if( service.equals(p1,p2) ){ pchange change = service.change; }
Comments
Post a Comment