SimpleAop is library for your Aop or Dynamic Proxy programming. I converted it more simply from the old framework.
Here includes following libraries.
-
SimpleAop
It needs Aop or Dynamic Proxy programming. -
SimpleAop.Extensions.DependencyInjection
Here includesIServiceCollection
extension class for .NET Core or ASP.NET Core.
To install, run following command or Nuget browser of visual studio.
Functionally list.
- Dynamic Proxy
- Aop of methods and classes.
- General methods.
- yield return
- async/await
- Generate constructors.
dotnet add package SimpleAop
dotnet add package SimpleAop.Extensions.DependencyInjection
It's simple. We just need Interface declaring and Implementation class.
public interface IPrint
{
void PrintMessage(string message);
}
public class Print : IPrint
{
public void PrintMessage(string message)
{
Console.WriteLine(message);
}
}
And to use it,
var type = DynamicProxyFactory.Create<IPrint, Print>();
var obj = (IPrint) Activator.CreateInstance(type);
obj.PrintMessage("Hello World");
The type
generate random character from the Guid
. It's just first SimpleAop version.
Additionally, provider OnMethodBoundAspect
attribute class. We just inherites it.
public class LoggingAspectAttribute : OnMethodBoundAspectAttribute
{
public override void OnBefore(IAspectInvocation invocation)
{
Console.WriteLine($"--- Before: {invocation.Method}, {invocation.Object}, {string.Join(",", invocation.Parameters)} ---");
}
public override void OnAfter(IAspectInvocation invocation)
{
Console.WriteLine("--- After ---");
}
}
And add logging attribute,
[LoggingAspect]
public class Print : IPrint
{
public void PrintMessage(string message)
{
Console.WriteLine(message);
}
}
So result is,
--- Before: Void PrintMessage(System.String), 9a19fdd7e64943c9b22ae2c79a886b50, Hello World ---
Hello World
--- After ---
Provide some methods is,
- AddWithProxy
- AddTransientWithProxy
- AddScopedWithProxy
- AddSingletonWithProxy
In Startup.cs
file,
services.AddSingletonWithProxy<ITestService, TestService>();
I did make SimpleAop.Sample.AspNetCoreWeb
project.
Let's run it, and watch the console log.