score:3
We use something very similar. One thing you need to decide on is if you are going to expose IQueryable outside of the repository. Your find method returns IEnumerable which could be the IQueryable returned from your when clause.
The advantage of returning the IQueryable is that you can further refine your criteria up outside of your repository layer.
repository.Find(predicate).Where(x => x.SomeValue == 1);
The expression will only be compiled when you come to use the returned data and here in lies the disadvantage. Because you only hit the database when you actually come to use the results you could end up trying to call the database after your session (nhibernate) or connections have been closed.
My personal preference is to use the specification pattern where you pass your find method an ISpecification object is used to do the query.
public interface ISpecification<TCandidate>
{
IQueryable<TCandidate> GetSatisfyingElements(IQueryable<TCandidate> source);
}
public class TestSpecification : ISpecification<TestEntity>
{
public IQueryable<TestEntity> GetSatisfyingElements(IQueryable<TestEntity> source)
{
return source.Where(x => x.SomeValue == 2);
}
}
public class ActiveRecordFooRepository: IFooRepository
{
...
public IEnumerable<TEntity> Find<TEntity>(ISpecification<TEntity> specification) where TEntity : class
{
...
return specification.GetSatisfyingElements(ActiveRecordLinq.AsQueryable<TEntity>()).ToArray();
...
}
public TEntity FindFirst<TEntity>(ISpecification<TEntity> specification) where TEntity : class
{
return specification.GetSatisfyingElements(ActiveRecordLinq.AsQueryable<TEntity>()).First();
}
}
After the query is run the repository calls ToArray or ToList on the resulting IQueryable returned from the specification so that the query is evaluated there and then. Whilst this may seem less flexible than exposing IQueryable it comes with several advantages.
- Queries are executed straight away and prevents a call to the database being made after sessions have closed.
- Because your queries are now bundled into specifications they are unit testable.
- Specifications are reusable meaning you don't have code duplication when trying to run similar queries and any bugs in the queries only need to be fixed in one place.
- With the right kind of implementation you can also chain your specifications together.
repository.Find(
firstSpecification
.And(secondSpecification)
.Or(thirdSpecification)
.OrderBy(orderBySpecification));
score:0
Is passing a Func as a parameter to your service layer's Find method, instead of the FooSearchArgs, an option? Enumerables have a Where method (linq) that takes a Func as a parameter, so you could use it to filter the results.
Source: stackoverflow.com
Related Articles
- Advice With Repository/Service Layer Design Pattern
- Linq sub query when using a repository pattern with EF code first
- Compiled Linq with Generic Repository Design Pattern
- Dealing with Queries in a Repository Pattern with multiple concrete implementations?
- Repository pattern with "modern" data access strategies
- Repository Pattern with Caching and SqlMethods
- Problem With Repository Pattern & Abstract Classes
- How to get a count of records with entity framework and repository pattern
- Abstraction with repository pattern
- LINQ: returning 2 SELECTs in 1 IQueryable for pagination? Keeping in lines with the repository pattern and not changing the return type
- LINQ-to-SQL repository pattern attempt that is working, but am concerned with seperation
- Design of Service Layer and Repositories in Microsoft MVC
- EF How to query more entities with .include() and using repository pattern
- Query with Repository Pattern
- Filter dataset in repository design pattern C#
- Search records with code pattern in column value c#
- What does this C# code with an "arrow" mean and how is it called?
- Entity Framework, Navigation Properties, and the Repository Pattern
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- NHibernate and Repository pattern
- C# Replace all elements of List<string> with the same pattern with LINQ
- passing dynamic expression to order by in code first EF repository
- Should the UI layer be able to pass lambda expressions into the service layer instead of calling a specific method?
- Why doesn't this code compile in VS2010 with .NET 4.0?
- Eager load ALL by default with Entity Framework within a Generic Repository
- Why is this code with PredicateBuilder not working?
- "the method join is not supported" with Tridion OData service & Linq
- Can you advise me a resource with LINQ/lambda code exercises?
- EF4, Lambda, Repository pattern and DTOs
- LINQ Source Code Available
- How to use GroupBy with Join in Linq's method syntax in EF?
- Getting mongodb documents but ToListAsync on IQueryable throws exception
- How to return a List(Of String) from a LINQ statement with a Group By in VB.net?
- C# SQL/Linq/Entity Framework calculating column totals for multiple columns from large data source
- Linq Dynamically append Where clauses with OR
- Find minimal and maximal date in array using LINQ?
- Very easy lambda expression
- Reuse methods containing same properties but different lambda where clause condition
- orderby () containing numbers and letters
- Optimise LINQ query
- Using String.compare inside a lambda expression
- Moving SP-Linq queries from test site to production site
- Linq not contains dynamic query
- Using LINQ to query nested classes in db4o?
- Is there a better way to code this LINQ fragment?
- LINQ to XML: value of sibling if another sibling element exists
- DataBinding Linq to SQL table to DataGrid using XAML
- Linq Query Returns Incorrect Result Set
- How to sum decimal value from a column in a table in html file for C#
- I want to print Count using Groupby and Left join in linq