I’m looking at adding some basic search and filtering functionality in a generic/standardized way to my WCF Restful webservices.
The idea is a client will POST a SearchRequest to any container resource i.e. /users or /sessions – And the server should then construct a uri to the search results and redirect to them (POST-Redirect-GET pattern).
They way I think I need to do this (Open to suggestions) is that each searchable resource should implement an interface I define. That resource can then be used with the generic utilities I’ll create for making this only a few lines of code to implement.
The interface I have come up with is:
public interface ISearchable
{
ChunkedList<object> GetAll(int chunkStart, int chunkEnd);
ChunkedList<object> SearchByValue(string searchValue, int chunkStart, int chunkEnd);
ChunkedList<object> SearchByValueWithFilters(string searchValue, List<string> filters, int chunkStart, int chunkEnd);
}
The idea being that any resource that implements this interface can do an optimized search and limit the result set (A chunked list has a collection of objects, and a prev/next chunk uri).
The problem I have is that the interface has a generic on it ChunkedList<object> but the actual implementations want to return ChunkedList<User> or ChunkedList<Session> etc. and this gives me an invalid cast exception.
I know I can use list.convert to manually cast each item to an object, but it would be pain for every implementation to have to do this.
Is there a more appropriate interface or OO pattern to use for this? For example could I achieve something “cleaner” with a base class and derive the searchable resource off of that?
1 Answer