Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define a non-generic interface which can have generic methods?

Tags:

c#

oop

generics

I was wondering if this code could be improved. The IProvider implements IProvider and overwrites Request(...). I'd like to combine these into a single interface. But I still need a typed and untyped interface to work with.

Is there a way to combine these where I get both or is this how the interfaces should look?

public interface IProvider
{
    DataSourceDescriptor DataSource { get; set; }

    IConfiguration Configuration { get; set; }

    IResult Request(IQuery request);
}

public interface IProvider<T> : IProvider
{
    new IResult<T> Request(IQuery request);
}
like image 363
phillip Avatar asked Mar 09 '11 14:03

phillip


1 Answers

If the generic type parameter T is only relevant in the context of the Request method, declare only that method as generic:

public interface IProvider
{
    DataSourceDescriptor DataSource { get; set; }

    IConfiguration Configuration { get; set; }

    IResult Request(IQuery request);

    IResult<T> Request<T>(IQuery request);
}
like image 179
Daniel Hilgarth Avatar answered Oct 19 '22 22:10

Daniel Hilgarth