When I started I had an interface:
public interface IBlogReader
{
BlogPost GetLatest();
IEnumerable<BlogPost> FindByDate(DateTime from, int limit);
IEnumerable<BlogPost> FindByCategory(string category, int limit);
IEnumerable<BlogPost> FindByAuthor(string author, int limit);
}
Then I needed different permutations of the queries (FindByAuthorWithCategory, FindByDateWithAuthor, etc.) I figured I needed to change the approach as this would keep growing. I need more and different queries. Next I had an interface such as:
public struct FindCriteria
{
DateTime? from;
string category;
string author;
}
public interface IBlogReader
{
BlogPost GetLatest();
IEnumerable<BlogPost> Find(FindCriteria criteria);
}
The interface was smaller and I could refrain from making many methods. However, I basically moved the many method implementation into a big-honking single method implementation. It made me feel better for a while. I was thinking of moving to an approach that wrapped the big-honking query method into a series of individual objects where each object handled the specifics and I call for the results:
public abstract class QueryCommand
{
protected IBlogReader reader = null;
public QueryCommand(IBlogReader reader)
{
this.reader = reader;
}
public abstract void Execute();
}
public class GetLatest : QueryCommand
{
public BlogPost GetResults();
public void Execute();
}
public class FindByDate : QueryCommand
{
public IEnumerable<BlogPost> GetResults();
public void Execute();
}
It still doesn’t feel right. I read about the Repository pattern and I don’t see how it applies. It seemed to me that I would still end up with the many methods in the end. The data is being stored in the cloud. Using the Specification pattern would be too heavy weight as my reading of it would bring all of the records local to qualification. In another attempt I created individual DAO objects and wrapped them inside of a repository-like facade object. The end result was the same as I began with…many methods…but the repository didn’t do all of the work.
Should I just resolve myself to having either many methods or many objects and get over it?
It looks like Query Object.
Sometimes we also use variation of it:
And then resolving non-null criteria to filter expression.