New Posts New Posts RSS Feed: Include navigations in a loaded object
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Include navigations in a loaded object

 Post Reply Post Reply
Author
cefernan View Drop Down
Groupie
Groupie


Joined: 13-Jul-2012
Posts: 70
Post Options Post Options   Quote cefernan Quote  Post ReplyReply Direct Link To This Post Topic: Include navigations in a loaded object
    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?
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 11-May-2013 at 4:08am
I think I need some more background to answer this question. 
Back to Top
cefernan View Drop Down
Groupie
Groupie


Joined: 13-Jul-2012
Posts: 70
Post Options Post Options   Quote cefernan Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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.

 
Back to Top
cefernan View Drop Down
Groupie
Groupie


Joined: 13-Jul-2012
Posts: 70
Post Options Post Options   Quote cefernan Quote  Post ReplyReply Direct Link To This Post Posted: 16-May-2013 at 5:41am
Ok, I changed my business logic to receive IDs instead of entities.

Thank you Marcel.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down