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 (eg 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?
I'm no PostSharp pro but I think you need to add and set the AttributeReplace value to true on the overridden method. There is also an AllowMultiple
[SecuredOperation(DomainAccess.None, AttributeReplace = true)]
public override void Add(SomeEntity entity)
You can also define the aspect so that it will always override a previous one by adding the following
[MulticastAttributeUsage(MulticastTargets.Method, AllowMultiple = false)]
to the aspect class definition.
PostSharp doc link
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.