views:

38

answers:

1

Suppose I have a Comment class having properties and their methods like

public Comment GetComment(Guid id)

And

public static List<Comment> GetComments()
public static  List<Comment> GetCommentsByAuthor(Author id)

Now, What I usually do is write the database logic for each of the above methods . That said, Now I am seeing BlogEngine.NET code and He wrote in this way; He wrote the Database logic for GetComments() method and extracted from it for all remaining methods, even for GetComment(Guid id) by using something like

   //FindAll is a method in List<T> class
    FindAll(delegate(Comment comment)
    {
    return comment.Author.Equals(author ,
    StringComparison.OrdinalIgnoreCase);
    }
    );

My question is, is it a good idea to do it in this way rather than the first approach? Any pros and cons of this way and suggestions and pointers and resources are most welcome. TIA Srinivas

+2  A: 

It's a good idea to re-use code where possible rather than rewriting, but this is not the best way to do it. It requires extracting all rows from the database and then filtering them on the client. If you use Linq, you can follow the same pattern of reusing queries, but the filtering will be done in the database instead of on the client, improving performance.

public IQueryable<Comment> GetCommentsByAuthor(Author author) {
    return GetComments().Where(comment => comment.Author.Id == author.Id);
}
Mark Byers