2009-02-03 41 views
1

我正在使用以下函数将DataTable中的列(从数据层传递)映射到对象属性。该函数存在于我正在填充的类中。该类有两种方法:Load()和LoadAll(),LoadAll()返回一组已填充的对象。我希望能够使用相同的代码来填充当前对象或新对象。然而,我对结果不满意,主要是因为重复,这是一个维护的噩梦。如何重新分解这段代码?

Private Function MapDataRowToProperties(ByVal dr As DataRow, ByVal target As Incident) As Incident 
      If target.Equals(Me) Then 
       Me.ID = Convert.ToInt32(dr.Item("pkIncidentID")) 
       Me.Description = dr.Item("IncidentDetail").ToString 
       Me.Created = Convert.ToDateTime(dr.Item("CreatedOn")) 
       ... 
       Return Me 
      Else 
       Dim NewIncident As New Incident 
       NewIncident.ID = Convert.ToInt32(dr.Item("pkIncidentID")) 
       NewIncident.Description = dr.Item("IncidentDetail").ToString 
       NewIncident.Created = Convert.ToDateTime(dr.Item("CreatedOn")) 
       ... 
       Return NewIncident 
      End If 

    End Function 

注:我很清楚的ORM工具,会为我这样做,我通常使用EntitySpaces,但对于这个项目,我不能这样做。

+0

我可能是遥远,但在这里......在一个私人的功能,不应该针对几乎永远是我? – Svish 2009-02-03 13:11:15

回答

4

在我看来,你的IfElse块仅在一个假设现有对象的事实不同(目前实例)已被传递,另一个通过重新创建它来覆盖任何传递的引用。

这似乎像ByRef参数理想的情况下,换句话说,让调用代码传递不同的变量,并让此功能工作,在每种情况下以同样的方式:

Private Sub MapDataRowToProperties(ByVal dr As DataRow, ByRef target As Incident) 
    If target Is Nothing Then 
    target = New Incident() 
    End If 
    'Set properties 
    '... 
End Sub 

样品调用:

'Usage 1: 
MapDataRowToProperties(dr, Me) 

'Usage 2: 
Dim inc as New Incident() 
MapDataRowToProperties(dr, inc) 
6

如何:

dim Inc as Incident 
if target.Equals(me) then 
    Inc = Me 
else 
    Inc = new Incident 
end if 
'Other code' 
return Inc 

OTOH如果target.Equals(我)应该只匹配当前的对象(在这种情况下,你应该用的是或的ReferenceEquals,平等相待,可以重载返回TRUE其他对象),并且您在第二种情况下传递新事件(或者可以修改目标),然后删除if并直接使用target。

+0

无可否认,这是一个很好的解决方案,它让我想起了它。唯一的一点是,它对当前的情况太具体了。 ;-) – Cerebrus 2009-02-03 13:04:44

0

希望这会有所帮助。 (请原谅我在语法上的错误,因为vb编码已经很久了)。

在调用函数

If target.Equals(Me) Then 

    target = MapDataRosToProperties(target,dr) 
Else 

    Dim target as Incident 

    target = MapDataRosToProperties(target,dr) 

重构功能

Private Function MapDataRowToProperties(ByVal target as Incident,ByVal dr as DataRow) As Incident 

    target .ID = Convert.ToInt32(dr.Item("pkIncidentID")) 
    target .Description = dr.Item("IncidentDetail").ToString 
    target .Created = Convert.ToDateTime(dr.Item("CreatedOn")) 
    ... 
    Return target 

End Function 
1

你已经一个函数试图做太多不止一两件事,独立的意图,都将变得清晰

Public Class Incident 

    Private sub CopyFrom(ByVal dr As DataRow) 
     MapDataRowToProperties(dr, me) 
    End Function 

    Private Shared Function CreateFrom(ByVal dr As DataRow) As Incident 
     Dim NewIncident As New Incident 
     MapDataRowToProperties(dr, me) 
     Return NewIncident 
    End Function 

    Private Shared sub MapDataRowToProperties(ByVal dr As DataRow, byval target As Incident) 
     target.ID = Convert.ToInt32(dr.Item("pkIncidentID")) 
     target.Description = dr.Item("IncidentDetail").ToString 
     target.Created = Convert.ToDateTime(dr.Item("CreatedOn")) 
    End Sub 
End Class 

现在使用变得

Dim aClass as new Incident 
    aClass.CopyFrom(dr) 

Dim aClass as MyClass = Incident.CreateFrom(dr)