I'm in the process of creating manager classes and I wrote a couple of functions that return a List(Of EntityField2) to be used in the Fetch functions. Here is the template code:
Public Shared Function CreateIncludeItems( _
<%For cnt As Integer = 1 To currentEntity.Fields.Count - 1%> Optional ByVal includeItem<%=cnt%> As <%=currentEntityName%>FieldIndex = CType(-1, <%=currentEntityName%>FieldIndex), <%Next%>Optional ByVal includeItemLast As <%=currentEntityName%>FieldIndex = CType(-1, <%=currentEntityName%>FieldIndex)) As List(Of EntityField2)
Dim EntityList As New List(Of EntityField2)
<%For cnt As Integer = 1 To currentEntity.Fields.Count - 1%>
If includeItem<%=cnt%> = -1 Then Return EntityList
EntityList.Add(CType(FactoryClasses.EntityFieldFactory.Create(includeItem<%=cnt%>), EntityField2))<%Next%>
If includeItemLast = -1 Then Return EntityList
EntityList.Add(CType(FactoryClasses.EntityFieldFactory.Create(includeItemLast), EntityField2))
Return EntityList
End Function
Public Shared Function CreateIncludeItems(ByVal ParamArray includeItems() As EntityField2) As List(Of EntityField2)
Return includeItems.ToList
End Function
Here is a sample output:
Public Shared Function CreateIncludeItems( _
Optional ByVal includeItem1 As MealDetailFieldIndex = CType(-1, MealDetailFieldIndex), Optional ByVal includeItem2 As MealDetailFieldIndex = CType(-1, MealDetailFieldIndex), Optional ByVal includeItem3 As MealDetailFieldIndex = CType(-1, MealDetailFieldIndex), Optional ByVal includeItem4 As MealDetailFieldIndex = CType(-1, MealDetailFieldIndex), Optional ByVal includeItem5 As MealDetailFieldIndex = CType(-1, MealDetailFieldIndex), Optional ByVal includeItem6 As MealDetailFieldIndex = CType(-1, MealDetailFieldIndex), Optional ByVal includeItemLast As MealDetailFieldIndex = CType(-1, MealDetailFieldIndex)) As List(Of EntityField2)
Dim EntityList As New List(Of EntityField2)
If includeItem1 = -1 Then Return EntityList
EntityList.Add(CType(FactoryClasses.EntityFieldFactory.Create(includeItem1), EntityField2))
If includeItem2 = -1 Then Return EntityList
EntityList.Add(CType(FactoryClasses.EntityFieldFactory.Create(includeItem2), EntityField2))
If includeItem3 = -1 Then Return EntityList
EntityList.Add(CType(FactoryClasses.EntityFieldFactory.Create(includeItem3), EntityField2))
If includeItem4 = -1 Then Return EntityList
EntityList.Add(CType(FactoryClasses.EntityFieldFactory.Create(includeItem4), EntityField2))
If includeItem5 = -1 Then Return EntityList
EntityList.Add(CType(FactoryClasses.EntityFieldFactory.Create(includeItem5), EntityField2))
If includeItem6 = -1 Then Return EntityList
EntityList.Add(CType(FactoryClasses.EntityFieldFactory.Create(includeItem6), EntityField2))
If includeItemLast = -1 Then Return EntityList
EntityList.Add(CType(FactoryClasses.EntityFieldFactory.Create(includeItemLast), EntityField2))
Return EntityList
End Function
Public Shared Function CreateIncludeItems(ByVal ParamArray includeItems() As EntityField2) As List(Of EntityField2)
Return includeItems.ToList
End Function
As you can see the CreateIncludeItems is overloaded so you can create them for the fieldIndex for the specific entity or you can provide the EntityField2 items and a list of them will be returned. The first method works really well because intellesense shows only the fields for the specific entity.
So, my question is: Is this the best way of doing this? Is it ineffecient doing all those ctypes?
Any improvements or suggestions are welcomed.
Thanks,
Fishy