My project is structured as follows:
DAL
public IQueryable<Post> GetPosts()
{
var posts = from p in context.Post select p;
return posts;
}
Service
public IList<Post> GetPosts()
{
var posts = repository.GetPosts().ToList();
return posts;
}
//Returns a list of the latest feeds, restricted by the count.
public IList<PostFeed> GetPostFeeds(int latestCount)
{
List<Post> post - GetPosts();
//CODE TO CREATE FEEDS HERE
return feeds;
}
Lets say the GetPostFeeds(5) is supposed to return the 5 latest feeds. By going up the list, doesn’t it pull down every single post from the database from GetPosts(), just to extract 5 from it?
If each post is say 5kb from the database, and there is 1 million records. Wont that be 5GB of ram being used per call to GetPostFeeds()?
Is this the way it happens? Should I go back to my DAL and write queries that return only what I need?
As long as you’re working with
IQueryable, query execution can be deferred. The query is executed once you callToList()or do something else that requires data to be fetched (e.g. iterating over a child collection).I don’t think you need to worry about your DAL too much as I like to keep them fairly lean. You could take advantage of using deferred execution by rewriting your
GetPostFeedsmethod though, e.g.: