项目作者: powerumc

项目描述 :
SimpleAop is library for your AOP or dynamic proxy programming.
高级语言: C#
项目地址: git://github.com/powerumc/SimpleAop.Net.git
创建时间: 2018-07-16T02:48:17Z
项目社区:https://github.com/powerumc/SimpleAop.Net

开源协议:MIT License

下载


SimpleAop

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 includes IServiceCollection 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.
  1. dotnet add package SimpleAop
  1. dotnet add package SimpleAop.Extensions.DependencyInjection

Dynamic Proxy Programming

It’s simple. We just need Interface declaring and Implementation class.

  1. public interface IPrint
  2. {
  3. void PrintMessage(string message);
  4. }
  5. public class Print : IPrint
  6. {
  7. public void PrintMessage(string message)
  8. {
  9. Console.WriteLine(message);
  10. }
  11. }

And to use it,

  1. var type = DynamicProxyFactory.Create<IPrint, Print>();
  2. var obj = (IPrint) Activator.CreateInstance(type);
  3. obj.PrintMessage("Hello World");

The type generate random character from the Guid. It’s just first SimpleAop version.

Aop Programming

Additionally, provider OnMethodBoundAspect attribute class. We just inherites it.

  1. public class LoggingAspectAttribute : OnMethodBoundAspectAttribute
  2. {
  3. public override void OnBefore(IAspectInvocation invocation)
  4. {
  5. Console.WriteLine($"--- Before: {invocation.Method}, {invocation.Object}, {string.Join(",", invocation.Parameters)} ---");
  6. }
  7. public override void OnAfter(IAspectInvocation invocation)
  8. {
  9. Console.WriteLine("--- After ---");
  10. }
  11. }

And add logging attribute,

  1. [LoggingAspect]
  2. public class Print : IPrint
  3. {
  4. public void PrintMessage(string message)
  5. {
  6. Console.WriteLine(message);
  7. }
  8. }

So result is,

  1. --- Before: Void PrintMessage(System.String), 9a19fdd7e64943c9b22ae2c79a886b50, Hello World ---
  2. Hello World
  3. --- After ---

ASP.NET Core or .NET Core

Provide some methods is,

  • AddWithProxy
  • AddTransientWithProxy
  • AddScopedWithProxy
  • AddSingletonWithProxy

In Startup.cs file,

  1. services.AddSingletonWithProxy<ITestService, TestService>();

I did make SimpleAop.Sample.AspNetCoreWeb project.

Let’s run it, and watch the console log.

Have a issue?

https://github.com/powerumc/SimpleAop/issues