Abstract / Generic FetchEntityCollection

Posts   
 
    
Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 25-Apr-2004 05:51:09   

I find myself trying to write reusable code alot. So far, what I really like about the adapter pattern is that I can create an instance of an object, set its primary key value, and pass it to a facade method that uses abstract objects and fetch the data, such as the code below:

    Public Function FetchData(ByVal Entity As EntityBase2) As EntityBase2
        Dim myDB As DataAccessAdapter = db(True)
        If myDB.FetchEntity(Entity) Then
            Return Entity
        End If
    End Function

When creating an instance of an entityCollection, you create the instance using a factory and then fetch data into it. This code typically looks something like this:

        
Public Function Seasons(ByVal PortalID as integer) as HelperClasses.EntityCollection
Dim mSeasons As New HelperClasses.EntityCollection(New FactoryClasses.SeasonEntityFactory)
'... predicate code omitted
        myDB.FetchEntityCollection(mSeasons, pred, 0, sort)
        If mSeasons.Count > 0 Then
            TraceMessage("Located " & mSeasons.Count.ToString & " seasons")
            Return mSeasons
        End If
End Function

My question is, this: does an entity object know about what factory it is related to?

In the Seasons sample above, I create a new entityCollection using a factory class. If an entity was aware of it's factory, an entity could be passed to this method, and the method could use the entity's factory to create the collection. The goal of this exercise would be to have one method, that would fetch collections, and not care about the factory being used, because it could determine the factory from the data passed in.

Any thoughts on how I could make my code more extensible?

netclectic avatar
netclectic
User
Posts: 255
Joined: 28-Jan-2004
# Posted on: 26-Apr-2004 10:37:30   

I was looking for something similar to this, and couldn't find a way to get from the entitiy to the factory so i ended getting the assembly to create an entity factory for me given the name of the entity with 'Factory' tacked on the end, e.g. (in C#)


using System.Reflection;

...

public static EntityCollection FetchCollection(IEntity2 ent)
{
    EntityCollection ents = null;
    Assembly ass = Assembly.GetExecutingAssembly();
    IEntityFactory2 fac = (IEntityFactory2)ass.CreateInstance(string.Format("MyProject.FactoryClasses.{0}Factory", ent.LLBLGenProEntityName), true);
    if (fac != null) ents = new EntityCollection(fac);

    DataAccessAdapter adapter = new DataAccessAdapter();
    adapter.FetchEntityCollection(ents);

    return ents;
}

Hope this helps simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39623
Joined: 17-Aug-2003
# Posted on: 26-Apr-2004 12:34:44   

That's a way indeed. There is no way an entity has a reference to its factory. This is done for performance reasons, as otherwise the entity should hold a reference to a factory object, while it would never produce an entity itself.

Frans Bouma | Lead developer LLBLGen Pro
Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 26-Apr-2004 15:40:05   

Thanks a lot for the code snippet. That is a good use of reflection and definately provides a solution to my question.

Thanks again.