Living in the Tech Avalanche Generation

A practitioner’s introspective on technology

Fetching Strategies for the Entity Framework - a waste of time for now?

I really want Fetching Strategies for the Entity Framework however my efforts in making this happen have been exhausted for now. Implementing Fetching Strategies (eager loading only) was possible in LINQ To SQL and I expect that abstracting the .Includes extension method on ObjectQuery<T> is just as feasible, however what’s questionable right now is whether it’s worth implementing a half baked solution that only helps in eager fetching. Building calls to .Includes() in non generic methods implemented by a Repository, would probably be the best place to do this for Repositories implemented on top of Entity Framework V 1.0.

public class EntitiesRepository<E, C> where E : EntityObject
                                      where C : ObjectContext
{
    private readonly C _ctx;

    //Unit of Work Accessor
    public C Session
    {
        get { return _ctx; }
    }

    //Constructor
    public EntitiesRepository(C session)
    {
        _ctx = session;
    }

    //Common Unit of Work Methods
    public void Save()
    {
        _ctx.SaveChanges();
    }

    /// <summary>
    /// A generic method to return ALL the entities
    /// of type TEntity. This overload will use the
    /// the parameter entitySetName in the resolution of mapping
    /// between the CSDL and SSDL. This method is useful
    /// for Models that DO have their pluralized names
    /// changed by the developer. For example if Customers
    /// table from Northwind produces an Entity that by
    /// default is named Customers but has it’s name changed
    /// to Customer this method call would fail. In Version
    /// 2.0 this pluralizing behaviour will change and this
    /// method overload should be used only if Developers
    /// change EntitySet names from the default name generated.
    /// </summary>
    /// <param name=”entitySetName”>
    /// The EntitySet name of the entity in the model.
    /// </param>
    /// <typeparam name=”TEntity”>
    /// The Entity to load from the database.
    /// </typeparam>
    /// <returns>Returns a set of TEntity.</returns>
    public ObjectQuery<E> All(string entitySetName)
    {
        return (ObjectQuery<E>)_ctx.CreateQuery<E>(“[" + entitySetName + "]“);
    }

    /// <summary>
    /// A generic method to return ALL the entities
    /// of type TEntity. This overload will use the
    /// typeof(TEntity).Name in the resolution of mapping
    /// between the CSDL and SSDL. This method is useful
    /// for Models that DO NOT have their pluralized names
    /// changed by the developer. For example if Customers
    /// table from Northwind produces an Entity that by
    /// default is EntitySet name is NOT changed.
    /// </summary>
    /// <typeparam name=”TEntity”>
    /// The Entity to load from the database.
    /// </typeparam>
    /// <returns>Returns a set of TEntity.</returns>
    public ObjectQuery<E> All()
    {
        return (ObjectQuery<E>)_ctx.CreateQuery<E>(“[" + typeof(E).Name + "]“);
    }

    //…etc etc etc
}

public class CustomerRepository : EntitiesRepository<Customer, NorthwindEntities>
{
    public IQueryable<Customer> GetCustomerByCountry(string country)
    {
        var custs = from c in Session.Customers
                    where c.Country == country
                    select c;

        return custs;
    }

    /// <summary>
    /// Uses the base constructor
    /// to inject the Unit of work
    /// session. (should new up the
    /// session outside of here if
    /// it’s being used accross 
    /// repositories. Also change to
    /// add the context to the constructor
    /// parameters as well)
    /// </summary>
    public CustomerRepository() :
        base(new NorthwindEntities()) { }

}

My fear is that if the changes / upgrades to the Entity Framework for Version 2.0 don’t cut deep enough many will lose faith and patience and may be forced by default into using some other ORM. Personally this is a critical feature for me however for the moment I have decided there is no satisfying solution to this problem and so I am going to leave Fetching Strategies out of my library for now. I will post the entire code soon.

Share/Save/Bookmark

4 Comments so far

  1. Phillip January 8th, 2009 7:49 am

    Hey,

    I’ve been looking at other O/R Mapping tools lately, mainly because Ayende Rahien floods my RSS feeds with NHibernate crap every morning, and i hate the damn thing!

    But I found one in some Microsoft forum thread which is called LightSpeed.

    So far the thing kicks ass, i have a few issues with it, like i hate the fact it renames my identity properties to Id instead of leaving it as PostID.

    However the model is as simple as LINQ2SQL, but has far more functionality than either the EF or LINQ2SQL.

    It even supports FullText searching, many to many relationships, and a buttload of other features which i actually spent ages writing into my LINQ2SQL projects.

    I REALLY suggest you take a look at it.

    http://www.mindscape.co.nz/Products/LightSpeed/comparison.aspx

    To be honest, altho im using LINQ2SQL for a really large project at the moment, i’ve given up on the EF and im in the process of ripping it out of a personal project at home and slowly replacing it with LightSpeed.

    [Reply]

  2. Phillip January 8th, 2009 7:52 am

    Oh i forgot to mention, you might wanna take a quick look at the Egar Loading video.

    http://www.mindscape.co.nz/products/lightspeed/screencasts/eagerloading.aspx

    [Reply]

  3. [...] Most recently I also made it clear that I felt the idea of Fetching Strategies was (for now) a waste of time (in the Entity Framework) but I have (as promised) reworked the earlier code libraries I posted for [...]

  4. [...] an approach to lazy and eager loading with the Entity Framework. I was ready to give up on the idea a while back but I am a stubborn PITA. It has been my goal for a while to try get Specifications, [...]

Leave a reply

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