|
1
|
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
|
3 Answers
|
up vote 3 down vote accepted
|
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
public
interface IDataContext
{ void Add<T>(T entity) where T : BaseEntity; void Delete<T>(T entity) where T : BaseEntity; IQueryable<T> Find<T>(Expression<Func<T, bool>> where) where T : BaseEntity; }
where
BaseEntity is the base class to all our classes, it looks like, this class is
not mapped to any table in db
public abstract class BaseEntity
{ public int Id { get; set; } public DateTime CreateDateTime { get; set; } public string CreateUser { get; set; } public DateTime ModDateTime { get; set; } public string ModUser { get; set; } public byte[] RowVersion { get; set; } }
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 be
ICollection<WFGroup> wgGroups =
this.dataContext.Find<WFGroup>((w) => true).ToList();
where
WFGroup is a class derived from BaseEntity, I typically use lazy loading and
proxy and dont detach/attach objects to a context.
|
|||||
|
|
show 1 more comment
|
|
up vote 2 down vote
|
It
depends on whether you wish to have any future queries performed on the
entity and whether these should be in memory or not:
|
||||||||
|
|
|
|
up vote 2 down vote
|
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 of yield returns 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 custom IEnumerable, you can always have the old method call the new
one and materialise the results to keep compatibility with older clients.
|
Your Answer
Sign up or log in
Sign up
using Google
Sign up
using Facebook
Sign up
using Stack Exchange
Post as a guest
Name
Email
<h3>Post
as a guest</h3> <div class="form-item"> <table>
<tr> <script type="text/javascript">
StackExchange.ready(function () {
StackExchange.helpers.bindHelpOverlayEvents($('.vm input')); }); </script>
<td class="vm"> <div> <label
for="display-name">Name</label> <input
id="display-name" name="display-name" type="text"
size="30" maxlength="30" value=""
tabindex="105"> </div> <div> <label
for="m-address">Email</label> <input
id="m-address" name="m-address" type="text"
size="30" maxlength="100" value=""
tabindex="106"> <span
class="edit-field-overlay">required, but not shown</span>
</div> </td> </tr> </table> </div>
Not the answer you're looking for? Browse other questions tagged entity-framework-4.1 repository appfabric azure-appfabric appfabric-beta-2 or ask
your own question.
tagged
|
asked
|
1 year ago
|
|
viewed
|
1385 times
|
|
active
|
Community Bulletin
Linked
Related
<div><img
src="/posts/8102149/ivc/0664" class="dno" alt=""
width="0" height="0"></div>
about help badges blog chat data legal privacy policy jobs advertising info mobile contact us feedback
|
Technology
|
|
|
Life / Arts
|
Culture / Recreation
|
Science
|
Other
|
site design /
logo © 2013 stack exchange inc; user contributions licensed under cc-wiki with attribution
required
rev 2013.6.12.735
<div
id="noscript-warning">Stack Overflow works best with JavaScript
enabled<img
src="http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif"
alt="" class="dno"></div>
