• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MjE4

Related Code
Generic Entity Framework 4.0 Base Repository
Generic Entity Framework 4.0 Base Repository with Paging
Keep Entity Framework ObjectContext in a WCF Service OperationContext
Generic linq-to-sql repository
A Default Entity Framework 4.0 ObjectContext Factory
UnitOfWork Action filter for ASP.NET MVC and nHibernate

Loading Strategy for Entity Framework 4.0

Loading strategy for specifying how to load data, for example eager loading and define queries etc.

3
694 1 0 0 0 0

Here is a simple generic Repository (I left out some other methods, they aren't important in this current context) using Entity Framework 4.0, where a Loading strategy is added:

public class Repository<TEntity> where TEntity : class
{
    readonly ObjectContext _objectContext;
    readonly ObjectSet<TEntity> _objectSet;


    public Repository(ObjectContext objectContext)
    {
        Contract.Requires(objectContext != null);

        _objectContext = objectContext;
        _objectSet = _objectContext.CreateObjectSet<TEntity>();
    }


    public IEnumerable<TEntity> Find(ILoadingStrategy<TEntity> loadingStrategy)
    {
        var query = loadingStrategy.CreateStrategy(_objectSet);

        return query.ToList();
    }

    ...
}


Here is the ILoadingStrategy interface (I know it's a leaky abstraction but in this case if we decide to use Entity Framework and the likeliness of changing to another O/R-mapper is very minimal.

public interface ILoadingStrategy<TEntity> where TEntity : class
{
   IQueryable<TEntity> CreateStrategy(ObjectSet<TEntity> objectSet);
}


Here is an example of a Loading strategy, it will make sure it will include some tables and also perform a search:

public class SharedCodeSearchOnTitle : ILoadingStrategy<SharedCode>
{
    private readonly string _search;

    public SharedCodeSearchOnTitle (string search = null)
    {
        _search = search;
    }


    public IQueryable<SharedCode> CreateStrategy(ObjectSet<SharedCode> objectSet)
    {
        var query = objectSet.Include("Tags")
                             .Where(sharedCode => sharedCode.Approved);

        if (!string.IsNullOrWhiteSpace(_search))
            query = query.Where(c => c.Title.Contains(_search));

        return query;
    }

In code it can be used:

 var sharedCodes = new Repository<SharedCode>(...);

 var result = sharedCodes.Find(new SharedCodeSearchOnTitle(search));

What's the idea of the code?

The idea is to avoid having queries floating around in different places and also to reuse the generic Repository instead of adding query methods to different Repositories implementations.


Share: twitter | facebook   Action: used | fork | flag

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

ef4.0

Mark 'ef4.0' tag as 'like'

Mark 'ef4.0' tag as 'ignore'

repository

Mark 'repository' tag as 'like'

Mark 'repository' tag as 'ignore'


 @fredrikn "I'm the master"
3.11k
July 25, 2010 10:41 AM

Fork

 Loading Strategy for Entity Framework 4.0 -  @fredrikn Sunday 25, 2010 10:41 AM


0 Feedback


You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010