Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Generic Repository using Entity framework code first

I'm experiencing my first try on implementing Generic Repository Pattern and Unit of framework. I'm not using MVC on the project in hand. Please take a look at this method included in Generic Repository class:

public virtual IEnumerable<TEntity> Get(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> query = dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList();
        }
    }

it must be a powerful method and accomplishes the goal of DRY well. My problem is that, I cannot order the result as descending? Can anyone write some lines of code to help me on this? Thanks,

like image 451
user2394196 Avatar asked Jan 21 '26 22:01

user2394196


1 Answers

Have a look at this: http://prodinner.codeplex.com/ and this http://efmvc.codeplex.com/. These projects are good examples of simple architecture and you can see how generic repository is implemented and how it is used.

like image 154
eternity Avatar answered Jan 24 '26 17:01

eternity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!