1

I was curious if it was possible to create like a parent abstract class that I can define a specific set of methods in but have the children classes include different entity types? Code Example:

public abstract class BaseService
{
    public abstract void Add();

    public abstract void Delete();

    public abstract void Update();

    public abstract void Get();        
}

Maybe be able to do something like public abstract List<'random type'> GetAll();

But here i would want to override each method with specific parameters that are specific to each of its children:

public class CategoryService : BaseService
{
    public override void Add(){ }

    public override void Delete(){ }

    public override void Update(){ }

    public override void Get(){ }
}

However, in my child class, I would want my Get() method to return a specific List<"of Type"> (in this case Category). Furthermore, I might want to do public override Add(int CategoryID) instead of the inherited Add from BaseService.

Is this possible? Thoughts? Am I just crazy? Or am I trying to make this more complicated than it needs to be? I have about 10 different service types that I want to make sure get those generic methods from BaseService.

Thanks in advance!

moyeradon
  • 443
  • 5
  • 13
  • 1
    If you were going to have different parameters for `Add` in all the sub-classes, then what would you be gaining by having these defined in a base class? – Charles Mager May 27 '16 at 12:29
  • Possible duplicate of [Using generics in abstract classes](http://stackoverflow.com/questions/2359540/using-generics-in-abstract-classes) – pw94 May 27 '16 at 12:32
  • `I might want to do public override Add(int CategoryID) instead of the inherited Add from BaseService.` Then why would you want to use `override` keyword at all? – Pawan Nogariya May 27 '16 at 12:35

3 Answers3

4

You could do this if I understand you correctly:

public abstract class BaseService<T>
{
    public abstract T Get();        
}

Then:

public class CategoryService : BaseService<Category>
{
    public override Category Get(){ ... }
}

You can't override methods and have a different signature in the override, but you could use a dictionary/hashtable or something more fancy to pass parameters into the Add method. Passing parameters in using a generic container would mean you are starting to use the query pattern where the parameters in the container determine the query (just for info :-)).

keith
  • 5,122
  • 3
  • 21
  • 50
1

Try using generic type

public abstract List<T> GetAll<T>();

And return appropriate type in your child class

Update

@pw94 answer is also a good way to achieve this, but the only problem is you cannot have multiple type for different methods, only one type will work once you inherit the class.

Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105
0

You can create abstract generic class:

public abstract class BaseService<T>
{
    public abstract void Add();

    public abstract List<T> Get();        
}

And implement:

public class CategoryService : BaseService<int>
{
    public override void Add(){ ... }

    public override List<int> Get(){ ... }
}
pw94
  • 391
  • 2
  • 5
  • 21