Sample - EntityCollection from Retrieval Query

Posts   
 
    
Posts: 2
Joined: 16-Feb-2006
# Posted on: 09-Oct-2006 10:20:26   

Hi guys,

I recently needed the ability to convert a retrieval procedure into an entity collection and after spending some time with the new FetchProjection methods, managed to come up with the following code.

The retrieval query should return all of the fields the entity uses.


public class MyDataAdapter : DataAccessAdapter
{
    public void FetchEntityCollectionUsingRetrievalQuery( IEntityCollection2 collectionToFill, IPrefetchPath2 prefetchPath, IRetrievalQuery queryToExecute )
    {
        IEntityFactory2 entityFactory = collectionToFill.EntityFactoryToUse;
        if ( entityFactory == null )
        {
            throw new ArgumentException( "No entity factory specified in the passed in IEntityCollection2 object. Cannot continue", "collectionToFill" );
        }
        
        IEntityFields2 entityFields = entityFactory.CreateFields();
        List<IDataValueProjector> valueProjectors = new List<IDataValueProjector>( entityFields.Count );
        
        foreach ( EntityField2 entityField in entityFields )
        {
            valueProjectors.Add( new DataValueProjector( entityField.Alias, entityField.FieldIndex, entityField.DataType ) );
        }

        FetchProjection( valueProjectors, new DataProjectorToIEntityCollection2( collectionToFill ), queryToExecute );
        
        FetchPrefetchPath( collectionToFill, null, 0, null, prefetchPath );
    }
}

This method will need to appear in the DataAccessAdapter class because it uses the protected FetchPrefetchPath method to support prefetching of additional entities.

HTH, Shannon

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 09-Oct-2006 16:45:33   

I'm not sure if that method should be incorporated, since it sounds more of a factory abstracting some of the projector's structure. I think Frans's moving away from such factories to keep up with more generic code (how many such projected overloads should we then provide?).

Now maybe the question is should FetchPrefetchPath get public? Since it gets care of restoring the state of its parameters, I don't see any reason for preventing it, yet there might be plumbing reasons. Frans can tell.