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.
4 comments








