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 2 Posted by sharpcrafters on 24 Jul, 2013 02:11 PM

    sharpcrafters'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

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

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