Override base class aspect in derived class

Brett Postin's Avatar

Brett Postin

24 Jul, 2013 08:31 AM

I have a generic repository class with various methods marked with a PostSharp aspect (SecuredOperation)...

public class Repository<TEntity> : IRepository<TEntity>, ISecurable
    where TEntity : class, IEntity
    {      
    ...

    [SecuredOperation(DomainAccess.Write)]        
    public virtual void Add(TEntity entity)
    {
        this.Context.AddEntity(entity);
    }

    ...
}

In some of my derived repository classes I want to change the required access on the Add() method (e.g. DomainAccess.None).

My initial attempt was to override the method and reapply the aspect...

public class SomeRepository : Repository<SomeEntity>
{       
    ...

    [SecuredOperation(DomainAccess.None)]
    public override void Add(SomeEntity entity)
    {
        base.Add(entity);
    }

    ...
}

The aspect is correctly applied to this overridden method, however once I call the base class implementation the base class aspect kicks in.

Obviously duplicating the base class logic in the derived classes is not a solution.

How can I get around this problem of overriding a base class aspect in a derived class?

  1. Support Staff 1 Posted by PostSharp Techn... on 24 Jul, 2013 02:11 PM

    PostSharp Technologies's Avatar

    Hello,

    This behavior is by design and is ok for most situations.
    In your case, I suggest you use a ThreadStatic constructs to store the information whether a access verifications should be ignored. The precise type of your ThreadStatic field depends on the level of complexity of your security requirements.

    -gael

  2. System closed this discussion on 13 Apr, 2016 10:33 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac