Search code examples
c#ninjectaopinterceptor

How to create an abstract class, then intercept it's descendant method


I'm trying to separate "contracts" from "implementation". Security features are "contracts" too (apart of implementation of how exactly those features work).

So I'm trying to write something like this:

public abstract class SomeServiceContract
{
    [Authorize]
    public abstract void SomeMethod();
}

public class SomeServiceImpl : SomeServiceContract
{
    public override void SomeMethod()
    {
        // this can just be implemented, no need to worry about access rigts 
    }
}

The only purpose of the Authorize attribute (which is inherited) is to be checked in an interceptor. But to be "interceptable", a method must be virtual, or it can't be wrapped by Ninject, for example. And I can't make an overridden method to be virtual.

How else can I implement this?


Solution

  • This method is virtual by default.

    The restriction is only if it is marked with "sealed".

    If you override method from abstract class - the method is virtual. If from interface - the method is "virtual sealed", unless you marked it as "virtual"