Creating EntityField2

Posts   
 
    
Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 13-May-2009 17:38:18   

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.simple_smile

Thanks,

Fishy

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 14-May-2009 10:47:49   

I see you return the first n Fields.

What about the following pseudo code:

Public Shared Function CreateIncludeItems(ByVal firstNFields as Integer)
            Dim EntityList As New List(Of EntityField2)

            Dim i As Integer
            i = -1

            While firstNFields > i
                        i += 1                      
                        EntityList.Add(myEntity.Fields[i])                      
            End While
                        
            Return EntityList
End Function

P.S. I'm not good in VB.

Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 14-May-2009 16:36:35   

Thank you for your response Walaa.

Actually I'm returning the fields that the programmer is requesting, not the first n fields.

Both of the methods that I describe work. The first one gives intellesense of all the fields in the entity.

I was just concerened with all the ctypeing I'm doing especially on entities that have more then 100 fields.

So, if there is a better way of accomplishing the same outcome I would be intereseted in know what that was.

Thanks,

Fishy