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 = null) where 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?