Generic interface problem
Asked Answered
S

2

1

I'd like to have one interface for all my grid related tasks.The tasks implement this interface:

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

The T type is always a DTO class. I cant't create a common interface for this DTOs because they have nothing common.Just a dumb DTO with particular properties.

I'd like to use it like this :

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 I don't know what to put instead of the T.

Sustentation answered 11/3, 2010 at 10:20 Comment(3)
There was an error in my post, it's fixed nowSustentation
What are you going to do with coll ?Luger
follow up question asked over here #2424787Frail
K
3

Maybe Im miss-understanding you, but couldnt you make a super class that all of your DTO's like BaseDTO

Then use it like so:

public class CustomerDTO : BaseDTO {}

IDataForGrid<BaseDTO> aa;

var coll = aa.GetList();

This way, your coll variable will be of type IGridResponse<BaseDTO> which all of your DTO object extend from.

That make sense?

Kingsize answered 11/3, 2010 at 10:26 Comment(3)
Yes, you understood it well. But what to put in the base class ? The DTOs have nothing common.Sustentation
you technically dont need anything in the base class, although having nothing in there sort of feel weird. But if thats the cost of getting your way in other more detailed areas of your code, then so be it. Chances are that in the future your DTO's will end up having something in common anyways, they usually do :)Kingsize
will you not get a cast exceptions when casting? IDataForGrid<object> aa = new CustomerDataGrid();Leshia
O
3

You can make the method generic as well so that T can be substituted as required:

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();
  }
}
Openmouthed answered 11/3, 2010 at 10:23 Comment(0)
K
3

Maybe Im miss-understanding you, but couldnt you make a super class that all of your DTO's like BaseDTO

Then use it like so:

public class CustomerDTO : BaseDTO {}

IDataForGrid<BaseDTO> aa;

var coll = aa.GetList();

This way, your coll variable will be of type IGridResponse<BaseDTO> which all of your DTO object extend from.

That make sense?

Kingsize answered 11/3, 2010 at 10:26 Comment(3)
Yes, you understood it well. But what to put in the base class ? The DTOs have nothing common.Sustentation
you technically dont need anything in the base class, although having nothing in there sort of feel weird. But if thats the cost of getting your way in other more detailed areas of your code, then so be it. Chances are that in the future your DTO's will end up having something in common anyways, they usually do :)Kingsize
will you not get a cast exceptions when casting? IDataForGrid<object> aa = new CustomerDataGrid();Leshia

© 2022 - 2024 — McMap. All rights reserved.