Print Page | Close Window

Using the new methods with fetchOptions

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=3860
Printed Date: 28-Mar-2024 at 4:25am


Topic: Using the new methods with fetchOptions
Posted By: cefernan
Subject: Using the new methods with fetchOptions
Date Posted: 20-Dec-2012 at 4:05am
Hi,

First of all, thanks for update Cocktail and release new interesting features.

I've updated to Cocktail 2.2.0 and I've seen the new way to configure eager fetching using fetchOptions.

I have this call:
var entityData = await _unitOfWork.Entities.FindAsync(SetPredicate(), T => T.OrderBySelector(sortSelector), IncludeProperties);

I'd like to change and use fetchOptions. My property "IncludeProperties" is a string separated by comma. 

How to do that?




Replies:
Posted By: mgood
Date Posted: 20-Dec-2012 at 9:11am
If you want to eager fetch multiple related entities, you simply chain together several Includes as in options => options.Include("A").Include("B").Include(x => x.C) and so forth.
 
If you have an exisiting comma separated list, you could write a little extension method like so:
 
        public static void Include<T>(this IFetchOptions<T> fetchOptions, string includeProperties)
        {
            if (string.IsNullOrWhiteSpace(includeProperties)) return;
            ParseIncludeProperties(includeProperties)
                .ForEach(x => fetchOptions.Include(x));
        }
        private static IEnumerable<string> ParseIncludeProperties(string includeProperties)
        {
            return includeProperties.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        }
 
var entityData = await _unitOfWork.Entities.FindAsync(SetPredicate(), T => T.OrderBySelector(sortSelector),
options => options.Include(includeProperties));


Posted By: mgood
Date Posted: 20-Dec-2012 at 9:38am
Sorry, haven't had my coffee yet. Just realized that's an ambiguous signature. We have to name the extension method something else like IncludeMany.
 
public static void IncludeMany<T>(this IFetchOptions<T> fetchOptions, string includeProperties)
{
if (string.IsNullOrWhiteSpace(includeProperties)) return;
ParseIncludeProperties(includeProperties)
.ForEach(x => fetchOptions.Include(x));
}
private static IEnumerable<string> ParseIncludeProperties(string includeProperties)
{
return includeProperties.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
var entityData = await _unitOfWork.Entities.FindAsync(SetPredicate(), T => T.OrderBySelector(sortSelector),
options => options.IncludeMany(includeProperties));
 
Or better yet, ditch the comma separated list and go with an IEnumerable. You probably already have an IEnumerable from which you currently build the comma separated list.
 
        public static void Include<T>(this IFetchOptions<T> fetchOptions, IEnumerable<string> properties)
        {
            properties.ForEach(x => fetchOptions.Include(x));
        }
 
var properties = new [] { "A", "B", "C" };
var entityData = await _unitOfWork.Entities.FindAsync(SetPredicate(), T => T.OrderBySelector(sortSelector),
options => options.Include(properties ));


Posted By: cefernan
Date Posted: 20-Dec-2012 at 10:03am
Really nice Marcel, thank you very much.



Print Page | Close Window