French ALT .NET Demo

Last week, I spoke at an EF session for the french ALT .NET community.

During its course, I made a WCF demo with EF POCO and a sort of LINQ provider in the client part.

This provider is very simplistic (made in less than 15 minutes), isn't thread safe but is interesting for the POC and because if my provider doesn't support a LINQ method, it will use LINQ To Object method.

I just want to come back on this provider, which is very different than normal LINQ providers, and particularly on this code:

public static class CategoryClient

{

    public static MyQueryable<Category> Categories

    {

        get

        {

            ClientLINQ.AllEntities = true;

            return GetCategories().ToMyQueryable();

        }

    }

 

    private static IEnumerable<Category> GetCategories()

    {

        List<Category> value;

        if (ClientLINQ.AllEntities)

            value = Context.Instance.CategoryContract.GetAllCategories();

        else

            value = Context.Instance.CategoryContract.GetCategories(ClientLINQ.IncludeValues, ClientLINQ.WhereValue, ClientLINQ.OrderByValue, ClientLINQ.SkipValue, ClientLINQ.TakeValue);

        foreach (var category in value)

            yield return category;

    }

}

How does it work? The set of AllEntities to true initializes IncludeValues, WhereValues, etc

IncludeValues = null;

WhereValue = null;

OrderByValue = null;

SkipValue = 0;

TakeValue = 0;

How can AllEntities be false in GetCategories method whereas the instruction which follows AllEntities = true is the GetCategories call and this method starts with a test on AllEntities ?

In fact, you should see that this method uses yield return syntax. So, it will be run only when Categories properties will be iterated. This means that the different LINQ methods are called before. That's why it's works.

Published Tue, Apr 28 2009 16:13 by Matthieu MEZIL

Comments

# EF with WCF recent demos

I recently made a demo of WCF and EF with POCO for French ALT .NET event . Danny made a great demo for

Thursday, June 04, 2009 6:12 PM by Matthieu MEZIL

# Using T4 Templates to generate EF classes

Spurred on both by the success of Damien Guard in using T4 to generate Linq to SQL code and of Jeff Reed

Friday, June 05, 2009 12:04 AM by VS2010学习

Leave a Comment

(required) 
(required) 
(optional)
(required)