Reputation: 11330
I have created a generic unit of work/repository pattern and it works in most cases.
Here's an example:
public IEnumerable<Deal> GetBySubcategory(int subcategoryId)
{
using (var uow = new ReadUow())
{
var r = new ReadRepo<Deal>(uow.Context);
var deals = r.FindBy(d => d.DealSubcategories.Any(s => s.SubcategoryId == subcategoryId))
.Include("DealSubcategories");
return deals.ToList();
}
}
In order to keep the size of data to a minimum, I try to filter each main query. The question I have is, how to make the above more reusable?
The above method has one filter, but this could be 2, 3 or more. If possible I don't want to create a method for each variation but as soon as the IEnumerable<T>
exits the context, it's no longer avaiable without calling ToList()
or something. This means that everything at that point is materialized, even though it may not be used and can only impact performance.
Anyone got any experience of extending this context as I've described?
Upvotes: 2
Views: 410
Reputation: 9660
Instead of returning IEnumerable<T>
, return IQueryable<T>
, by omitting the .ToList()
in your return statement.
However your DbContext would need to live throughout the length of the HttpRequest (assuming it's a website).
A while ago, I've made a blog post, somewhat related to this (I think).
Upvotes: 3