Bless Yahu
Bless Yahu

Reputation: 1341

Can you apply aspects in PostSharp without using attributes?

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.

Upvotes: 7

Views: 2095

Answers (3)

Michael Freidgeim
Michael Freidgeim

Reputation: 28435

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.

Upvotes: 0

Wolle
Wolle

Reputation: 11

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.

Upvotes: 1

Dustin Davis
Dustin Davis

Reputation: 14585

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.

Upvotes: 3

Related Questions