SharpAspect 0.1.3
See the version list below for details.
dotnet add package SharpAspect --version 0.1.3
NuGet\Install-Package SharpAspect -Version 0.1.3
<PackageReference Include="SharpAspect" Version="0.1.3" />
paket add SharpAspect --version 0.1.3
#r "nuget: SharpAspect, 0.1.3"
// Install SharpAspect as a Cake Addin
#addin nuget:?package=SharpAspect&version=0.1.3
// Install SharpAspect as a Cake Tool
#tool nuget:?package=SharpAspect&version=0.1.3
Getting Started
<a href="https://www.nuget.org/packages/SharpAspect/"> <img alt="Nuget (with prereleases)" src="https://img.shields.io/nuget/vpre/SharpAspect?label=SharpAspect%20%7C%20NuGet">
</a>
<a href="https://www.nuget.org/packages/SharpAspect/"> <img src="https://img.shields.io/nuget/dt/SharpAspect"> </a>
dotnet add package SharpAspect
Defining Interceptors
All attributes must derive from MethodInterceptionAttribute class.
public class LogAttribute: MethodInterceptorAttribute
{
}
public class CacheAttribute: MethodInterceptorAttribute
{
}
<br>
All interceptors also must implement the IMethodInterceptor interface.
public class LogInterceptor : IMethodInterceptor
{
public void AfterInvoke(IInvocation invocation)
{
// throw new System.NotImplementedException();
}
public void BeforeInvoke(IInvocation invocation)
{
System.Console.WriteLine($"[Logging] {invocation.Method.DeclaringType.FullName}.{invocation.Method.Name}");
}
public void OnError(IInvocation invocation, System.Exception e)
{
throw new System.NotImplementedException();
// System.Console.WriteLine(e.Message);
}
}
public class CacheInterceptor : IMethodInterceptor
{
public void AfterInvoke(IInvocation invocation)
{
// throw new System.NotImplementedException();
}
public void BeforeInvoke(IInvocation invocation)
{
System.Console.WriteLine($"[Caching] {invocation.Method.DeclaringType.FullName}.{invocation.Method.Name}");
}
public void OnError(IInvocation invocation, System.Exception e)
{
throw new System.NotImplementedException();
}
}
Registering your interceptors
private static IServiceProvider ConfigureServices()
{
return new ServiceCollection()
.ConfigureDynamicProxy(c =>
{
// Attribute & Interceptor mapping
c.AddInterceptor<CacheAttribute, CacheInterceptor>();
c.AddInterceptor<LogAttribute, LogInterceptor>();
})
// Transient service
.AddTransientProxy<IRocket, Rocket>()
.BuildServiceProvider();
}
public interface IRocket
{
void Launch();
string SetRoute(string route);
}
public class Rocket: IRocket
{
[Log]
public void Launch()
{
System.Console.WriteLine("Launching rocket in 3...2.....1 🚀");
}
[Log]
[Cache]
public string SetRoute(string route)
{
System.Console.WriteLine($"Route: {route}");
return route;
}
}
static void Main(string[] args)
{
var services = ConfigureServices();
var rocket = services.GetRequiredService<IRocket>();
rocket.SetRoute("Moon");
rocket.Launch();
}
Sample Output
[Logging] SharpAspect.Sample.IRocket.SetRoute
[Caching] SharpAspect.Sample.IRocket.SetRoute
Route: Moon
[Logging] SharpAspect.Sample.IRocket.Launch
Launching rocket in 3...2.....1 🚀
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETStandard 2.0
- Castle.Core (>= 4.4.1)
- Castle.Core.AsyncInterceptor (>= 1.7.0)
- Microsoft.Extensions.DependencyInjection (>= 3.1.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
A changelog is available at https://github.com/fasetto/SharpAspect/blob/master/CHANGELOG.md.