-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
83 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2018 VESCON GmbH | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,80 @@ | ||
# MethodBoundaryAspect.Fody | ||
|
||
[![Build status](https://img.shields.io/appveyor/ci/marcells/methodboundaryaspect-fody.svg)](https://ci.appveyor.com/project/marcells/methodboundaryaspect-fody) | ||
[![Tests](https://img.shields.io/appveyor/tests/marcells/methodboundaryaspect-fody.svg)](https://ci.appveyor.com/project/marcells/methodboundaryaspect-fody/build/tests) | ||
[![NuGet](https://img.shields.io/nuget/v/MethodBoundaryAspect.Fody.svg)](https://www.nuget.org/packages/MethodBoundaryAspect.Fody/) | ||
|
||
NuGet package https://www.nuget.org/packages/MethodBoundaryAspect.Fody/ | ||
## MethodBoundaryAspect.Fody | ||
> A [Fody weaver](https://github.com/Fody/Fody) whichs injects custom code into the decorated method and provides information about the method parameters. | ||
## Introduction | ||
Allows decorated method to access some runtime properties before and after method execution. | ||
```csharp | ||
public class MethodExecutionArgs | ||
{ | ||
public object Instance { get; set; } | ||
public MethodBase Method { get; set; } | ||
public object[] Arguments { get; set; } | ||
public object ReturnValue { get; set; } | ||
public Exception Exception { get; set; } | ||
public object MethodExecutionTag { get; set; } | ||
} | ||
``` | ||
### Your Code | ||
You can easily write your own aspects for | ||
- transaction handling | ||
- logging | ||
- measuring method execution time | ||
- exception wrapping | ||
- displaying wait cursor | ||
- and much more ... | ||
|
||
```csharp | ||
public sealed class TransactionScopeAttribute : OnMethodBoundaryAspect | ||
{ | ||
public int TimeoutInSeconds { get; set; } | ||
### Supported features | ||
- 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](https://msdn.microsoft.com/en-us/library/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 | ||
|
||
public override void OnEntry(MethodExecutionArgs args) | ||
{ | ||
var transactionScopeProvider = GlobalServiceLocator.ServiceLocator.GetInstance<ITransactionScopeProvider>(); | ||
Feel free to make a [Fork](https://github.com/vescon/MethodBoundaryAspect.Fody/fork) of this repository. | ||
|
||
if (TimeoutInSeconds != 0) | ||
transactionScopeProvider.Timeout = TimeSpan.FromSeconds(TimeoutInSeconds); | ||
### Quickstart | ||
|
||
var transactionScope = transactionScopeProvider.Create(); | ||
args.MethodExecutionTag = transactionScope; | ||
} | ||
1. Install the MethodBoundaryAspect.Fody NuGet package (`Install-Package MethodBoundaryAspect.Fody`) | ||
2. Create `FodyWeavers.xml` in your project and add the Weaver `MethodBoundaryAspect` to it ([further details](https://github.com/Fody/Fody)) | ||
3. Write your custom aspects by deriving from `OnMethodBoundaryAspect` and decorate your methods (see sample below) | ||
|
||
public override void OnException(MethodExecutionArgs args) | ||
{ | ||
FinishTransaction(args); | ||
} | ||
### Sample | ||
|
||
public override void OnExit(MethodExecutionArgs args) | ||
{ | ||
FinishTransaction(args); | ||
} | ||
Short sample how a transaction aspect could be implemented. | ||
|
||
private static void FinishTransaction(MethodExecutionArgs args) | ||
{ | ||
var transactionScope = (ITransactionScope)args.MethodExecutionTag; | ||
#### The aspect code | ||
|
||
try | ||
{ | ||
if (args.Exception == null) | ||
transactionScope.Complete(); | ||
} | ||
finally | ||
{ | ||
if (transactionScope != null) | ||
transactionScope.Dispose(); | ||
} | ||
} | ||
```csharp | ||
public sealed class TransactionScopeAttribute : OnMethodBoundaryAspect | ||
{ | ||
public override void OnEntry(MethodExecutionArgs args) | ||
{ | ||
args.MethodExecutionTag = new TransactionScope(); | ||
} | ||
|
||
public class Sample | ||
{ | ||
[TransactionScope] | ||
public void Method() | ||
{ | ||
Debug.WriteLine("Do some database stuff isolated in surrounding transaction"); | ||
} | ||
} | ||
``` | ||
|
||
License | ||
------- | ||
|
||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 VESCON GmbH | ||
public override void OnExit(MethodExecutionArgs args) | ||
{ | ||
var transactionScope = (TransactionScope)args.MethodExecutionTag; | ||
|
||
transactionScope.Complete(); | ||
transactionScope.Dispose(); | ||
} | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
public override void OnException(MethodExecutionArgs args) | ||
{ | ||
var transactionScope = (TransactionScope)args.MethodExecutionTag; | ||
|
||
transactionScope.Dispose(); | ||
} | ||
} | ||
``` | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
#### The applied aspect | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
```csharp | ||
public class Sample | ||
{ | ||
[TransactionScope] | ||
public void Method() | ||
{ | ||
Debug.WriteLine("Do some database stuff isolated in surrounding transaction"); | ||
} | ||
} | ||
``` |