0
votes

I'm using PostSharp to apply a CompoundAspect to a ActiveRecord class (from CastleProject). The code looks like this:

public override void ProvideAspects(object targetElement, LaosReflectionAspectCollection collection)
{
    Type targetType = (Type)targetElement;
    RevertibleSubAspect revertible = new RevertibleSubAspect();
    revertible.Cascade = this.Cascade;
    collection.AddAspect(targetType, revertible);

    //This isn't working
    MethodInfo saveMethod = targetType.GetMethod("Save");
    collection.AddAspect(saveMethod, new CommitOnSaveSubAspect());

    foreach (PropertyInfo property in targetType.GetProperties())
    {
        if((this.Only != null && this.Only.IndexOf(property.Name) == -1) ||
           (this.Except != null && this.Except.IndexOf(property.Name) > -1))
        {
            continue;
        }

        if (property.DeclaringType == targetType && property.CanWrite)
        {
            MethodInfo method = property.GetSetMethod();
            if (method != null && !method.IsStatic)
            {
                collection.AddAspect(method, new TrackInitialPropertyValuesSubAspect());
            }
        }
    }
}

Everything works fine, except the CommitOnSaveSubAspect which is a OnMethodBoundaryAspect. The OnSuccess method never gets called when the Save method is invoked. I have already tried moving the code to OnEntry and OnExit but same situation here.

The CommitOnSaveSubAspect class looks like this:

[Serializable]
class CommitOnSaveSubAspect : OnMethodBoundaryAspect
{
    public override void OnSuccess(MethodExecutionEventArgs eventArgs)
    {
        ((IRevertible)eventArgs.Instance).Commit();
    }
}

Am I applying the aspect the wrong way?

2
@Mato: Just checking that !method.IsStatic and your other if conditions. Are you sure the aspect is assigned and is not held back by these conditional statements? - David Andres
Why not just use the regular interception mechanisms of NHibernate/ActiveRecord? (override OnSave() or FindDirty(), IInterceptor, event listeners) - Mauricio Scheffer
@David Andres: The CommitOnSaveSubAspect is applied outside the foreach loop, so the conditional statements don't affect this. @Mauricio Scheffer: I don't want to spread the code on to many classes because the reusability is better this way. Another reason for not doing this is that in my opinion this wouldn't be propper AOP. - Mato

2 Answers

2
votes

A good way to debug an aspect is to look at the resulting assembly using Reflector. Are methods enhanced as you expect?

You can also debug the ProvideAspects method by putting a breakpoint into it and running msbuild with the following command line:

msbuild /p:PostSharpAttachDebugger=true
0
votes

Was PostSharp defined globally when you installed it? Otherwise, you'll have to edit your project files in order for PostSharp to be injected properly into your assemblies.

See http://doc.postsharp.org/1.0/Default.aspx##PostSharp.HxS/UserGuide/Platform/EnablingPostSharp.html.