Extending the framework

Posts   
 
    
DrM
User
Posts: 49
Joined: 09-Feb-2004
# Posted on: 11-Feb-2004 17:33:12   

I've gone with the Adapter model after your advice the last few days. Now I need to extend one of the classes.

I have a table called Site in the database which results in LLBLGen producing a SiteEntity and a SiteEntityFactory.

I need this class to have a number of additional properties. So I read the section on "Extending the framework" in the docs and need some guidance here.

Do I need to derive two new classes or one to do this.

Do I need to derive a new MySiteEntity as well as a MySiteEntityFactory?

If so do I also need to override all the constructors? Am I missing something here?

Cheers Derek

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 11-Feb-2004 18:33:06   

DrM wrote:

I've gone with the Adapter model after your advice the last few days. Now I need to extend one of the classes. I have a table called Site in the database which results in LLBLGen producing a SiteEntity and a SiteEntityFactory.

I need this class to have a number of additional properties. So I read the section on "Extending the framework" in the docs and need some guidance here.

Do I need to derive two new classes or one to do this. Do I need to derive a new MySiteEntity as well as a MySiteEntityFactory?

You need to derive from the factory class to produce MySiteEntity objects. You then also derive a new class from SiteEntity and call that MySiteEntity and you add some constructors to that class as shown in the documentation.

If so do I also need to override all the constructors? Am I missing something here?

You should at least implement the constructor which accepts an IEntityFields2 object and the one which accepts the PK fields. (simply copy them over from the class you're inheriting from, empty the body and add :base(_parameters_) to the header (and change the names of course) simple_smile

Because you created a new type, entities which relate to SiteEntity have to know that the reference they hold should be a MySiteEntity reference, that is, if you want to have that. That's the reason why I also derived from Customer in the documentation, so I could specify that 'Orders' contains MyOrderEntity objects and that when I use MyCustomerEntity objects somewhere (in a collection, in a grid) the Orders collection will contain/should contain MyOrderEntity objects. This then will result in a bound grid that the extra properties added in MyOrderEntity are added to the grid, because when I navigate from MyCustomerEntity to its Orders collection, the grid thinks it sees MyOrderEntity objects, due to the fact I've specified that in MyCustomerEntity, as shown in the documentation.

It might be a little cumbersome at first, but it will turn out to be more flexible in the end.

Frans Bouma | Lead developer LLBLGen Pro
DrM
User
Posts: 49
Joined: 09-Feb-2004
# Posted on: 11-Feb-2004 19:06:20   

OK so in the original SIteEntity class I have this:

    Public Sub New()
        MyBase.New("SiteEntity")
        InitClassEmpty(New SiteValidator(), EntityFieldsFactory.CreateEntityFieldsObject(EntityType.SiteEntity))
    End Sub

What do I do with the InitClassEmpty in my new entity? It seems that it is used for validation. I'm happy to put the time into understanding this but I find myself wishing for more documentation or samples.

DrM
User
Posts: 49
Joined: 09-Feb-2004
# Posted on: 11-Feb-2004 19:14:52   

Here is what I have come up so far - I'm just looking for some affirmation that I'm on the right track. Obviously getting the underlying architecture right is essential to retaining the flexibility of the generated code.

Imports ORM.EntityClasses Imports ORM.HelperClasses Imports ORM.FactoryClasses Imports ORM.RelationClasses

Imports SD.LLBLGen.Pro.ORMSupportClasses

Public Class MySiteEntity Inherits SiteEntity

Public Sub New()
End Sub

Public Sub New(ByVal fields As IEntityFields)
    MyBase.New("SiteEntity")
    'not sure what to do in the next line
    'InitClassEmpty(New SiteValidator, fields)
End Sub

Public Sub New(ByVal siteID As System.Int32)
    MyBase.New("SiteEntity")
    'not sure what to do in the next line
    'InitClassEmpty(New SiteValidator, EntityFieldsFactory.CreateEntityFieldsObject(EntityType.SiteEntity))
    Me.SiteID = siteID
End Sub

Public ReadOnly Property ExampleProperty() As String
    Get
        Return MyBase.SiteName
    End Get
End Property

End Class

Public Class MySiteEntityFactory Inherits SiteEntityFactory

Public Sub New()
End Sub

Public Overloads Overrides Function Create() As IEntity2
    Return New MySiteEntity
End Function

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

End Class

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

DrM wrote:

OK so in the original SIteEntity class I have this:

        Public Sub New()
            MyBase.New("SiteEntity")
            InitClassEmpty(New SiteValidator(), EntityFieldsFactory.CreateEntityFieldsObject(EntityType.SiteEntity))
        End Sub

What do I do with the InitClassEmpty in my new entity? It seems that it is used for validation. I'm happy to put the time into understanding this but I find myself wishing for more documentation or samples.

I see that I was very confusing with my explanation simple_smile I'll retry simple_smile

What I meant was just copy the constructor headers and empty the bodies and add calls to the MyBase.New() method. So your constructors simply look like:


Public Sub New()
End Sub

Public Sub New(theID as Integer)
MyBase.New(theID)
End Sub

etc. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 11-Feb-2004 20:54:01   

I've altered your code a bit so it's ok. simple_smile


Imports ORM.EntityClasses
Imports ORM.HelperClasses
Imports ORM.FactoryClasses
Imports ORM.RelationClasses

Imports SD.LLBLGen.Pro.ORMSupportClasses

Public Class MySiteEntity
    Inherits SiteEntity

    Public Sub New()
    End Sub

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

    Public Sub New(ByVal siteID As System.Int32)
        MyBase.New(siteID)
    End Sub

    Public ReadOnly Property ExampleProperty() As String
        Get
            Return MyBase.SiteName
        End Get
    End Property

End Class


Public Class MySiteEntityFactory
    Inherits SiteEntityFactory

    Public Sub New()
    End Sub

    Public Overloads Overrides Function Create() As IEntity2
        Return New MySiteEntity()
    End Function

    Public Overloads Overrides Function Create(ByVal fields As IEntityFields2) As IEntity2
        Return New MySiteEntity(fields)
    End Function
End Class

As you can see, the constructors simply pass the parameters on to the base class, nothing further is done, the rest is all in the base class, so inheriting from a generated class is pretty simple.

Frans Bouma | Lead developer LLBLGen Pro
DrM
User
Posts: 49
Joined: 09-Feb-2004
# Posted on: 11-Feb-2004 21:59:06   

Much appreciated!!

DrM
User
Posts: 49
Joined: 09-Feb-2004
# Posted on: 12-Feb-2004 03:24:39   

Small problem using the code above:

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

The MyBase.New(fields) gives me an error saying that the Overload resolution failed because the New method cannot be called without a narrowing conversion?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 12-Feb-2004 09:05:42   

DrM wrote:

Small problem using the code above:

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

The MyBase.New(fields) gives me an error saying that the Overload resolution failed because the New method cannot be called without a narrowing conversion?

Whoops, I didn't saw your typo and included it too, it of course should have been:

Public Sub New(ByVal fields As IEntityFields**2**)
    MyBase.New(fields)
End Sub
Frans Bouma | Lead developer LLBLGen Pro
DrM
User
Posts: 49
Joined: 09-Feb-2004
# Posted on: 12-Feb-2004 16:04:23   

Brilliant! Sorry my typing leaves a lot to be desired. I just made the change and it works like a charm. I'm starting to love this product.