c# - How best to over come 2 different DB system designs using OOP? -
i need write middle ware code move data between 2 third party student systems. these systems both have own different web service api. plan build custom wrapper on top of each web service api , build common assembly on top of both wrappers move data between systems , allow further development on common code base. there 12 other applications using these systems directly , maintenance becoming unmanageable. want build can migrated to.
in pseudo-code
//system 1 out puts class student_sytema { public studentid {get;set;} //pkid system public fname {get;set;} public lname {get;set;} public dob {get;set;} } //system 2 out puts class student_systemb{ public contactid {get;set;} //pk systemb public firstname {get;set;} public lastname {get;set;} public dateofbirth {get;set;} public studentid_ref {get;set;} //reference system } this sort of thing endemic through out both systems holding exact same information under different field names or data structures.
my thought have this
class student_comman{ public contactid {get;set;} public firstname {get;set;} public lastname {get;set;} public dateofbirth {get;set;} public studentid_ref {get;set;} public wireupsystema(student_systema student){ studentid_ref = student .studentid; firstname = student .fname ; lastname = student .lname ; dateofbirth = student .dob ; } public wireupsystemb(student_systemb student){ studentid_ref = student . studentid_ref ; firstname = student . firstname ; lastname = student . lastname; dateofbirth = student . dateofbirth; } } how go bringing these architecturally using oop writing , maintaining least amount of wiring code? use inheritance if can cut down coding , maintenance? there better way of going this?
for cases yours, prefer reflection/generic approach.
mark properties of , b attribute tells property must mapped in target type , create 1 generic converter:
//custom attribute class sealed class redirectedpropertyattribute : attribute { readonly string targetproperty; public redirectedpropertyattribute(string targetproperty) { this.targetproperty = targetproperty; } public string targetproperty { { return targetproperty; } } } //type converter public class typeconverter { public static t convert<t, s>(s source) t : class, new() s : class, new() { //if no instance passed return null if (source == null) return null; //get types of items type typeoftarget = typeof(t); type typeofsource = typeof(s); //get properties of items var sourceproperties = typeofsource.getproperties(); var targetproperties = typeoftarget.getproperties(); //create new instance of t var instance = activator.createinstance<t>(); foreach (var prop in sourceproperties) { //get or attributes var attribs = prop.getcustomattributes(typeof(redirectedpropertyattribute), false); //if want inherit attributes change yes //if it's not marked or marked more once, continue (really bad error ;)) if (attribs == null || attribs.length != 1) continue; //cast attribute redirectedpropertyattribute attrib = attribs[0] redirectedpropertyattribute; //no property set? ignore property if (string.isnullorwhitespace(attrib.targetproperty)) continue; //find target property in target type var tprop = targetproperties.where(t => t.name == attrib.targetproperty).firstordefault(); //not found? ignore property if (tprop == null) continue; try { //why try-catch? //because if types don't match exception can thrown //but it's easier comparing types (because if int mapped long want set) //warning!!, assuming non-indexed properties! tprop.setvalue(instance, prop.getvalue(source, null), null); } catch { } } //return new class return instance; } } //class source public class { [redirectedproperty("id")] public int idofa { get; set; } [redirectedproperty("name")] public string stringofa { get; set; } } //class source b public class b { [redirectedproperty("id")] public int idofb { get; set; } [redirectedproperty("name")] public string stringofb { get; set; } } //hub class or b public class abhub { public int id { get; set; } public string name { get; set; } } //and use: abhub acasted = typeconverter.convert<abhub, a>(new a{ idofa = 33, stringofa = "mynameisa" }); abhub bcasted = typeconverter.convert<abhub, b>(new b{ idofb = 33, stringofb = "mynameisb" });
Comments
Post a Comment