Abstract method in a virtual class

I have a C # class that has many virtual methods, some of these methods are essentially abstract (they are fully implemented in subclasses, and the base class is empty).

To get it for compilation, I throw an InvalidOperationException in the base class with a comment about what needs to be done. It just seems dirty.

Is there a better way to develop my classes?

edit: This is for a mid-tier application that will work in Canada, half of the methods are common, hence virtual. and half of the methods are province specific.

Public class PersonComponent() 
{

 public GetPersonById(Guid id) {
  //Code to get person - same for all provinces
 }

 Public virtual DeletePerson(Guid id) {
  //Common code
 }

 Public virtual UpdatePerson(Person p) {
  throw new InvalidOperation("I wanna be abstract");
 }

Public Class ABPersonComponent : PersonComponent
{
    public override DeletePerson(Guid id) 
    {
       //alberta specific delete code
    }

    public override UpdatePerson(Person p) 
    {
       //alberta specific update codecode
    }

}

hope it makes sense

+4
source share
2 answers

. , .

. / . "Core".

:

public abstract class BaseClass
{
    protected virtual void DeletePersonCore(Guid id)
    {
        //shared code
    }

    public void DeletePerson(Guid id)
    {
        //chain it to the core
        DeletePersonCore(id);
    }
}

public class DerivedClass : BaseClass
{
    protected override void DeletePersonCore(Guid id)
    {
        //do some polymorphistic stuff

        base.DeletePersonCore(id);
    }
}

public class UsageClass
{
    public void Delete()
    {
        DerivedClass dc = new DerivedClass();

        dc.DeletePerson(Guid.NewGuid());
    }
}
+1

, , .

public abstract class BaseClass
{

    public abstract void AbstractMethod();
}

public class SubClass: BaseClass
{
    public override void AbstractMethod()
    {
        //Do Something
    }
}

. , . . , ?

: , , PersonComponent UpdatePerson. , UpdatePerson PersonComponent, , UpdatePerson PersonComponent.

+15

All Articles