我目前遇到的问题是将多个GUI字段映射到对象属性(即表示层到业务逻辑层映射)。更具体地说,这是在VB.Net 2.0 WinForms中。业务对象映射的GUI VB.Net
解决方案的性质要求我们有4列,它们在我们的GUI上表现出相同类型的行为 - 每列包含11个文本框(我们只使用这个小样本大小,因为问题超出了11个文本框)。
什么我目前在做的所有四列的设置每个文本框的标签值,像这样:
Textbox1.tag = "name"
Textbox2.tag = "type"
Textbox3.tag = "speed"
当一个事件是由文本框(如按键)提出的,我看父容器,其标签也设置为映射特定对象的字符串。我将它与文本框的标记一起使用来确定我需要设置的对象的属性。总的来说,它看起来像这样:
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
正如你所看到的,这可能会变得非常糟糕,相当快。目前我们设置了一些可以映射到的奇怪属性 - 因此select语句非常长 - 其中很多嵌入了多个方法来尝试和尝试DRY(我已经淡化了代码本质上是一个概念实现)。
问题是:我该如何重构这个?我试图在一定程度上使用字典/散列,但它要么变得过于复杂,要么只是简单地没有实现实现,因为它更加复杂化了问题。
感谢您的帮助。
实际上反射效果很好。唯一的问题是每个值都设置得更复杂一点。所以我们可能会把它像objectToMap.Customer.Name = sender.text,然后objectToMap.Asset.Type = sender.text - 因此它基本上需要获取/设置嵌套的属性,这给它一个复杂的添加层 – MunkiPhD 2009-06-17 16:14:24