Generic Collection Method

Posts   
 
    
MikeP
User
Posts: 40
Joined: 27-May-2007
# Posted on: 17-Dec-2007 14:15:43   

Not sure whether this is possible or not. What I am trying to achieve is a generic method to retrieve EntityCollections. This should work with any entity and would be along the lines of (pseudo code):


public GenericCollection GetCollection(adapter, genericCollection)
{
    adapter.FetchEntityCollection(genericCollection);
    return genericCollection;
}

Is is possible to just pass any empty entity collection to this method and then cast the return generic collection back to the original EntityCollection? If so how would I define the generic collection in this method and how would I achieve the cast?

Thanks,

Mike

gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 17-Dec-2007 18:59:32   

Hi,

You might find something in:

http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=7666

(The .NET Activator)

Cheers, Gab

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Dec-2007 10:24:55   

I think this is just a wrap of the DataAccessAdapter FetchEntityCollection.

Maybe I'm missing something but I don't understand the need for this method, but anyway you can do it like this:

public void GetCollection(IDataAccessAdapter adapter, IEntityCollection2 genericCollection)
{
    adapter.FetchEntityCollection(genericCollection);
}

And you can call it the same way you call the adapter method:

EntityCollection<CustomerEntity> customers = new EntityCollection<CustomerEntity>();
DataAccessAdapter adapter = new DataAccessAdapter();
GetCollection(adapter, customers);
MikeP
User
Posts: 40
Joined: 27-May-2007
# Posted on: 18-Dec-2007 23:26:12   

DOH! Thanks Walaa, that was excatly what I tried but for some bizarre reason I had the strange idea that I had to return the collection instead of just calling the void method and the passed collection will be filled anyway (day one week one: passing by reference ...)

Anyway the reason I try to wrap the FetchEntityCollection is that I would like to put my error handling for this in one place (i.e. the try catch blocks) so that I don't have to do this in all the BL methods that need to fetch collections. I am not actually handling the exception there I am just throwing a new custom exception with more information which is the same for all methods.

Thanks again for all your help, it's much appreciated!