Passing collections as paramters when using Generics

Posts   
 
    
JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 25-Jul-2006 22:22:44   

How can I pass a strongly typed EntityCollection to a generic function?, e.g. to centralize use of Adapter specific code.

the following code generates this error at compile time.

Error 1 The best overloaded method match for 'MyAppDataModule.DataService.FetchCollection(ref MyAppDB.HelperClasses.EntityCollection<SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2>, SD.LLBLGen.Pro.ORMSupportClasses.IRelationPredicateBucket)' has some invalid arguments C:\Code\MyApp2\MyDataModule\DataServices.cs 27 4 MyAppDataModule

Consider the following code:


public EntityCollection<AffiliationEntity> GetAffiliates()
{
    EntityCollection<AffiliationEntity> ec = new EntityCollection<AffiliationEntity>();
    this.FetchCollection(ec, null);
    return ec;
}

private void FetchCollection(ref EntityCollection<EntityBase2> ec, IRelationPredicateBucket bucket)
{
    using (IDataAccessAdapter adapter = this.GetAdapter())
    {
        adapter.FetchEntityCollection(ec, bucket);
    }
}

public IDataAccessAdapter GetAdapter()
{
    IDataAccessAdapter adapter = new MyAppDB.DatabaseSpecific.DataAccessAdapter();
    return adapter;
}


Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 26-Jul-2006 08:08:42   

Would you try the following?

private void FetchCollection(ref EntityCollection ec, IRelationPredicateBucket bucket)
{
    using (IDataAccessAdapter adapter = this.GetAdapter())
    {
        adapter.FetchEntityCollection(ec, bucket);
    }
}

Note: <EntityBase2> was removed from the FetchCollection() parameter definition.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 26-Jul-2006 09:48:05   

You should use IEntityCollection2 as the parameter type for ec in the FetchCollection method, OR: private void FetchCollection<T>(EntityCollection<T> ec, IRelationPredicateBucket bucket)

Don't use 'ref' as it's not needed, because you already pass an object. ref suggests you then might set ec to a different entity collection, which is not the case.

Frans Bouma | Lead developer LLBLGen Pro
JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 26-Jul-2006 12:45:12   

Thank you Waala and Otis for your replies.

I used IEntityCollection2 as Otis suggested, and removed the ref flushed