Newbie Question: How do I tap into a call to an entity collection property

Posts   
 
    
LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 25-Jul-2007 21:06:21   

I'm using NET 2.0 with LLBLGEN v2 adapter model (for the framework extension, possible multiple db types in the near future) with partial classes.

Since I'm using the adapter model I need to do lazy-loading myself. There is no problem here and I've gotten the prefetch paths idea nailed down.

I would like to not have to modify any of the templates.

What I would like to know is how to tap into access to a property that returns an EntityCollection so that I may custom pre-load that entity collection.

Is the best practice way to do this is just override it?

Thanks, -Luke

Seth avatar
Seth
User
Posts: 204
Joined: 25-Mar-2006
# Posted on: 26-Jul-2007 04:01:48   

See the following help topics in the documentation:

Generated code - Adding your own code to the generated classes Generated code - Tapping into actions on entities and collections

You can do it using partial classes or by inserting your own code in the actual Entity class. I've used both: they work great.

-Seth

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 26-Jul-2007 10:47:48   

The above methods mentioned by Seth will work.

But IMHO, I prefer to use Manager Classes (Business Layer) to perform such actions in the Adapter model. The Adapter model was intended to be used to abstract the DBSpecific code from the Entity Classes (Business Objects), but you will do otherwise if you add code to the entityClasses that reference the DBSpecific to fetch other collections.

Another option is to derive from the DataAccessAdapter and override the needed fetch mehtods where you can detect the type fo entities being fetched and add prefetches to other related entities.

LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 30-Jul-2007 19:37:35   

Thanks!

LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 30-Jul-2007 19:48:34   

Ok. One more question about this:

Here is some of my code:

    ''' <summary>Gets the EntityCollection with the related entities of type 'PlanOptionsEntity' which are related to this entity via a relation of type '1:n'.
    ''' If the EntityCollection hasn't been fetched yet, the collection returned will be empty.</summary>
    <TypeContainedAttribute(GetType(PlanOptionEntity))> _
    Public Overrides ReadOnly Property [PlanOptions]() As EntityCollection(Of PlanOptionEntity)
        Get
            If Not _planOptionsLoaded Then
                LoadPlanInvestmentOptions()
            End If
            Return MyBase.PlanOptions
        End Get
    End Property

This is inside my Plan class which inherits from PlanEntity. So far so good. I also have PlanOption derived from PlanOptionEntity. How do I return an entity collection of my inherited class PlanOption instead of PlanOptionEntity?

I'm trying to not touch the code-gen classes at all.

Thanks, -Luke

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 31-Jul-2007 01:35:18   

I think I would go with updating the adapter "two classes" scenario templates for the derived classes directly, since this is more or less what you're looking for.

two classes scenario introduces a whole bunch of inherited "My<xxx>Entity", with corresponding related entities / factories /prefetch paths (and only with those, since all the LLBLGen logic is kept in the parent classes).

You should be safe with tweaking or better copying the derived class templates directly, and make your own scenario (Adapter - two classes - lazyloading)

Have a look in the manual and SDK for more info (it may look more complex than it actually is)

LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 31-Jul-2007 09:20:40   

I did look into the "two classes" scenario but... got confused with the "My<xxx>Entity" stuff.

I can go back to that. My question is this: Since I don't need to perform the same lazyloading all the time I'm fine with using the templates as is (maybe later when I study this more I will) so should I use the "My<xxx>Entity" classes directly and expose them or derive from them?

Thanks, -Luke

Jessynoo wrote:

I think I would go with updating the adapter "two classes" scenario templates for the derived classes directly, since this is more or less what you're looking for.

two classes scenario introduces a whole bunch of inherited "My<xxx>Entity", with corresponding related entities / factories /prefetch paths (and only with those, since all the LLBLGen logic is kept in the parent classes).

You should be safe with tweaking or better copying the derived class templates directly, and make your own scenario (Adapter - two classes - lazyloading)

Have a look in the manual and SDK for more info (it may look more complex than it actually is)

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 31-Jul-2007 09:37:09   

My opinion is that the MyXXXEntity don't bring to regular adapter scenario anymore than specifically what you're looking for in terms of inheritence:

they derive from the orginal entities and provide shadowed members so that the related entities, factories etc... don't get you back to the parent classes.

Of course you may think you don't want to get into the templating stuff nor update the generated code anywhere outside of the dedicated regions, but well, if you are to rewrite your own "myxxxentities" to add your lazy loading logic, IMHO you'd better generate with a two class scenario and make the additional code yours, or better simply add your lazyloading logic to the derived class templates.

In the latter case, you can regenerate safely, but even in the former case (update the generated derived classes), I reckon you should be able to regenerate a single class scenario, and keep the derived classes as your own (maybe I'm wrong on that point, but in any case, it will give you most of the code you are to write manually)

LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 31-Jul-2007 19:11:43   

Thanks. I've dived into template editing since what I want to do is to leave the existing XXXEntity classes available but place them into a namespace like nnn.core while keeping the derived entities in a namespace called nnn (the goal being to hide complexity while still making it available).

I also want to change the naming of the subentities from MyXXXEntity to just XXX (just the entity name).

-Luke

Jessynoo wrote:

My opinion is that the MyXXXEntity don't bring to regular adapter scenario anymore than specifically what you're looking for in terms of inheritence:

they derive from the orginal entities and provide shadowed members so that the related entities, factories etc... don't get you back to the parent classes.

Of course you may think you don't want to get into the templating stuff nor update the generated code anywhere outside of the dedicated regions, but well, if you are to rewrite your own "myxxxentities" to add your lazy loading logic, IMHO you'd better generate with a two class scenario and make the additional code yours, or better simply add your lazyloading logic to the derived class templates.

In the latter case, you can regenerate safely, but even in the former case (update the generated derived classes), I reckon you should be able to regenerate a single class scenario, and keep the derived classes as your own (maybe I'm wrong on that point, but in any case, it will give you most of the code you are to write manually)

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 01-Aug-2007 08:52:31   

I also want to change the naming of the subentities from MyXXXEntity to just XXX (just the entity name).

Please check the following threads: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=10044 http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=6880

LukeO
User
Posts: 58
Joined: 23-Jul-2007
# Posted on: 01-Aug-2007 22:28:55   

Thanks! I don't mind reading through all the existing forums it is just a little hard to find the answers if your asking the wrong questions.

Walaa wrote:

I also want to change the naming of the subentities from MyXXXEntity to just XXX (just the entity name).

Please check the following threads: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=10044 http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=6880