Entity state when fetched using projections

Posts   
 
    
siegemos
User
Posts: 47
Joined: 25-Jun-2007
# Posted on: 29-Jul-2008 14:23:57   

Hi,

My apologies if this has been asked before, I can't find it if it has.

I am retrieving some products using a SPROC in an Sql 2005 db. I'm then projecting them on to a ProductCollection. This is all working fine but the problem I'm having is that the state of the entities in the product collection (ProductCollection[0].Fields.State) is set to EntityState.New which is causing issue further down the line. I understand that when using projection, only the field properties are propulated so Fields.State never get set to EntityState.Fetched but how can I change this? Here's the code I'm currently using:


ProductCollection products = new ProductCollection(new ExtendedProductFactory());
        using (IRetrievalQuery query = RetrievalProcedures.GetSearchProductFuzzyCallAsQuery(keyword, applicationId))
        {
            TypedListDAO dao = new TypedListDAO();
            using (IDataReader reader = dao.GetAsDataReader(null, query, CommandBehavior.CloseConnection))
            {
                List<IDataValueProjector> valueProjectors = new List<IDataValueProjector>();
                valueProjectors.Add(new DataValueProjector(ProductFieldIndex.Id.ToString(), 2, typeof(int)));
                valueProjectors.Add(new DataValueProjector(ProductFieldIndex.Code.ToString(), 3, typeof(string)));
                valueProjectors.Add(new DataValueProjector(ProductFieldIndex.Name.ToString(), 4, typeof(string)));
                .....

                DataProjectorToIEntityCollection projector = new DataProjectorToIEntityCollection(products);
                
            
                dao.GetAsProjection(valueProjectors, projector, reader);
                reader.Close();
            }
        }
        return products;

Thanks in advance for any help.

Chris.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 29-Jul-2008 15:33:59   

You should loop inside the products collection and set each entity's .Fields.State to EntityState.New

siegemos
User
Posts: 47
Joined: 25-Jun-2007
# Posted on: 29-Jul-2008 15:59:26   

I thought that might be the case but it does seem an inefficient way of doing it. Surely the collection is being iterated once already during the projection. Now I have iterate through it again to set the states that could have been set during the first run through. It does make sense that the entity states to be set to 'fetched' by 'DataProjectorToIEntityCollection' during the projection process, as they have been fetched from the db and ''DataProjectorToIEntityCollection'' knows the values are being projected onto an IEntityCollection.

Chris.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39866
Joined: 17-Aug-2003
# Posted on: 29-Jul-2008 17:49:05   

It's not necessarily so that the projector should set the states to fetched, as you can also use the projector to merge entity data for example.

What you could do is bind an event handler to products.EntityAdded and then set the state of the fields in the entity inside the CollectionChangedEventArgs.

You can also copy the code of the projector and use that as your own, to do your own post processing if you want to.

Frans Bouma | Lead developer LLBLGen Pro