Getting LLBLGenType (Enum) from LLBLGen Type (RuntimeType)

Posts   
 
    
saggett
User
Posts: 50
Joined: 12-Nov-2007
# Posted on: 01-Apr-2008 11:22:46   

What's the easiest way of getting the LLBLGenType (the enum) from the runtime type of the LLBLGen Entity (i.e. the Type object). So far I haven't found an easier way to do it than creating an instance of the type via reflection and casting it to IEntity2 (I'm using Adapter in C#).

My motivation for doing this is to write a generic fetch method like this:

        public TEntity FetchEntity<TEntity>(int entityId, IPrefetchPath2 prefetchPath, ExcludeIncludeFieldsList excludedIncludedFields) where TEntity : EntityBase2, IEntity2
        {
            TEntity ent = ReflectionHelper.CreateInstance<TEntity>();
            return (TEntity)FetchEntity((EntityType)ent.LLBLGenProEntityTypeValue, entityId, prefetchPath, excludedIncludedFields);
        }

        private IEntity2 FetchEntity(EntityType entityType, int entityId, IPrefetchPath2 prefetchPath, ExcludeIncludeFieldsList excludedIncludedFields)
        {
            IEntity2 entity = GeneralEntityFactory.Create(entityType);
            entity.PrimaryKeyFields[0].CurrentValue = entityId;
            var adapter = CreateAdapter();
            adapter.FetchEntity(entity, prefetchPath, null, excludedIncludedFields);
            return entity;
        }

I'm aware that the above code assumes that all entities have a single int primary key, but I'm happy with that.

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 01-Apr-2008 16:37:52   

Why do you want to pass in the type instead of simply passing in the EntityType enum?

The calling code either has to know one or the other, and I can't imagine any advantage to using the type instead of the enum (unless I'm missing something, which is always possible simple_smile ).

It seems like your second method would be all you need. In fact, it looks almost exactly like my single-pk-entity-fetch:


        internal static IEntity2 FetchEntityByType(object primaryKey, IPrefetchPath2 prePath, EntityType type)
        {
            IEntity2 entity = (IEntity2)GeneralEntityFactory.Create(type);
            using (MyCustomDataAccessAdapteradapter = new MyCustomDataAccessAdapter())
            {               
                ((IEntityField2)(entity.Fields.PrimaryKeyFields[0])).CurrentValue = primaryKey;
                adapter.FetchEntity(entity, prePath);
                if (entity.Fields.State != EntityState.Fetched)
                {
                    throw new MyCustomException("Entity with ID: " + primaryKey.ToString() + " not found.");
                }
                return entity;
            }
        }

As far as your original question--I don't know of any better way to get the EntityType from an entity type (if that makes sense simple_smile ).

Phil

saggett
User
Posts: 50
Joined: 12-Nov-2007
# Posted on: 01-Apr-2008 16:57:54   

No good reason, I just prefer the look of the code

ProductEntity product = FetchEntity<ProductEntity>(productId);

to

ProductEntity product = (ProductEntity)FetchEntity(EntityType.Product, productId);

mainly because it's shorter.

Your code:

                if (entity.Fields.State != EntityState.Fetched)
                {
                    throw new MyCustomException("Entity with ID: " + primaryKey.ToString() + " not found.");
                }

Is an improvement on my method, thanks. simple_smile