Creating an interface for creating a shared 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 available to third parties and the GWT interface, which means that I will use web services to move the data. The client also has a different localization than the development team, so I want to send status messages as a wrapper for the returned data items, for example:

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

However, I would prefer to have an interface for these methods to inherit to ensure that we always send the status code and message in the same way. But my understanding of how generics work in an interface does not seem to allow me. I believe this 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 stuck here.

+4
source share
2 answers

Something like that?

 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;} } 
+7
source

If the interface has common type parameters, the interface itself must be shared, so you will 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 as follows:

 public class HighLevelConversionDataServiceWrapper : IServiceWrapper<HighLevelConversionData> { public List<HighLevelConversionData> ReturnedData {get;set;} } 
+2
source

Source: https://habr.com/ru/post/1336762/


All Articles