Print Page | Close Window

Include navigations in a loaded object

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4144
Printed Date: 19-Apr-2024 at 2:37pm


Topic: Include navigations in a loaded object
Posted By: cefernan
Subject: Include navigations in a loaded object
Date Posted: 09-May-2013 at 7:35am
Hi,

I have a loaded object (entity) and now, in another context, I need to include some navigations of this entity.

How is the best way to do that?



Replies:
Posted By: mgood
Date Posted: 11-May-2013 at 4:08am
I think I need some more background to answer this question. 


Posted By: cefernan
Date Posted: 13-May-2013 at 5:53am
Let's go.

I have a class allocated in a different project that receive an object (entity) by parameter and do some business logic. In this code, I need this object and some navigations of this object. If the programmer forget to include the navigations before pass the object to this method, my code will fail. So, to ensure that my code will work, I receive the object and include the navigations in my method.

To do this I've created this extension method:
public static async Task<T> GetSingleEntityGraphAsync<T>(this EntityManager manager, T entity, Action<IFetchOptions<T>> fetchOptions, QueryStrategy strategy = nullwhere T : Entity
{
    IEntityQuery<T> query = entity.ToQuery<T>();
 
    if (fetchOptions != null)
    {
        var options = new FetchOptions<T>(query);
        fetchOptions(options);
        query = options.Query;
    }
 
    if (strategy != null)
        query.With(strategy);
 
    var result = await manager.ExecuteQueryAsync<T>(query);
    return result.Single();
}

In my business logic:
...    
    // Load necessary data of campaign
    Campaign = await EntityManager.GetSingleEntityGraphAsync<Campaign>(Campaign, f => f
        .Include(i => i.CampValues).Include(i => i.CampPrizes));
...    

But, to create this extension method, I needed to declare my own FetchOptions<T>.

Is it good strategy or do you suggest something better?


Posted By: mgood
Date Posted: 14-May-2013 at 6:22pm
ViewModels and business logic are best written such that they ensure all the required data is loaded instead of trusting an entity that is passed from the outside. You can help this by passing IDs instead of entities. 

There are several ways you can accomplish this. The easiest is taking advantage of the query cache in DevForce. If a ViewModel or business logic requests some data and the data has been previously loaded using the same query, the data automatically comes from the cache and it won't round-trip to the server.

 


Posted By: cefernan
Date Posted: 16-May-2013 at 5:41am
Ok, I changed my business logic to receive IDs instead of entities.

Thank you Marcel.



Print Page | Close Window