How to detect if a child collection is fetched?

Posts   
 
    
Barry
User
Posts: 232
Joined: 17-Aug-2005
# Posted on: 19-Apr-2006 17:38:32   

I'm using adapter, I want to know if a child collection fetched or not. For example, a Customer entity has a SalesOrders collection, which contains the sales orders belonged to the customer.

In some procedures, i only fetch customer entity without any preftech path, therefore, the order collection is empty.

However, in other procedures, it is required to access the data in SalesOrder collection, if it is not fetched and empty, it may cause wrong calculation in my procedure, so I want to fetch it before I call this procedure if it is not fetched.


void CalculateOutstandingOrderBalance(CustomerEntity customer)
{
    EntityCollection orders = customer.Orders
    
    /**********************
    //if orders is not fetched, fetch it first
    /**********************
    {
        DataAccessAdapter adapter = new DataAccessAdapter();
        adapter.FetchEntityCollection(orders, customer.GetRelationInfoOrders());
    }
    
    // Some calculation and business logic here
    // and update data to database
}

I want to know how to detect if the collection is fetched or not? and what will happen if collection is fetched and I fetch it again?

Thank you very much

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 19-Apr-2006 17:51:51   

You may use the EntityCollection.Count to detect an empty collection.

and what will happen if collection is fetched and I fetch it again?

Just make sure EntityCollection.DoNotPerformAddIfPresent is set to true. You may also clear the collection before re-fetching it.

Barry
User
Posts: 232
Joined: 17-Aug-2005
# Posted on: 19-Apr-2006 18:01:35   

if I use a uint of work, and delete one of the orders in prior steps, then clear the collection and fetch it. Will the deleted entity being fetched into collection again?

Or if I add some new entity in collection in prior steps and re-fetch it, will it merge the collection with data from database?

Entity class has a State property which I can distinguish the entity is new, fetched or other state, does EntityCollection have similar property?

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 20-Apr-2006 02:41:20   

if I use a uint of work, and delete one of the orders in prior steps, then clear the collection and fetch it. Will the deleted entity being fetched into collection again?

If the unit of work has not been committed then the data will still exist in the database and be refetched.

Or if I add some new entity in collection in prior steps and re-fetch it, will it merge the collection with data from database?

It will be merged and your new entites will still be there.

Entity class has a State property which I can distinguish the entity is new, fetched or other state, does EntityCollection have similar property?

EntityCollection has the DirtyEntites property which is a list of entities that are either updated or new. Is this the type of state you are looking for?

Barry
User
Posts: 232
Joined: 17-Aug-2005
# Posted on: 20-Apr-2006 03:10:12   

I'm looking for the state fetched or not, I want to know if a collection is already fetched in prior steps. If it is not fetched, I would fetch it first before further processing, a non-fetched collection may lead to incorrect calculation as a result.

I cannot use the count property to distinguish collection is fetched or not, because entities maybe deleted in prior steps, then the count would be zero. And if I fetched it again, deleted entities will be added into collcetion again, it will cause incorrect calculation too.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Apr-2006 07:41:02   

I'm looking for the state fetched or not, I want to know if a collection is already fetched in prior steps.

I think you'd better use a your own flag.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 20-Apr-2006 10:50:23   

A fetched collection of entities is a result of a query on the actual data store which contains the entities, i.o.w. a snapshot. If you have a routine which requires that you have fetched a given entity's related entities, if any, you should always fetch the data before the routine and pass that data to the routine. You can of course rely on data inmemory but that's stale and although fetched data is stale the second it gets out of the db, it's less stale than data in memory: what if another thread/user adds a salesorder for that customer? You don't pick it up in your routine if you don't fetch the data prior to the routine.

It also makes your code simpler: - get data - process data - persist changes - done.

No complicated scenario's when to skip a step, but: "these are the steps, take them all and it's done."

Frans Bouma | Lead developer LLBLGen Pro