Class Design with Generic Class and Interface in C#

Ricky

I am working on an old piece of code and trying to implement it afresh with the new advancements in .NET. However I can't wrap my head around the design for the same. Previously there were no template classes/interfaces and now I need to make use of the same. I will try to give an example of the design and where I am getting stuck. The design is something like this :

interface Service<T>
{
    T Value;
    Task AsyncWork();
}

class Input<T> : Service<T>, Input
{
    Worker w1;
    Task AsyncWork()
    {
        w1.WorkOnInput(this); //error
        ... //will return a Task eventually
    }

}

class Input
{
    //common members and methods for child classes
    int priority;
    string Name;
    FormatInput()
    {
        //some common implementation
    }

}

class StringInput:Input<string>
{
    //Implementation specific to string input
}

class IntInput:Input<int>
{
    //Implementation specific to int input
}

class Worker
{
    WorkOnInput(Input)
    {
        ...
    }
}

Main()
{
    Worker w = new Worker();
    Input input1 = new StringInput();
    Input input2 = new IntInput();
    input1.FormatInput();
    input2.FormatInput();
    List<Input> inputList = new List<Input>();
    inputList.Add(input1);
    inputList.Add(input2);
    AnotherMethod(inputList); //another method which expects a list of Inputs
    w.WorkOnInput(input1);
    w.WorkOnInput(input2);
}

I cannot change the interface implementation as I am not the owner of the same. But as the comment shows I would have an error at w1.WorkOnInput(this), since this here expects Input type and not Input<T>.

However if I change the WorkOnInput to accept an argument of type Input<T> then I would have to make it a generic method as WorkOnInput<T> and if I would need to call it I would explicitly have to provide the type of the input which is also not desirable.

Also I have a list of inputs which is needed to be passed to AnotherMethod() and a List<Input<T>> is not possible.

I think I am getting a bit too confused with the scenario and am going round and round without any concrete solution.

Can someone please point me into the right direction?

Charles Bretana

Shouldn't class Input<T> : Service<T>, Input be class Input<T> : Input, Service<T> ?

... and if you can, you should rename Service<T> as IService<T> - It is an interface not a class. By following best practice naming conventions, it would make writing

class Input<T> : IService<T>, Input

clearly and obviously wrong, cause the interface dependency is listed before the one and only allowed base class.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Generic extending class AND implements interface in Kotlin

From Dev

Generic class with an interface constraint vs class implementing interface

From Dev

C++ generic class design with methods which return subclass

From Dev

Casting in generic abstract class that implements interface

From Dev

Instantiating generic class and implementing generic interface

From Dev

The class design - interface or abstract class?

From Dev

Generic class with two type constraints, and a interface implementation

From Dev

Casting in generic class to interface delphi

From Dev

Use a generic class as a custom view in Interface Builder

From Dev

Java proper extension design for a class with a generic collection

From Dev

Parameterizing a generic interface in class declaration

From Dev

Javassist: Create class that implements generic interface

From Dev

CLI/C++ Interface class, Generic method calling template method

From Dev

Using a interface as a type parameter of generic class with the limitation of "class" in C#

From Dev

Abstract class implement a generic Interface Type

From Dev

A singleton class to design a generic deck of card?

From Dev

Class design:class implementing an Interface implementing another interface

From Dev

Modifying value type in generic class implementing generic interface C#

From Dev

C# Generics : CS0311 when implementing generic class that meets a generic interface contract

From Dev

"Class does not implement interface member" error on class implementing a generic interface

From Dev

Generic C# class with limited types to some interface is not implementing other interface with member of the first one

From Dev

CLI/C++ Interface class, Generic method calling template method

From Dev

C# - Cast generic interface to generic class that pass type <T> that is determined at runtime

From Dev

A singleton class to design a generic deck of card?

From Dev

Class design:class implementing an Interface implementing another interface

From Dev

Non-intrusive design of generic C++ class

From Dev

C# generic constraint for base class and interface

From Dev

Casting a Generic Class<T> in C# from an Interface?

From Dev

Register generic interface with non generic class

Related Related

  1. 1

    Generic extending class AND implements interface in Kotlin

  2. 2

    Generic class with an interface constraint vs class implementing interface

  3. 3

    C++ generic class design with methods which return subclass

  4. 4

    Casting in generic abstract class that implements interface

  5. 5

    Instantiating generic class and implementing generic interface

  6. 6

    The class design - interface or abstract class?

  7. 7

    Generic class with two type constraints, and a interface implementation

  8. 8

    Casting in generic class to interface delphi

  9. 9

    Use a generic class as a custom view in Interface Builder

  10. 10

    Java proper extension design for a class with a generic collection

  11. 11

    Parameterizing a generic interface in class declaration

  12. 12

    Javassist: Create class that implements generic interface

  13. 13

    CLI/C++ Interface class, Generic method calling template method

  14. 14

    Using a interface as a type parameter of generic class with the limitation of "class" in C#

  15. 15

    Abstract class implement a generic Interface Type

  16. 16

    A singleton class to design a generic deck of card?

  17. 17

    Class design:class implementing an Interface implementing another interface

  18. 18

    Modifying value type in generic class implementing generic interface C#

  19. 19

    C# Generics : CS0311 when implementing generic class that meets a generic interface contract

  20. 20

    "Class does not implement interface member" error on class implementing a generic interface

  21. 21

    Generic C# class with limited types to some interface is not implementing other interface with member of the first one

  22. 22

    CLI/C++ Interface class, Generic method calling template method

  23. 23

    C# - Cast generic interface to generic class that pass type <T> that is determined at runtime

  24. 24

    A singleton class to design a generic deck of card?

  25. 25

    Class design:class implementing an Interface implementing another interface

  26. 26

    Non-intrusive design of generic C++ class

  27. 27

    C# generic constraint for base class and interface

  28. 28

    Casting a Generic Class<T> in C# from an Interface?

  29. 29

    Register generic interface with non generic class

HotTag

Archive