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 Query
- 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
More Query from same tag
- Convert System collection list <anonymous> to generic list<myviewmodel> ASP.NET-MVC5
- What's wrong with this Lambda expression?
- Linq to Entities doesn't return same value as foreach
- LINQ and various joining sample
- LINQ Lambda query with JOIN and WHERE 'not in'
- Elegant averaging of arrays from different instances of an object in C#
- Left join in Linq?
- Entity Framework LINQ Get all items part of another collection
- How to create an EF query which returns a nested list with navigation properties based on different entity ids?
- Getting SqlCeException on restart if I don't insert data to the DB
- Why is comparing a DateTime value to a DateTime value considered an invalid cast?
- Angular Application Data Binding is Not Working
- How to get the xml attribute value of root?
- Linq, Entity Framework and WCF
- How do I write this linq command to an equivalent delete SQL query?
- Why this Linq is returning null?
- EntityFramework Multiple NavigationProperties to the same table
- Is it possible to select multiple variables with LINQ without using keyword new?
- Using NOLOCK Hint in EF4?
- How to get EF Framework to generate this group by query
- Differences in using List<> returned by a List.FindAll in a foreach loop from using a local List?
- Linq query in VB.net to group several columns
- Keeping Duplicates
- LINQ Conditional Sum Calculation - cannot be applied to operands of type 'int' and 'bool'
- display it into the "Table1" table
- How do I use Linq to select the top/bottom N contestants in a Ranked Choice Vote?
- Search an element on list inside the list
- MVC ViewModel making use of a List
- Filtering nullable items in the List - LINQ
- Create a list of items within 'var response'