The issue I'm currently having is mapping multiple GUI fields to object properties (i.e. Presentation layer to Business Logic Layer mapping). To be more specific, this is in VB.Net 2.0 WinForms.
The nature of the solution requires us to have 4 columns which exhibit the same type of behavior on our GUI - each column consisting of 11 textboxes (we'll just use this small sample size, as the problem extends beyond 11 textboxes).
What I am currently doing is setting the tag of each textbox in all four columns to a value like so:
Textbox1.tag = "name"
Textbox2.tag = "type"
Textbox3.tag = "speed"
When an event is raised by the textbox (e.g. keypress), I look at the parent container, whose tag I have also set as a string mapping a specific object. I use that, in conjunction with the textbox's tag, to determine what object's property I need to set. Overall, it looks something like this:
dim objectToMapTo //the generic parent object which all my custom myObjects inherit from
select case sender.parent.tag //the parent object that the property needs to map to
case "column1"
objectToMapTo = myObject1
case "column2"
objectToMapTo = myObject2
case "column3"
objectToMapTo = myObject3
case "column4"
objectToMapTo = myObject4
end select
select case sender.tag //the actual textbox's tag value which maps to the property
case "name"
objectToMapTo.Name = sender.text //sender.text is conceptual for
//the data that needs to be set -- i.e. this could be a calculated
//number based on the text, or simply a string, etc
case "type"
objectToMapTo.Type = sender.text
case "speed"
objectToMapTo.Speed = sender.text
...
end select
As you can see this can get very bad, rather quickly. Currently we're setting 43 some odd properties that can be mapped to -- thus that select statement is extremely long -- many of which are embedded in multiple methods to try and attempt DRY (I've watered down the code to essentially a conceptual implementation).
Question is: how can I refactor this? I've attempted using a dictionaries/hashes to a certain extent, but it either became overly complex, or just plain didn't make an implementation sense as it convoluted the problem even more.
Thanks for the help.