New Posts New Posts RSS Feed: Using the new methods with fetchOptions
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Using the new methods with fetchOptions

 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: Using the new methods with fetchOptions
    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?

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: 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));
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: 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 ));


Edited by mgood - 20-Dec-2012 at 9:39am
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: 20-Dec-2012 at 10:03am
Really nice Marcel, thank you very much.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down