Skip to content

AOP (Aspect Oriented Programming) using dotnet source generator

License

Notifications You must be signed in to change notification settings

Teleopti/Saspect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Saspect

AOP (Aspect Oriented Programming) using dotnet source generator.

Including extensions for Autofac, but any implementation that calls the generated code would do.

Usage example, logging all calls to console:

public class MyService
{
[Log]
public virtual void MyMethod()
{
}
}
public class LogAttribute() : AspectAttribute(typeof(LogAspect));
public class LogAspect : IAspect
{
public void OnBeforeInvocation(InvocationInfo invocation)
{
Console.WriteLine($"{invocation.Method.Name} is invoked");
}
public void OnAfterInvocation(Exception exception, InvocationInfo invocation)
{
Console.WriteLine($"{invocation.Method.Name} exited");
}
}
public class SampleTest
{
[Test]
public void ShouldLogMethodCallToConsole()
{
var builder = new ContainerBuilder();
builder.RegisterType<AspectInterceptor>();
builder.RegisterType<LogAspect>().As<IAspect>();
builder.RegisterType<MyService>().ApplyAspects();
var container = builder.Build();
var target = container.Resolve<MyService>();
target.MyMethod();
}
}

About

AOP (Aspect Oriented Programming) using dotnet source generator

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages