Fetch entity with entitytype & PK

Posts   
 
    
MatthewM
User
Posts: 78
Joined: 26-Jul-2006
# Posted on: 19-Dec-2006 17:30:44   

I have an EntityType and a record PK ID. I need to fetch that entity.

I can create it with GeneralEntityFactory.Create(EntityType) of course. However, IEntity has no way to fetch.

I know I could use reflection CreateInstance, but it seems like there should be a better way..something native in IEntity.

MatthewM
User
Posts: 78
Joined: 26-Jul-2006
# Posted on: 19-Dec-2006 18:36:04   

I must be getting good at thinking outside the box wink Ok I modified entityFactories.template -

First I made the assertion that I only want to do this generic fetch for entities that have a PK of type int (which means I later have to exclude the ones that have a guid pk)

Next I added a middle interface:

    public interface IEntityFactoryB : IEntityFactory
    {
        IEntity Create(int PKId);
    }

Then I changed the entity factory class def to inherit from the new new middle interface:

public <[If UsePartialClasses]>partial <[EndIf]>class <[CurrentEntityName]>EntityFactory : IEntityFactoryB

Then in the entity factory class I added the new interface method def, notice I took a brute force exclusion of 5 entities that have non int PKs:

/// <summary>Creates a new, fetched <[CurrentEntityName]>Entity object.</summary>
        /// <returns>A new, fetched <[CurrentEntityName]>Entity object.</returns>
        public virtual IEntity Create(int PKId)
        {
            IEntity toReturn = null;
            <[If Not StringValueEquals CurrentEntityName "EmployeeAux,TblEmailAddress,ContentData,SQLLog,SQLLOGKEYS"]>
            toReturn = new <[CurrentEntityName]>Entity(PKId);<[EndIf]>
            return toReturn;
        }

Finally I added a new static method to the GeneralEntityFactory class at the bottom:

        /// <summary>Creates a new, empty Entity object of the type specified</summary>
        /// <param name="entityTypeToCreate">The entity type to create.</param>
        /// <returns>A new, empty Entity object.</returns>
        public static IEntity Create(EntityType entityTypeToCreate, int PKId)
        {
            IEntityFactoryB factoryToUse = null;
            switch(entityTypeToCreate)
            {
<[Foreach Entity CrLf]>             case <[RootNamespace]>.EntityType.<[CurrentEntityName]>Entity:
                    factoryToUse = new <[CurrentEntityName]>EntityFactory();
                    break;<[NextForeach]>
            }
            return factoryToUse.Create(PKId);
        }       

VOILA! I can now create and fetch entities by EntityType and a PKID value.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39914
Joined: 17-Aug-2003
# Posted on: 20-Dec-2006 11:34:34   

Cool code snippet! smile

One other way would have been by using a projection, fetch a dyn. list using the fields of the entity instance and a filter on the PK field, and the factory to produce the entity instance. But this is also a nice way of doing it simple_smile

Frans Bouma | Lead developer LLBLGen Pro