NConcern .NET AOP Framework
NConcern is a .NET runtime AOP (Aspect-Oriented Programming) lightweight framework written in C# that reduces tangling caused by cross-cutting concerns. Its role is to introduce Aspect-Oriented Programming paradigm with a minimum cost to maximize quality and productivity.
NConcern AOP Framework is based on code injection at runtime.
An aspect represents a set of features related to a specific concern. The role of an aspect is to provide advices for method. It can be added and removed at runtime.
An advice is the code added by an aspect to complete a method. It can be implemented using delegate, expression or CIL generation.
I want to trace my service calls with "Console.WriteLine".
My services are OperationContract typed (WCF service)
[ServiceContract]
public class Calculator
{
[OperationContract]
public int Add(int a, int b)
{
return a + b;
}
}
PM> Install-Package CNeptune
static public class Tracer
{
static public void Trace(MethodBase method, object[] arguments)
{
Console.WriteLine("{0}({1})", method.Name, string.Join(", ", arguments));
}
}
PM> Install-Package NConcern
public class Logging : IAspect
{
public IEnumerable<IAdvice> Advise(MethodBase method)
{
yield return Advice.Basic.Before((instance, arguments) =>
{
Tracer.Trace(method, arguments);
});
}
}
Aspect.Weave<Logging>(typeof(OperationContractAttribute));
Aspect.Release<Logging>(typeof(OperationContractAttribute));
How this AOP Framework is different from the others? Most of time developping cross-cutting source code required reflection and boxing to be done. NConcern offer a way to define it using Linq Expressions or ILGenerator because cross-cutting source code have to manage not statically known datas. No need factory and no need base class is the second exclusive feature that make the difference because interception is not based on method overriding, MarshalByRef nor ContextBoundObject.
How fast is "low performance overhead"? There is no perceptible overhead when Linq Expressions or ILGenerator are used. Basic advice introduce a light overhead caused by boxing and arguments array creation. However, MethodInfo is not prepared if capture is not required in lambda expression.
Why I have to use CNeptune? _Interception is based on CNeptune. Indeed CNeptune add a build action to rewrite CIL to make assembly "Architect Friendly" by injecting transparents and hidden features to to grant full execution control at runtime.
Can I add multiple aspect for same target? If yes how can I control priority? Yes you can. Priority is defined by the order of weaving. It can be reorganized by calling Aspect.Release(...)/Aspect.Weave(...) and you can check the whole aspects mapping by calling Aspect.Lookup(...).
Is an attribute required to identify a mehod to weave? No you can identify a method by the way you want. There is a Aspect.Weave(...) overload that take a Func<MethodInfo, bool> to select methods.
Can I intercept constructor? If yes, how do I implement it? Constructor interception is supported and is treated like another method with declaring type as first argument and void for return type.