3

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,

user2394196
  • 341
  • 5
  • 18

2 Answers2

2

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.

eternity
  • 1,668
  • 15
  • 25
0

To filter by product category, try this:

var repo = new GenericRepository<Product>();

var results = repo.Get(
    p => p.Category.Name == "Foo");

Here we declare an instance of the Generic Repository, with an entity type of Product, then we pass a lamda expression that performs the filtering on each Product's Category whose name is "Foo".

Netricity
  • 2,550
  • 1
  • 22
  • 28
  • Well... Do you have any idea about where the filter operation is taken place? I precisely mean; the web server memory or the database? And also can you help how to order the result descending? Sorry, this is the first time working with this pattern and I know I need to study more about GENERICS in C# :( But I'd appreciate if could help me again.. – user2394196 May 17 '13 at 14:28
  • The filtering takes place in the database; EF converts your lambda expression to SQL and passes it to the database. Sorry, I don't know about the ordering, as I'm not familiar with that Func syntax. Perhaps someone else can help with that? – Netricity May 17 '13 at 14:44
  • PS, if you still need help with this, maybe don't mark the question as answered – Netricity May 17 '13 at 14:46
  • By this way some amount of memory are being saved and good deal. Ok my friend no problem for not answering the ordering.. Thanks again – user2394196 May 17 '13 at 14:48
  • oOopsSs.. Yes.. you're right.. But sorry I just cant hit the button of 'This answer is useful' I lack of some reputation and some stuff like that – user2394196 May 17 '13 at 14:51
  • Sorry bothering you with some comments but I finally figure it out and like to share it with you: return unitOfWork.CermonyCategoryRepository.Get(orderBy: cc => cc.OrderByDescending(s => s.CermCatID)).ToList(); – user2394196 May 17 '13 at 15:01
  • Glad you sussed it. Looks pretty simple once you know what you're doing. The intellisense makes it look complex. – Netricity May 17 '13 at 15:34