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

Recent Discussions

24 Nov, 2014 05:03 PM
24 Nov, 2014 02:36 PM
24 Nov, 2014 06:12 AM
21 Nov, 2014 06:42 PM
20 Nov, 2014 11:45 AM

 

19 Nov, 2014 06:42 PM
19 Nov, 2014 01:01 PM
19 Nov, 2014 12:58 PM
19 Nov, 2014 10:19 AM
18 Nov, 2014 10:17 PM
17 Nov, 2014 10:35 AM