General interface problem

I would like to have one interface for all network related tasks. Tasks implement this interface:

public interface IDataForGrid<T> { IGridResponse<T> GetList(IGridRequest request); } 

Type T is always a DTO class. I cannot create a common interface for these DTOs because they have nothing in common. Just a dumb DTO with certain properties.

I would like to use it as follows:

 public class Service1 { public IGridResponse CreateResponse(IGridRequest request) { ... IDataForGrid<T> aa; if(request == 1) aa = new CustomerGridData; if(request == 2) aa = new OrderGridData; var coll = aa.GetList(); } } public class CustomerGridData : IDataForGrid<CustomerDTO> { ... } 

The problem is that I do not know what to put in place of T.

+1
c # interface application-design
source share
2 answers

Perhaps I missed the understanding of you, but could not make a superclass that all your BaseDTO like BaseDTO

Then use it like this:

 public class CustomerDTO : BaseDTO {} IDataForGrid<BaseDTO> aa; var coll = aa.GetList(); 

Thus, your coll variable will be of type IGridResponse<BaseDTO> , from which your entire DTO object extends.

It makes sense?

+3
source share

You can also make the method generalized so that T can be replaced as needed:

 public class Service1 { public IGridResponse<T> CreateResponse<T>(IGridRequest request) { ... IDataForGrid<T> aa; if(request == 1) cg = new CustomerGridData; if(request == 2) og = new OrderGridData; var coll = aa.GetList(); } } 
+3
source share

All Articles