Search code examples
c#teleriktelerik-open-access

Telerik Open Access FetchStrategy does not seem to be applied to my query


I am having some (more) issues with my Telerik OpenAccess ORM. This time in the area of applying Fetch Strategies to a query. Here's the code ...

using (DAL.DarkRoomDB ctx = new DarkRoomDB())
            {
                //
                // Set the resulting object to include the contents of the package 
                FetchStrategy strategy = new FetchStrategy();
                strategy.LoadWith<DeliverablePackageEntity>(c => c.PackageContents);
                strategy.LoadWith<DeliverablePackageContentEntity>(c => c.Deliverable);
                strategy.LoadWith<DeliverablePackageContentEntity>(c => c.DeliverablePackage);
                strategy.MaxFetchDepth = 3;
                ctx.FetchStrategy = strategy;
                //
                // get the package that matches the SKU
                DataStoreRepository<DeliverablePackageEntity> repo = DataStoreRepository<DeliverablePackageEntity>.GetInstance(ctx);
                DeliverablePackageEntity entity = repo.GetEntityList(c => c.PackageSku == SKU).FirstOrDefault();
                //
                // Create a DISCONNECTED COPY of the entity
                copy = ctx.CreateDetachedCopy<DeliverablePackageEntity>(entity, strategy); 

            }

            ret = EntityToDomainMapper.Map<DeliverablePackageEntity, DeliverablePackage>(copy);              
            return ret;

When I run this I expect the PackageContents of the DeliverablePackageEntity to be pre-loaded. When I look at the entity variable in the debugger however it tells me that the "contents of the property will be enumerated when expanded" which would suggest to me that the property has not yet been pre-populated, which is what I thought the purpose of the FetchStrategy was.

Am I missing something?


Solution

  • This behavior is normal because the navigation properties of the entity object are of type IEnumerable. Therefore even if they are preloaded in memory, you need to enumerate through them in order to access them.

    You can verify that the navigation properties specified in the FetchStrategy have been preloaded by checking the whether there is SQL script generated when accessing them.

    Consider the following example where the related RentalOrders for the Car object are preloaded. Upon executing the ToList() method they will be enumerated but the executedScript will remain empty because they have been preloaded by the FetchStrategy:

    using (EntitiesModel1 context = new EntitiesModel1())
    {
    
        FetchStrategy loadWithRentalOrders = new FetchStrategy();
        loadWithRentalOrders.LoadWith<Car>(car => car.RentalOrders);
        context.FetchStrategy = loadWithRentalOrders;
        Car firstCar = context.Cars.First();
    
        context.Log = new StringWriter();
        List<RentalOrder> relatedOrders = firstCar.RentalOrders.ToList();
        //should be empty
        string executedScript = context.Log.ToString();
    }
    

    I hope this helps.