2

This might be a stupid question, but here goes.

I have the following problem:

public class MyBaseClass
{
    public void SomethingAwesome()
    {
        //Awesome stuff happens here, but only when the Something() method
        //is called in the other classes.
    }
}

public class SomeClass : MyBaseClass
{
    public void Something()
    {
        //Something happens here
    }
}

public class SomeOtherClass : MyBaseClass
{
    public void Something()
    {
        //Something else happens here
    }
}

MyBaseClass has a method which needs to be called when the Something() method is called in the class that inherits from it.

The idea behind this is that I need to log when this method is called for lots of boring corporate reasons. I would rather have a base class which automatically audits when a method is called, rather than having the developer call the method himself/herself.

Can something like this be achieved? If so, how?

I've considered partial methods, but that would require classes with the same name, which isn't possible in this scenario.

fubo
  • 44,811
  • 17
  • 103
  • 137
Mike Eason
  • 9,525
  • 2
  • 38
  • 63

2 Answers2

6

It sounds like you want the template method pattern:

public abstract class MyBaseClass
{
    public void Something()
    {
        // Code from SomethingAwesome here, or keep SomethingAwesome
        // separate and call it from here
        SomethingImpl();
    }

    protected abstract void SomethingImpl();
}

public class SomeClass : MyBaseClass
{
    protected override SomethingImpl()
    {
        // Something happens here
    }
}

That's assuming you're happy for MyBaseClass to have a public Something method, of course - if it doesn't declare Something (in some way) then the two Something methods in the derived classes are unrelated.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • This is somewhat what I am after, however the abstract SomethingImpl() needs to have an implementation. What I'm after is SomethingImpl() gets called in SomeClass, and called in the base class. – Mike Eason Feb 11 '15 at 09:34
  • @helb: That *will* happen - because the only way of calling the subclass code is via the base class method. – Jon Skeet Feb 11 '15 at 09:34
  • @MikeEason: It doesn't need to have an implementation if it's abstract. I don't really follow your comment, but this really *is* what you want, I'm sure. If you don't want `MyBaseClass` to be abstract, you could make `SomethingImpl` just an empty virtual method instead. – Jon Skeet Feb 11 '15 at 09:35
  • 2
    I think there's a small typo: "SomethingAwful" should perhaps say "SomethingAwesome"? – Matthew Watson Feb 11 '15 at 09:36
  • Yes, you're absolutely right. Thank you very much sir. – Mike Eason Feb 11 '15 at 09:38
1
public class MyBaseClass
{
    public void SomethingAwesome()
    {
         // ...
    }

    public void Something()
    {
         SomethingImpl();
         SomethingAwesome();
    }

    protected abstract void SomethingImpl();
}
Sebastian Negraszus
  • 11,915
  • 7
  • 43
  • 70