0

I want to create many Service classes that use a common set of functions. All these classes will inherit ClientBase class which is provided by Microsoft. Currently I have written those common functionalities as private methods in class. What is the best way to do it to avoid code repetition and efficiency. I cannot add those common methods to base class as it cannot be modified.

Madhur Maurya
  • 1,016
  • 4
  • 19
  • 42

2 Answers2

3

Have your own abstract base class that inherits ClientBase<T>, call it MyClientBase<T>, move the common methods to it and make them protected so that successors could access them. Then make your service classes inherit MyClientBase<T>.

Alexander Kravets
  • 4,245
  • 1
  • 16
  • 15
  • I am implementing it like `public abstract class WingsClientBase:ClientBase where T :class` Is it acceptable, since ClientBase take an interface and I am writing `where T:class` in definition. I get compiler error **Compiler Error CS0452 - MSDN - Microsoft** without writing `where T:class` – Madhur Maurya May 09 '16 at 06:11
  • 1
    This error occurs when you pass a value type such as a struct or int as a parameter to a generic type or method that has a reference type constraint. Get rid of `where T:class` constraint or don't use it with value types. – Alexander Kravets May 09 '16 at 11:45
0

If all other classes are directly inheriting from ClientBase<T>I would try to define the additional functionalities via extension methods of ClientBase<T>.

Moerwald
  • 10,448
  • 9
  • 43
  • 83