简体   繁体   English

如何实现方法 <T> (T)来自界面

[英]How can I implement method<T>(T) from an interface

I am setting up a small infrastructure that I use to communicate with different search systems (eg Apache Solr, SQL DB, etc). 我正在建立一个小型基础架构,用于与不同的搜索系统(例如Apache Solr,SQL DB等)进行通信。 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. 对于每个系统,我都有一个单独的类(=“ communicator”),该类实现一个公共接口,以便后续的应用程序可以以相同的方式与所有搜索系统“对话”。 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 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. Solr类需要一个SolrQuery类型的对象。 The MySQL class expects an object of type MySQLQuery. MySQL类需要一个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 (eg) to 然后将Solr(例如)的类定义更改为

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. SolrQuery类型仅与此一种方法(查询)相关,而不与整个类相关。 There might be other types (other than SolrQuery, eg SolrAnswer) that I need to include. 我可能需要包括其他类型(除了SolrQuery,例如SolrAnswer)。 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. 我试图将<T>部分附加到接口中的方法( query<T>(T queryObject) ),但是我没有找到任何实际实现它的方法。

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 谢谢

即使从概念的角度来看,您使用ISearchTechnology<T>也是正确的。

How about this instead: 怎么样呢:

public interface ITechnology<TQuery, TSearch>
{
    TQuery Query(TQuery queryObject);
    TSearch Search(TSearch saerchObject);
}

public interface ISolrTechnology : ITechnology<SolrQuery, SolrSearch>
{
}

public class Solr : ISolrTechnology
{
    public SolrQuery Query(SolrQuery queryObject) {
        // ...
    }
    public SolrSearch Search(SolrSearch searchObject) {
        // ...
    }
}

The interface ISolrTechnology is of course optional. interface ISolrTechnology当然是可选的。 class Solr could inherit ITechnology<SolrQuery, SolrSearch> itself. class Solr可以继承ITechnology<SolrQuery, SolrSearch>本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM