'Generic' routine to handle multiple Typed Collections

Posts   
 
    
Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 01-Jun-2010 20:06:16   

Hi all,

I'm trying to create a generic form that handles multiple entity types. The problem is I cannot seem to be able to handle the 'type collections' correctly. I know I'm missing something, but after 8 hours of coding, I'm way too deep in to be able to catch this on my own.

Simply put, I have a search form that I want to work with multiple collection types, and returns a typed collection (or anything else, as long as it works!) of the fetched entities. Then it opens the appropriate form and displays said entities.

Oh yes, I'm using 'self servicing'.

Thanks,

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 01-Jun-2010 20:27:56   

Erm - you haven't really asked us a question that we can answer for you... simple_smile

We need a specific problem/question to look at - otherwise how do we know where to start...?

Thanks

Matt

Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 01-Jun-2010 20:33:57   

Ok, fair enough. Let's put it this way:

I want to fill an entity collection (a typed entitycollection) with some entities. However, the type of entities to be filled can only be determined at run time.

I cannot create a new InvoiceCollection and fill it with InvoiceEntities... I need a more 'generic' way of doing things.

Thanks,

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 01-Jun-2010 20:51:02   

Something along the lines of (from memory so may need adapting for SelfServicing)


public <T> GetCollection<T>() where T:EntityCollectionBase 
{
    var col = new T();
    col.GetMulti();
    return col;
}

        

called like



CustomerCollection custCollection = (CustomerCollection)GetCollection<CustomerCollection>();


Is this along the right lines...?

Matt

Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 01-Jun-2010 22:46:56   

smile Sounds like a very good idea... only it didn't work... but you're on the right track though...

Any more ideas? cry

EntityCollectionBase is what's causing the problem, BTW.

Thanks a million!

Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 01-Jun-2010 22:52:48   

AND I'm using VB...

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 02-Jun-2010 01:10:10   

This won't be exactly what you are looking for, since it's adapter code AND C#. I didn't provide the FetchEntityCollection() code since you will have to convert to GetMulti anyway.

        public IEntityCollection2 FetchEntityCollectionByType(EntityType type, IRelationPredicateBucket filterBucket, IPrefetchPath2 prePath)
        {
            //get the system type of the entity
            Type entityType = GeneralEntityFactory.Create(type).GetType();
            //get the generic "container" type
            Type genericType = typeof(EntityCollection<>);
            //use the generic type and entity system type to create the full type
            Type boundType = genericType.MakeGenericType(entityType);
            //activate and use to fetch collection.
            var collectionToFill = (IEntityCollection2)Activator.CreateInstance(boundType);
            FetchEntityCollection(collectionToFill, filterBucket, prePath);
            return collectionToFill;
        }
Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 02-Jun-2010 09:04:19   

And this is another code that I've tried (sorry it's in C#, but I'm sure you can walk the extra mile to convert it to VB.NET)

private IEntityCollection GetCollection(string entityName)
{                   
    var entityType = (EntityType)Enum.Parse(typeof(EntityType), entityName, false);
    var myEntity = GeneralEntityFactory.Create(entityType);

    var entityfactory = myEntity.GetEntityFactory();
    var collection = entityfactory.CreateEntityCollection();

    collection.GetMulti(null);

    return collection;
}

Which can be called like follows:

var collection = GetCollection("OrdersEntity");

The above methods assumes you have the entity name as a string, if you have the entityType then you can comment the first line and directly pass the entityType as a parameter instead of the entityName string.

Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 02-Jun-2010 09:42:44   

Guys!! You da best!!! smile Walaa, you da man!!!

I love LLBL!!

Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 03-Jun-2010 10:00:12   

Hi,

Just for the sake of sharing, it is important to point out that there are two functions for creating Entity and EntityCollection from EntityType without having to have either created first; in other words you don't need Entity for creating EntityCollection or vice-versa. For example:

GeneralEntityFactory.Create(entityType)

is to create a single entity, and...

GeneralEntityCollectionFactory.Create(entityType)

is to create a collection, thus, Walaa's code can be further enhanced to be...

private IEntityCollection GetCollection(string entityName)
{                   
    var entityType = (EntityType)Enum.Parse(typeof(EntityType), entityName, false);
    //var myEntity = GeneralEntityFactory.Create(entityType);

    //var entityfactory = myEntity.GetEntityFactory();
    var collection = GeneralEntityCollectionFactory.Create(entityType);

    collection.GetMulti(null);

    return collection;
}

Lovely code, IMHO.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 03-Jun-2010 10:07:09   

Thanks for the feedback. simple_smile