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
- Getting DISTINCT values from a JOIN
- LINQ using Contains with entity
- Dynamic LINQ, Select function, works on Enumerable, but not Queryable
- Lambda Expression for Many to Many realtionship in C# EF 5 Code First
- LINQ: Retrieve model data after GroupBy on joined data
- Linq Dynamic OrderBy
- Merging 2 lists with linq based on unique Id to avoid duplicates
- Prevent adding duplicate items on ListView
- LINQ to Entities does not recognize the method 'System.DateTime ConvertShamsiToMiladi(System.String)' method
- LINQ provider to a Web API causing lots of grief
- DateTime Format yyyymm
- without for loop how can is show var type these value in TextBox
- Replacing a foreach with LINQ
- Filtering a list of objects without linq c#
- C# one-liner to grab specific data from an RSS feed
- ASP.NET MVC EntityFramework: how to use LINQ inside ActionResult controller method?
- LINQ query against GridView filled with SqlDataSource
- Search method joing multiple tables
- Take groups of 5 strings from List
- LINQ .Select() is quite slow
- Linq order by issue
- Linq List of Objects GroupBy Multiple Property Equality
- Performing aggregate sum on custom class IQueryable(Of T)
- C# linq group by
- LINQ track changes
- How to use Rx to monitor a file for changes?
- Find a set number of values from collection
- Empty set in LINQ in C#
- Why does the StringComparison.InvariantCultureIgnoreCase not work with this Db4o linq query?
- How to write recursive lambda expression (LINQ query)