c# - Updating different Objects that have same properties -
in code, i've got 3 (or more...) different objects, have same properties (e.g. trackingitemtype, readerid etc).
they derived counterbase (which not have properties needed, others).
now want iterate through collection of objects. in collection, of 3 objects can occur. therefore, i've implemented "update" each object seperately.
question: how can avoid duplicate code written? there pattern available update different objects same properties?!
thanks!
method of interest:
private void updatetrackingcounters(reading readingdata, trackingitemtype trackingitemtype, string trackingcountertype) { try { foreach (counterbase counter in this.trackingcounters) { if (typeof(trackingsimplecounter).isinstanceoftype(counter)) { trackingsimplecounter trackingcounter = (trackingsimplecounter)counter; if (trackingcounter.trackingcountertype == trackingcountertype) { if ((trackingcounter.readerid == 0 && trackingcounter.trackingitemtype == null) || (trackingcounter.readerid == 0 && trackingcounter.trackingitemtype == trackingcounter.trackingitemtype) || (trackingcounter.readerid == readingdata.readerid && trackingcounter.trackingitemtype == null) || (trackingcounter.readerid == readingdata.readerid && trackingcounter.trackingitemtype == trackingcounter.trackingitemtype) ) { trackingcounter.increaseone(); } } } else if (typeof(trackingintervalbasedrollingcounter).isinstanceoftype(counter)) { trackingintervalbasedrollingcounter trackingcounter = (trackingintervalbasedrollingcounter)counter; if (trackingcounter.trackingcountertype == trackingcountertype) { if ((trackingcounter.readerid == 0 && trackingcounter.trackingitemtype == null) || (trackingcounter.readerid == 0 && trackingcounter.trackingitemtype == trackingcounter.trackingitemtype) || (trackingcounter.readerid == readingdata.readerid && trackingcounter.trackingitemtype == null) || (trackingcounter.readerid == readingdata.readerid && trackingcounter.trackingitemtype == trackingcounter.trackingitemtype) ) { trackingcounter.increaseone(); } } } else if (typeof(trackingtriggerbasedrollingcounter).isinstanceoftype(counter)) { trackingtriggerbasedrollingcounter trackingcounter = (trackingtriggerbasedrollingcounter)counter; if (trackingcounter.trackingcountertype == trackingcountertype) { if ((trackingcounter.readerid == 0 && trackingcounter.trackingitemtype == null) || (trackingcounter.readerid == 0 && trackingcounter.trackingitemtype == trackingcounter.trackingitemtype) || (trackingcounter.readerid == readingdata.readerid && trackingcounter.trackingitemtype == null) || (trackingcounter.readerid == readingdata.readerid && trackingcounter.trackingitemtype == trackingcounter.trackingitemtype) ) { trackingcounter.increaseone(); } } } } } catch (exception ex) { this.trace.error(ex); } }
define interface contains properties , method need:
interface itrackable { int readerid; string trackingitemtype; void increaseone(); }
and add declaration of each class has them:
class trackingsimplecounter : counterbase, itrackable class trackingintervalbasedrollingcounter: counterbase, itrackable class trackingtriggerbasedrollingcounter : counterbase, itrackable
if classes share properties, won't have implement of interface, since it'll present.
then need is
foreach (itrackable counter in this.trackingcounters) { if ((counter.readerid == 0 && counter.trackingitemtype == null) || (counter.readerid == 0 && counter.trackingitemtype == counter.trackingitemtype) || (counter.readerid == readingdata.readerid && counter.trackingitemtype == null) || (counter.readerid == readingdata.readerid && counter.trackingitemtype == counter.trackingitemtype) ) { counter.increaseone(); } }
although unless misunderstand logic need is:
foreach (itrackable counter in this.trackingcounters) { if (counter.readerid == 0 || counter.readerid == readingdata.readerid) { counter.increaseone(); } }
or if want use linq:
foreach (var counter in this.trackingcounters.oftype<itrackable>().where(c => c.readerid == 0 || c.readerid = readingdata.readerid)) { counter.increaseone(); }
Comments
Post a Comment