1

We've seen some very useful methods of implementing EF4 Repository and Unit of Work pattern (Reference 1, Reference 2)

All the examples I see use methods requiring the use of LINQ Expression Syntax, e.g.:

IEnumerable<T> Query(Expression<Func<T, bool>> filter) 

var employees = employeeRepository.Query(emp => emp.Username == userName); 

But I want to use LINQ Query Expression syntax, e.g.:

from emp in context.Employees select emp where emp.Username == userName;

Is there a way to enjoy the benefits of a repository and UoW whilst being able to use LINQ Query Expression syntax in my repository methods?

Richard

Richard
  • 5,810
  • 6
  • 29
  • 36

1 Answers1

2

Yes but you must expose IQueryable<T>:

IQueryable<T> Query();

After that you can use code like:

var query = from emp in EmployeeRepository.Query() 
            where emp.Username == userName
            select emp;

The difference between approaches was discussed yesterday. Also whole concept of repository and unit of work was discussed many times - including why to use them and what problems you can expect.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • +1 That's the way I do it too. It still enables lambda expressions such as employeeRepository.Query().Where(emp => emp.UserName == userName); – Danny Varod Jun 21 '11 at 08:53
  • That looks good. I know the repository discussion has been done to death. Funny 2 discussions on this topic hit around the same time :) I think the fact there are so many discussions around these topics serves to highlight some pretty serious weaknesses in EF4. – Richard Jun 21 '11 at 22:56