Can you apply aspects in PostSharp without using attributes?
Asked Answered
F

3

7

I know with Castle Windsor, you can register aspects (when using method interception in Windsor as AOP) using code instead of applying attributes to classes. Is the same possible in Postsharp? It's a preference things, but prefer to have aspects matched to interfaces/objects in one place, as opposed to attributes all over.

Update: Curious if I can assign aspects to interfaces/objects similiar to this:

container.Register(
        Component
        .For<IService>()
        .ImplementedBy<Service>()
        .Interceptors(InterceptorReference.ForType<LoggingAspect>()).Anywhere
   );

If you could do this, you would have the option of NOT having to place attributes on assemblies/class/methods to apply aspects. I can then have one code file/class that contains which aspects are applied to which class/methods/etc.

Farnesol answered 17/10, 2011 at 15:34 Comment(0)
C
3

Yes. You can either use multicasting (http://www.sharpcrafters.com/blog/post/Day-2-Applying-Aspects-with-Multicasting-Part-1.aspx , http://www.sharpcrafters.com/blog/post/Day-3-Applying-Aspects-with-Multicasting-Part-2.aspx) or you can use aspect providers (http://www.sharpcrafters.com/blog/post/PostSharp-Principals-Day-12-e28093-Aspect-Providers-e28093-Part-1.aspx , http://www.sharpcrafters.com/blog/post/PostSharp-Principals-Day-13-e28093-Aspect-Providers-e28093-Part-2.aspx).

Example:

    using System;
    using PostSharp.Aspects;
    using PostSharp.Extensibility;

    [assembly: PostSharpInterfaceTest.MyAspect(AttributeTargetTypes = "PostSharpInterfaceTest.Interface1", AttributeInheritance = MulticastInheritance.Multicast)]

    namespace PostSharpInterfaceTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Example e = new Example();
            Example2 e2 = new Example2();
            e.DoSomething();
            e2.DoSomething();
            Console.ReadKey();
        }
    }

    class Example : Interface1
    {

        public void DoSomething()
        {
            Console.WriteLine("Doing something");
        }
    }

    class Example2 : Interface1
    {

        public void DoSomething()
        {
            Console.WriteLine("Doing something else");
        }
    }

    interface Interface1
    {
        void DoSomething();
    }

    [Serializable]
    class MyAspect : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionArgs args)
        {
            Console.WriteLine("Entered " + args.Method.Name);
        }
    }
}

I recommend that if you have complex requirements for determining which types get certain aspects that you consider creating an aspect provider instead.

Cordelier answered 17/10, 2011 at 17:21 Comment(5)
These articles still seem to apply an attribute to the class. I was looking along the lines of how Castle Does Dynamic injection: blog.andreloker.de/post/2009/02/20/…. I'm just curious if you can do this with PostSharp. Since PostSharp weaves their aspects as compile time, can understand if it doesn't.Farnesol
What are you looking to achieve? Your question states "It's a preference things, but prefer to have aspects matched to interfaces/objects in one place, as opposed to attributes all over." which is why I gave the answers I did. Now it sounds like you want to enable or disable aspects at runtime. What does it matter if aspects are applied using attributes? Do you have concerns with runtime vs compile time?Cordelier
My answer is still the same. I've updated to give you an example of what to do.Cordelier
@Bless there isn't a fuent interface to apply aspcets, but you could certainly build one yourself quite easily and then using an aspect provider, you could have the aspects applied based on the what was configured. You can keep all multicasting declarations in a single file such as "AspectApplication.cs"Cordelier
Okay. Guess I need to give your series a read. Thanks for your patience.Farnesol
S
1

Have a look at LOOM.NET, there you have a post compiler and a runtime weaver. With the later one you are able to archive exactly what you want.

Suffer answered 10/6, 2012 at 7:25 Comment(0)
E
0

It should be possible to use the PostSharp XML configuration. The XML configuration is the unification of the Plug-in and Project models in the project loader.

Description of .psproj could be found at http://www.sharpcrafters.com/blog/post/Configuring-PostSharp-Diagnostics-Toolkits.aspx.

Note, that I've only seen examples how PostSharp Toolkits use this XML configuration. But it should work for custom aspects the same way.

Warning: I've noticed that installation of a PostSharp Toolkit from Nuget overwrites existing psproj file. So do not forget to back up it.

Expansile answered 13/12, 2012 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.