Extending the framework.. next steps

Posts   
 
    
DrM
User
Posts: 49
Joined: 09-Feb-2004
# Posted on: 26-Feb-2004 08:26:03   

I have a custom entity called mySiteEntity which inherits the generated SiteEntity.

I have had no issues extending this to use different entity factories for dependent collections for example:

Public Class MySiteEntity

Public Sub New(ByVal siteID As System.Int32) MyBase.New(siteID) MyBase.SiteFeeds.EntityFactoryToUse = New MySiteFeedEntityFactory MyBase.CustomCategory.EntityFactoryToUse = New MyCustomCategoryEntityFactory End Sub

...some other constructors etc

End Class

But how do I extend MySiteEntity to use my a different entity factory for single enitities not collections.

So I have a mySiteHostEntity which inherits the SiteHostEntity and I want mySiteEntity's SiteHost member to use mySiteHostEntity.

I'm looking for the equivalent EntityFactoryToUse setting used for collections as in:

MyBase.SiteFeeds.EntityFactoryToUse = New MySiteFeedEntityFactory

but for entities:

MyBase.SiteHost.EntityFactoryToUse = mySiteHost <- and this I cannot do?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 26-Feb-2004 16:19:30   

You don't have to.

The single objects aren't created for you when a class is initialized, the collections are. This means that if I have an OrderEntity, and I create an instance of it, it doesn't contain a CustomerEntity instance, the referencing member variable is null/nothing, however it does contain an empty EntityCollection for OrderDetail object for example.

When you want to fetch a given entity and you want to fetch it in a derived class, simply create an instance of that class and fetch the data in that class using FetchNewEntity:

order.Customer = adapter.FetchNewEntity(New MyCustomerEntityFactory(), order.GetRelationInfoCustomer());

THis will use the factory passed in to create the right class, based on the fetched data.

Frans Bouma | Lead developer LLBLGen Pro
DrM
User
Posts: 49
Joined: 09-Feb-2004
# Posted on: 26-Feb-2004 17:52:23   

OK gotcha! I actually tried that earlier using this code:

SiteToGenerate.SiteHost = tempAdapter.FetchNewEntity(New MySiteHostEntityFactory, SiteToGenerate.GetRelationInfoSiteHost)

But it seems that once I had executed the first line:

SiteToGenerate.SiteHost = tempAdapter.FetchNewEntity(New MySiteHostEntityFactory, SiteToGenerate.GetRelationInfoSiteHost)

The SiteToGenerate.SiteHost entity did not have any of the new properties of the derived class MySiteHostEntity. I had to make a copy like this:

Dim siteHostCopy As MySiteHostEntity = SiteToGenerate.SiteHost Dim str As String = siteHostCopy.DBServer_DBOnly()

To get to the new properties (e.g. siteHostCopy.DBServer_DBOnly). Is this correct? Just want to make sure I'm on the right track here.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 26-Feb-2004 18:00:21   

tempAdapter.FetchNewEntity(New MySiteHostEntityFactory, SiteToGenerate.GetRelationInfoSiteHost)

should return a MySiteHostEntity.

Did you override the SiteHost property in the derived class? If not, it returns a SiteHostEntity, as is defined in the base class. You should 'hide' the base class' method by overriding it and by returning the type MySiteHostEntity, instead of SiteHostEntity.

Another possibility is to CType the SiteHost property's value to MySiteHostEntity.

Frans Bouma | Lead developer LLBLGen Pro
DrM
User
Posts: 49
Joined: 09-Feb-2004
# Posted on: 26-Feb-2004 18:15:51   

I prefer to override the property but how do I do this?

I presume I use some variation of this syntax in mySiteHostEntity (not the myHostSiteEntityFactory):

Public Overridable Property SiteHost As MySiteHostEntity Get Return _siteHost End Get

Set(ByVal Value As MySiteHostEntity) If MyBase.IsDeserializing Then _siteHost = Value Else CType(Value, IEntity2).SetRelatedEntity(Me, "Site") End If End Set

End Property

But it chokes on the _siteHost.

I apologize in advance for my lack of knowledge here...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 26-Feb-2004 20:11:46   

DrM wrote:

I prefer to override the property but how do I do this? I presume I use some variation of this syntax in mySiteHostEntity (not the myHostSiteEntityFactory):

Public Overridable Property SiteHost As MySiteHostEntity Get Return _siteHost End Get

Set(ByVal Value As MySiteHostEntity) If MyBase.IsDeserializing Then _siteHost = Value Else CType(Value, IEntity2).SetRelatedEntity(Me, "Site") End If End Set

End Property

But it chokes on the _siteHost. I apologize in advance for my lack of knowledge here...


Public Property SiteHost() As MySiteHostEntity
Get
    Return MyBase.SiteHost
End Get
Set(ByVal Value As MySiteHostEntity)
    MyBase.SiteHost = value
End Set
End Property

Wouldn't this do the trick?

Frans Bouma | Lead developer LLBLGen Pro
DrM
User
Posts: 49
Joined: 09-Feb-2004
# Posted on: 26-Feb-2004 20:40:05   

Frans,

Public Overridable Property SiteHost() As MySiteHostEntity
    Get
        Return MyBase.SiteHost
    End Get
    Set(ByVal Value As MySiteHostEntity)
        MyBase.SiteHost = Value
    End Set
End Property

I tried this initially but I get an error message saying that "they cannot overload each other because they differ only by return types". I assumed at this point that I was missing something. Do I need to add something to the argument signature?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 26-Feb-2004 21:46:09   

DrM wrote:

Frans,

Public Overridable Property SiteHost() As MySiteHostEntity
    Get
        Return MyBase.SiteHost
    End Get
    Set(ByVal Value As MySiteHostEntity)
        MyBase.SiteHost = Value
    End Set
End Property

I tried this initially but I get an error message saying that "they cannot overload each other because they differ only by return types". I assumed at this point that I was missing something. Do I need to add something to the argument signature?

Hmmm, I thought the compiler would simply 'hide' the base version.

I've to test this out to get a good answer to this. I'll get back to you in the morning.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 27-Feb-2004 10:32:19   

    Public Shadows Overridable Property SiteHost() As MySiteHostEntity
        Get
            Return CType(MyBase.SiteHost, MySiteHostEntity)
        End Get
        Set(ByVal Value As MySiteHostEntity)
            MyBase.SiteHost = Value
        End Set
    End Property

Shadows should be added. It then hides the base class 'SiteHost' property in favor of your new one. You internally use that base class property SiteHost to store the actual membervariable. You have to cast (CType) in the get of course, as MyBase.SiteHost returns the base class and strictly speaking yuo can't do an automatic upcast. (VB.NET will probably allow you to do so, C# will not).

Frans Bouma | Lead developer LLBLGen Pro