A Fody weaver which allows to decorate methods and hook into method start, method end and method exceptions. Additionally you have access to useful method parameters.
You can easily write your own aspects for
- transaction handling
- logging
- measuring method execution time
- exception wrapping
- displaying wait cursor
- and much more ...
- Hook into method start and end
- Hook into raised exceptions in a method
- Access method information like
- applied object instance
- the method itself (System.Reflection.MethodBase)
- the passed method arguments
- the thrown exception
- some custom object, which can be set at the start of the method and accessed at the end (e.g. useful for transactions or timers)
- Apply aspects at different levels
- globally in
AssemblyInfo.cs
- on class
- on method
- globally in
Feel free to make a Fork of this repository.
- Install the MethodBoundaryAspect.Fody NuGet package (
Install-Package MethodBoundaryAspect.Fody
) - Create
FodyWeavers.xml
in your project and add the WeaverMethodBoundaryAspect
to it (further details) - Write your custom aspects by deriving from
OnMethodBoundaryAspect
and decorate your methods (see sample below)
Short sample how a transaction aspect could be implemented.
public sealed class TransactionScopeAttribute : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
args.MethodExecutionTag = new TransactionScope();
}
public override void OnExit(MethodExecutionArgs args)
{
var transactionScope = (TransactionScope)args.MethodExecutionTag;
transactionScope.Complete();
transactionScope.Dispose();
}
public override void OnException(MethodExecutionArgs args)
{
var transactionScope = (TransactionScope)args.MethodExecutionTag;
transactionScope.Dispose();
}
}
public class Sample
{
[TransactionScope]
public void Method()
{
Debug.WriteLine("Do some database stuff isolated in surrounding transaction");
}
}