Custom property in inherited entity missing from databinding

Posts   
 
    
Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 12-Dec-2008 11:13:40   

If I add a property in class inherited from an entity (in my main project), this property wont show up in a bound datagrid. But If I add the same property in a partial class of the entity in the generic DAL project, the property will show up. Where is the difference? What I'm missing?

Sample code:

I have a PersonaEntity entity. In my DAL project I add the following property in a partial class file:

Namespace ISI.AlboTop.DAL.DBDati.EntityClasses

    Partial Public Class PersonaEntity

        Private maaaa As String
        Public Property AAAAAAAAAAAAAAAAAAAAA() As String
            Get
                Return maaaa
            End Get
            Set(ByVal value As String)
                maaaa = value
            End Set
        End Property

    End Class

End Namespace

then, in my main project, I create a new class that inherit from PersonaEntity to add another property:

Public Class InherithedPersonaEntity
    Inherits PersonaEntity

    Private mBBBB As String
    Public Property BBBBBBBBBBBBBBBBBBBBB() As String
        Get
            Return mBBBB
        End Get
        Set(ByVal value As String)
            mBBBB = value
        End Set
    End Property

End Class

Now, I create an instance of my InherithedPersonaEntity entity, put it in an entitycollection, and bind it to a grid

Dim entColl As New EntityCollection(Of InherithedPersonaEntity)

Dim inheritedPerson As New InherithedPersonaEntity

inheritedPerson.AAAAAAAAAAAAAAAAAAAAA = "TEST PROPERTY AAAAAAAAAAAAAAAAAAAAAAAA"
inheritedPerson.BBBBBBBBBBBBBBBBBBBBB = "TEST PROPERTY BBBBBBBBBBBBBBBBBBBBBBBB"

entColl.Add(inheritedPerson)

Dim frmEntColl As New frmDataGrid(entColl)
frmEntColl.Show()

in the datagrid I see the "AAAAAAAAAAAAAAAAAAAAA" column, but I don't see the "BBBBBBBBBBBBBBBBBBBBB" column.

Why can't I see the "BBBBBBBBBBBBBBBBBBBBB" column? Where is the difference between these 2 properties?

If I use a List(Of InherithedPersonaEntity) like a datasource (instead that the EntityCollection(Of InherithedPersonaEntity)) the datagrid will show the "BBBBBBBBBBBBBBBBBBBBB" column. But I can't fetch a List(Of InherithedPersonaEntity).

I'm definitely missing something.... confused

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 12-Dec-2008 12:26:20   

I think you should derive from PersonaEntityFactory, and use it in inestantiating the entityCollection. Of-course the factory should create instances from your derived entity.

Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 12-Dec-2008 14:49:52   

Walaa wrote:

I think you should derive from PersonaEntityFactory, and use it in inestantiating the entityCollection. Of-course the factory should create instances from your derived entity.

Yeah, thanks simple_smile You pointed me in the right direction.

here is the working code, the InherithedPersonaEntity class:

Public Class InherithedPersonaEntity
    Inherits PersonaEntity

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal fields As IEntityFields2)
        MyBase.New(fields)
    End Sub

    Public Sub New(ByVal validator As IValidator)
        MyBase.New(validator)
    End Sub

    Public Sub New(ByVal idpersona As Integer)
        MyBase.New(idpersona)
    End Sub

    Public Sub New(ByVal idpersona As Integer, ByVal validator As IValidator)
        MyBase.New(idpersona, validator)
    End Sub

    <EditorBrowsable(EditorBrowsableState.Never)> _
    Protected Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext)
        MyBase.New(info, context)
    End Sub



    Private mBBBB As String
    Public Property BBBBBBBBBBBBBBBBBBBBB() As String
        Get
            Return mBBBB
        End Get
        Set(ByVal value As String)
            mBBBB = value
        End Set
    End Property


    Protected Overrides Function CreateEntityFactory() As SD.LLBLGen.Pro.ORMSupportClasses.IEntityFactory2
        Return New InherithedPersonaEntityFactory
    End Function

End Class

and the InherithedPersonaEntityFactory class:

Public Class InherithedPersonaEntityFactory
    Inherits DAL.DBDati.FactoryClasses.PersonaEntityFactory

    Public Overrides Function Create() As SD.LLBLGen.Pro.ORMSupportClasses.IEntity2
        Return New InherithedPersonaEntity
    End Function

    Public Overrides Function Create(ByVal fields As IEntityFields2) As IEntity2
        Return New InherithedPersonaEntity(fields)
    End Function

    Public Overrides Function CreateEntityCollection() As IEntityCollection2
        Return New EntityCollection(Of InherithedPersonaEntity)(Me)
    End Function

End Class

Without creating a specific EntityFactory the BBBBBBBBBBBBBB property wasn't showing in the datagrid for the following reasons:

  • The dataBinding-engine ask the datasource for the columns

  • the datasource was an EntityCollection(Of InherithedPersonaEntity), but the grid is bound the the DefaultEntityView of that EntityCollection that is an EntityView(Of InherithedPersonaEntity)

  • so the EntityView.GetItemProperties function is called.

  • Then the EntityView.GetItemProperties function instantiate a dummy entity (EntityView.CreateDummyInstance) for grabbing the properties/columns.

  • Now, EntityView.CreateDummyInstance revert to the EntityCollection.EntityFactoryToUse, which, by default, is filled with the entityFactory instance returned by MyEntity.CreateEntityFactory. So, to make it work, we need to override the InherithedPersonaEntity.CreateEntityFactory method, and make it return an instance of InherithedPersonaEntityFactory which create instance of the class InherithedPersonaEntityFactory instead that instance of the class PersonaEntityFactory.

Now it works! sunglasses