Inheritance of Aspects

Technical questions about PostSharp Laos.

Inheritance of Aspects

Postby ddignam on Mon Apr 07, 2008 5:52 pm

Having tried many clumsy ways to achieve what is so painless with PostSharp. I have a question, which probably reveals my lack of knowledge of AOP.

Is there anyway for an aspect attribute to be inherited by SubClasses?

For instance, suppose I have my own Aspect derived from OnFieldAccessAspect and apply it to a class, I would like all subclasses to inherit that Aspect.

Actually, I guess this is hard to achieve in general as the subclasses could be compiled at different times ....

So, if that's the reason it doesn't work, is there a way to specify that the attribute applies to subclasses as well? Something like

[assembly: MyFieldAccess(AttributeTargetAssemblies = "ConsoleApplication2", AttributeTargetTypesAndSubTypes = "ConsoleApplication2.Foo")]

--
Dan
ddignam
 
Posts: 24
Joined: Mon Apr 07, 2008 5:07 pm
First Name: Daniel
Last Name: Dignam
Company: Solidworks

Re: Inheritance of Aspects

Postby gfraiteur on Tue Apr 08, 2008 10:44 am

Hi Dan,

What you want is not possible out of the box.

However, you can achieve the same thing by implementing an assembly-level CompoundAspect that will inspect all base types of types of the current assembly, and add the aspects to the derived types. It is fairly easy (100-200 loc).

Gael
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 839
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org

Re: Inheritance of Aspects

Postby ddignam on Tue Apr 08, 2008 2:21 pm

Thanks for the pointer Gael, I'll look into it.

I'm having a great time using this tool, it's extremely powerful,

Thanks !

--
Dan
ddignam
 
Posts: 24
Joined: Mon Apr 07, 2008 5:07 pm
First Name: Daniel
Last Name: Dignam
Company: Solidworks

Re: Inheritance of Aspects

Postby ddignam on Tue Apr 08, 2008 5:34 pm

gfraiteur wrote:Hi Dan,

What you want is not possible out of the box.

However, you can achieve the same thing by implementing an assembly-level CompoundAspect that will inspect all base types of types of the current assembly, and add the aspects to the derived types. It is fairly easy (100-200 loc).


It was a lot less code for my test example, I now have a reuseable assembly with a base class and subtypes in other assemblies also inherit aspects that the base class wants them to.

:D

--
Dan
ddignam
 
Posts: 24
Joined: Mon Apr 07, 2008 5:07 pm
First Name: Daniel
Last Name: Dignam
Company: Solidworks

Re: Inheritance of Aspects

Postby gfraiteur on Tue Apr 08, 2008 9:22 pm

Congrats!

I did not want to overpromise ;).

Gael
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 839
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org

Re: Inheritance of Aspects

Postby JeffG on Wed Apr 16, 2008 3:07 pm

Hello,

I have managed to do this as well (fairly simply), but I cannot seem to get the aspect to be applied automatically to other assemblies which reference the assembly continaing the base class.

In the Base class assembly, I am attempting to apply the attribute like so:

Code: Select all
[assembly:InheritMyAspect(AttributeTargetAssemblies="*")]


and the aspect is marked up with
Code: Select all
[MulticastAttributeUsage(MulticastTargets.Assembly,
                         AllowExternalAssemblies=true)]
.

To get this to apply to other assembliues which reference the base class assembly, I need to also apply the aspect in that assembly. Is there something I am missing?
I am not experienced with PostSharp at all - I have just been evaluating it for a couple of days, and so far it seems great.
JeffG
 
Posts: 3
Joined: Wed Apr 16, 2008 2:58 pm
First Name: Jeff
Last Name: Goddard
Company: RBI

Re: Inheritance of Aspects

Postby gfraiteur on Wed Apr 16, 2008 3:11 pm

I think it's just because the AttributeTargetAssemblies property does not support wildcards. You need to be explicit.

Gael
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 839
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org

Re: Inheritance of Aspects

Postby JeffG on Wed Apr 16, 2008 3:31 pm

Thanks for the quick answer!

As I don't know the names of the assemblies which will use the base class, I will just have to specify that they will need to include the attribute. That's not a real problemm, but I must admit it would be nicer not to have to.
JeffG
 
Posts: 3
Joined: Wed Apr 16, 2008 2:58 pm
First Name: Jeff
Last Name: Goddard
Company: RBI

Re: Inheritance of Aspects

Postby gfraiteur on Wed Apr 16, 2008 3:37 pm

Well... The reason I did not allow that is performance. You surely dont want to apply the aspect to all assemblies. Do you want to apply to mscorlib?
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 839
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org

Re: Inheritance of Aspects

Postby JeffG on Wed Apr 16, 2008 3:43 pm

gfraiteur wrote:Well... The reason I did not allow that is performance. You surely dont want to apply the aspect to all assemblies. Do you want to apply to mscorlib?


No, not at al, just the ones who reference my assembly.
I'm not complaining, just trying to find the limits of what we can do with PostSharp - of course there are bound to be some; but it just seems so magical at first, that it seems as though it can do anything.
JeffG
 
Posts: 3
Joined: Wed Apr 16, 2008 2:58 pm
First Name: Jeff
Last Name: Goddard
Company: RBI

Re: Inheritance of Aspects

Postby gfraiteur on Wed Apr 16, 2008 3:48 pm

There is a solution to your problem using PostSharp Core.

I will have to implement something in Laos too, but for another major version.

Gael
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 839
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: postsharp.org


Return to PostSharp Laos