Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an interface to have a generic List object

I have a data structure something like this:

public class HighLevelConversionData
{
    public int customerID {get;set;}
    public string customerName {get;set;}
    public decimal amountSpent {get;set;}
}

This data will be accessed by a third party and a GWT front end, meaning I'll be using web services to move the data around. The customer is also of a different localisation than the dev team, so I'm wanting to send status messages as a wrapper for the returned data items, like so:

public class HighLevelConversionDataWrapper
{
    public int StatusCode {get;set;}
    public string StatusMessage {get;set;}
    public List<HighLevelConversionData> {get;set;}
}

However I'd rather have an interface for these methods to inherit from, to ensure we're always sending the statuscode & message in the same way. But my understanding of how generics work in an interface seems to be failing me. I believe it should be something like:

public Interface IServiceWrapper
{
    public int StatusCode {get;set}
    public string StatusMessage {get;set;}
    public List<T> ReturnedData {get;set;}
}

But I've come unstuck here.

like image 506
Refugee Avatar asked Jan 24 '11 09:01

Refugee


People also ask

How do you declare a generic interface?

You can declare variant generic interfaces by using the in and out keywords for generic type parameters. ref , in , and out parameters in C# cannot be variant. Value types also do not support variance. You can declare a generic type parameter covariant by using the out keyword.

Can an interface be generic?

Generic interfaces can inherit from non-generic interfaces if the generic interface is covariant, which means it only uses its type parameter as a return value.

Can we create object of an interface in C#?

Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an "IAnimal" object in the Program class) Interface methods do not have a body - the body is provided by the "implement" class. On implementation of an interface, you must override all of its methods.


2 Answers

Something like this?

public class ConcreteWrapper : IServiceWrapper<HighLevelConversionData>
{
    public int StatusCode {get;set;}
    public string StatusMessage { get; set; }
    public List<HighLevelConversionData> ReturnedData { get; set;}
}

public class HighLevelConversionData
{
    public int customerID {get;set;}
    public string customerName {get;set;}
    public decimal amountSpent {get;set;}
}

public interface IServiceWrapper<T>
{
    int StatusCode { get; set; }
    string StatusMessage { get; set; }
    List<T> ReturnedData { get; set;}
}
like image 160
Rob Avatar answered Sep 20 '22 15:09

Rob


If the interface has generic type parameters, the interface itself must be generic, so you'll need to do:

public interface IServiceWrapper<T> 
{     
  public int StatusCode {get;set}     
  public string StatusMessage {get;set;}     
  public List<T> ReturnedData {get;set;} 
} 

and then specify the type parameters in the code like this:

public class HighLevelConversionDataServiceWrapper 
          : IServiceWrapper<HighLevelConversionData>
{
   public List<HighLevelConversionData> ReturnedData {get;set;} 
}
like image 43
SWeko Avatar answered Sep 21 '22 15:09

SWeko