Home

joelowrance edited this page May 6, 2011 · 9 revisions

A .NET library to make aspect-oriented programming a Snap!

Snap (Simple .NET Aspect-Oriented Programming) was conceived out of a recurring need for flexible AOP in the wake of PostSharp’s metamorphosis into a commercial product.

Not to be mistaken with a code weaver (i.e. a compile-time tool), Snap is a fully run-time executed library. Snap aims to pair with your Inversion of Control (IoC) container of choice to deliver a powerful aspect-oriented programming model.

What IoC tools does Snap work with?

  • StructureMap 2.6.x
  • Ninject 2.x
  • Autofac 2.x
  • LinFu 2.x
  • Castle MicroKernel 2.1.x
  • Castle Windsor 2.1.
  • Please help us fill this list in with more IoC tools!

What dependencies does Snap have?
Snap uses Castle DynamicProxy2 to create run-time wrappers (proxies) that handle type interception.

Have you considered using LinFu for dynamic proxies instead of Castle?
Just learned that LinFu has a dynamic proxy alternative to Castle. Anyone want to help explore this option?

Can I join the Snap team?
Yes! Help is welcome! All you need to know is C# and Git.

Getting started with Snap
Getting started with aspect-oriented programming is as easy as:

//Configure Snap to use StructureMap
    SnapConfiguration.For<StructureMapAspectContainer>(c =>
        {
            // Tell Snap to intercept types under the "Snap.Tests..." namespace.
            c.IncludeNamespaceRoot("Snap.Tests");
            // Register a custom interceptor (a.k.a. an aspect).
            c.Bind<HandleErrorInterceptor>().To<HandleErrorAttribute(); 
        });
    // Configure your own types with StructureMap.
    ObjectFactory.Configure(c => c.For<IMyType>().Use<MyType>());
    // Get an AoP wrapped instance of your type from StructureMap
    var badCode = ObjectFactory.GetInstance<IMyType>();

An example of your “MyType” class with a custom aspect looks like this:

public class MyType : IMyType{
    [HandleError]
    public void InterceptedMethod(){
        // Elided
    }
}

That’s all there is to is. Aspect-Oriented programming in a Snap!