Home
Help
Register
Log in

Search

 
   Active Threads  

You are here: Home > LLBLGen Pro > LLBLGen Pro Runtime Framework> Getting LLBLGenType (Enum) from LLBLGen Type (RuntimeType)
 

Pages: 1
LLBLGen Pro Runtime Framework
Getting LLBLGenType (Enum) from LLBLGen Type (RuntimeType)
Page:1/1 

  Print all messages in this thread  
Poster Message
saggett
User



Location:
Manchester, UK
Joined on:
12-Nov-2007 15:44:46
Posted:
50 posts
# 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:

Code:
        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.
  Top
psandler
User



Location:
Chicago, IL
Joined on:
22-Feb-2005 22:24:13
Posted:
540 posts
# 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 Regular Smiley ).

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

Code:

        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 Regular Smiley ).


Phil


My C#/SQL Blog (some LLBL content)
Email: psandler70 (at) yahoo.com
 
Top
saggett
User



Location:
Manchester, UK
Joined on:
12-Nov-2007 15:44:46
Posted:
50 posts
# 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:

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


Is an improvement on my method, thanks. Regular Smiley

  Top
Pages: 1  


Powered by HnD ©2002-2007 Solutions Design
HnD uses LLBLGen Pro

Version: 2.1.12172008 Final.