I'd like to make my application as flexible as possible, but not dig myself into a hole by making my Interface too specific.
What is the best object type for a repository? IEnumerable, IQueryable, or List?
The technologies I'm considering using are
Azure App Fabric Caching
Entity Framework 4.1
Possibly Windows Server AppFabric
It depends on whether you wish to have any future queries performed on the entity and whether these should be in memory or not:
How likely are you to ever need to return a custom implementation of
IEnumerable
(not a collection) from your DAL? (To answer this question, look at your previous projects and count how many of those or ofyield return
s you have around.)If the answer is "not very", I'd just return
ICollection
or even arrays (if you want to prevent the query results from being inadvertently modified.) In a pinch, if you ever need to change a query to "stream" results with a customIEnumerable
, you can always have the old method call the new one and materialise the results to keep compatibility with older clients.There is a very good recent article here that covers this, namely under the heading "Repositories that return IQueryable". This is what it says:
As a result of this, I've added the following coding convention within my team:
The reasoning being that IQueryable would allow the caller to build on this and ultimately modify the SQL query that is executed on the database. This can potentially be dangerous in terms of DB performance, but it is more about SoC. The caller doesn’t care about the data source; it just wants the data.
I would say build your DAL using IQueryable, and pass it around, make sure your object contects lifetime is the request. This way you will get benefit of delayed execution, but are exposed to the risk of inefficient querying of database.
Then make sure you performance test your application( or atleast the parts that are most likely to get traffic) and see the dataaccess patterns. Create specialized methods in your DAL to retreive fully materialized onjects and make these queries as pre compiled queries.
a sample of the repository interface would be like
where BaseEntity is the base class to all our classes, it looks like, this class is not mapped to any table in db
Expression<Func<T, bool>>
would pass the whole expression to your repository instead of just Func, since EF works on expression to generate sql query, a typical use would bewhere WFGroup is a class derived from BaseEntity, I typically use lazy loading and proxy and dont detach/attach objects to a context.