I am setting up a small infrastructure that I use to communicate with different search systems (e.g. Apache Solr, SQL DB, etc). For each system I have one individual class (=”communicator”) that implements a common interface, so that the subsequent application can “talk” to all search systems in the same way. Furthermore, each search system has its own class that I use to create the actual query. An object of that individual query class holds all relevant query parameters and is passed to the according communicator class. So for Solr it looks something like this:
Solr solr = new Solr();
SolrQuery sQuery = new SolrQuery();
//use some methods on sQuery to specify the query
var result = solr.query(sQuery);
So far so good. But how do I specify the query-method in the interface? The Solr class expects an object of type SolrQuery. The MySQL class expects an object of type MySQLQuery.
One solution that does what I want is this: I change the whole interface like this:
interface ISearchTechnology<T>
{
someReturnType query(T queryObject);
}
and then change the class definition of Solr (e.g.) to
class Solr : ISearchTechnology<SolrQuery>
That works. But this makes no sense conceptually. The type SolrQuery is only relevant for this one method (query), not for the whole class. There might be other types (other than SolrQuery, e.g. SolrAnswer) that I need to include. Including all of those like this is confusing and violates my understanding of the concept.
I tried to attach the <T> part to the method in the interface (query<T>(T queryObject)), but I haven’t found any way to actual implement it.
In the end I want to have the following situation: Each communicator class implements the query method as required by the interface, but only accepts one specific type for the parameter that is different from all other communicator classes.
Thanks
Your approach with
ISearchTechnology<T>is correct, even from conceptual standpoint.