Living in the Tech Avalanche Generation

A practitioner’s introspective on technology
Archive for February 24th, 2009

Entity Framework, Repositories, Specifications and Fetching Strategies Part 10.0 (or Lazy Loading and Fetching Strategies again, with feeling)!

One of the main goals of the previous post in this series was to take a look at Fetching Strategies for the Entity Framework and how we might be able to achieve them with a single approach, particularly in light of the fact that the Entity Framework’s  Lazy Loading (if we can call it that) is enlisted explicitly and from an API perspective it’s completely removed from how it manages eager fetching.

When we first looked at the Fetching Strategy the implementation looked something like this:

public class MultiLevelMixedStrategy : IFetchingStrategy
{
    private readonly IList<FetchingIntention> _intentions =
        new List<FetchingIntention>();

    public MultiLevelMixedStrategy()
    {
        this.Intentions.Add(new FetchingIntention(“Orders”,
                            FetchMode.Eager));
        this.Intentions.Add(new FetchingIntention(“Orders.Order_Details”,
                            FetchMode.Eager));
        this.Intentions.Add(new FetchingIntention(“Product”,
                            FetchMode.Lazy));
    }

    public IList<FetchingIntention> Intentions
    {
        get { return _intentions; }
    }
}

The next iteration had seen to it that the magic strings were not the only option when we want to new up a FetchingIntention; so lambda expressions became part of the menu. This iteration had our FetchingStrategy looking like this:

public class RoleStrategy : IFetchingStrategy<ICustomerMakePrefered>
{
    private readonly IList<FetchingIntention> _intentions =
        new List<FetchingIntention>();

    public RoleStrategy()
    {
        var order_intention =
            FetchingIntention.CreateInstance
            <Customer, EntityCollection<Order>>
            (c => c.Orders, FetchMode.Eager);

        var order_details_intention = order_intention.And(
            FetchingIntention.CreateInstance
            <Order, EntityCollection<Order_Detail>>
            (o => o.Order_Details, FetchMode.Eager));

        var product_intention = FetchingIntention.CreateInstance
            <Order_Detail, Product>
            (od => od.Product, FetchMode.Lazy);

        this.Intentions.Add(order_intention);
        this.Intentions.Add(order_details_intention);
        this.Intentions.Add(product_intention);
    }

    public IList<FetchingIntention> Intentions
    {
        get { return _intentions; }
    }
}

As I said in the previous post, the compounding of arguments that would ultimately build up the string used in an ObjectQuery<T>.Include(”object.relations.etc”) method call, was achieved by chaining FetchingIntention objects together with a fluent interface. For example:

var intent = order_intention
            .And(order_details_intention)
            .And(product_intention);

Finally I want to mention that whilst we did get Lazy Loading implemented as well (without it there is no support for the Fetching Strategies), I haven’t made a point of highlighting it because ultimately I find it reasonably uninteresting. You can look deeper into how the Lazy Loading was implemented (by looking at the code) with the aid of post sharp and some AOP’ish post compile weaving.

Share/Save/Bookmark

2 comments

Creative Commons Attribution-ShareAlike 2.5 Australia
Creative Commons Attribution-ShareAlike 2.5 Australia