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.

This entry was posted in 10369, 7671, 7672, 7674, 7677. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>