1

I'm creating a repository for basic CRUD operations and i have one question about the read functions.

I read that the IQueryable implements the IEnumerable, so. Can i create my select functions using only IQueryable return type or not???

Example:

IQueryable<T> GetQuery() 

and

IEnumerable<T> GetAll()

or only the queryable function?

IQueryable<T> GetAll()

thanks!

  • I'm using ASP.NET MVC5 and EF6 :)
tereško
  • 58,060
  • 25
  • 98
  • 150
leojnxs
  • 127
  • 1
  • 7
  • 2
    You can but what for? You need read function so go ahead with IEnumerable. The caller doesn't have to know about such things as IQueryable. Besides it's better to evaluate value when you exit from you GetAll method. – Artiom Dec 04 '13 at 14:02

1 Answers1

0

You can download from Microsoft DynamicQuery class this will help you a lot and it make your job easy.

http://msdn.microsoft.com/en-US/vstudio/bb964686.aspx

For example

public IEnumerable<T> GetAll<T>()
{
 return this.AddIncludes(this.DbContext.Set<T>()).OrderBy (this.SortSpec.PropertyName).ToList();
}

public IEnumerable<T> Find(Expression<Func<T, bool>> expression)
{
 return this.AddIncludes(this.DbContext.Set<T>()).Where(expression).OrderBy(sortingPropertyName).ToList();
}

Read this blog:

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Combine your Repository pattern with UnitOfWork pattern this will give you a better abstraction.

The repsoistory interface shall contains only the importent methods like:

public interface IRepository<T> 
{
   List<string> IncludeList { get; } // Include for EF

   IEnumerable<T> GetAll<T>();
   void Delete(T);
   void Add(T);
   IEnumerable<TEntity> Find(Expression<Func<T, bool>> expression);
}

IRepository might be also offer other methods like Any, GetRange etc, if you want.


NOW TO YOUR Q!

I read that the IQueryable implements the IEnumerable, so. Can i create my select functions using only IQueryable return type or not???

"The difference is that IQueryable is the interface that allows LINQ-to-SQL (LINQ.-to-anything really) to work. So if you further refine your query on an IQueryable, that query will be executed in the database, if possible.

For the IEnumerable case, it will be LINQ-to-object, meaning that all objects matching the original query will have to be loaded into memory from the database." See: Returning IEnumerable<T> vs. IQueryable<T>

Community
  • 1
  • 1
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
  • What you think is better: `public IQueryable GetAll() >>> return _context.DBSet();` or `public IEnumerable GetAll() >>> return _context.DBSet().ToList();`. I know that IQueryable it's more faster than IEnumerable because the query runs completely in the server, IEnumerable executes the query filters in the client side and IQueryable allows me to use LINQ-TO-ANYTHING. So, if IQueryable it's better than IEnumerable, why use IEnumerable for my read functions in my repository, you understand now?! I think IEnumerable is better for array collections for example. I'm correct? – leojnxs Dec 04 '13 at 15:55
  • Yes, I agree with you; The repository interface shall not prvoide IQueryable this will make the live diffcult for the Repository users. Good desgin offers the required functions just like that IEnumerable GetAll() >>> return _context.DBSet().ToList(); ((BUT)) the repository users must know this can be expnisve, if the do GetAll they might be load the whole Database. – Bassam Alugili Dec 04 '13 at 16:19