项目作者: sorhanselcuk

项目描述 :
Aspect Oriented Programming For C# .Net Core
高级语言: C#
项目地址: git://github.com/sorhanselcuk/Aspect-Oriented-Programming.git


Aspect-Oriented-Programming

Aspect Oriented Programming For C# .Net Core

1- How to create Aspect ?

  1. -> For create aspect, first create a concrete class and this created class attend from the Aspect class.

-> Example

  1. public class LogAspect:Aspect
  2. {
  3. //This codes
  4. }

2- Methods

  1. public class Aspect : Attribute, IAspect
  2. {
  3. public virtual void OnAfterVoid(object arg);
  4. public virtual object OnAfter(object arg);
  5. public virtual void OnBeforeVoid(object[] args);
  6. public virtual object OnBefore(object[] args);
  7. }
  8. void OnAfterVoid(object arg) -> After the method equipped with Aspect is running.
  9. Its parameter is the return value of the function equipped with Aspect.
  10. object OnAfter(object arg); -> The return value replaces the return value of the function with Aspect.
  11. void OnBeforeVoid(object[] args) -> Before the method equipped with Aspect is running.
  12. Its parameters are the input parameters of the corresponding function.
  13. object OnBefore(object[] args) -> The function equipped with Aspect is not executed.
  14. The return value of the corresponding function is the return value from here.

3- Properties

  1. public class Aspect
  2. {
  3. public readonly AspectContext AspectContext;
  4. }
  5. public class AspectContext
  6. {
  7. public string MethodName { get; set; }
  8. public object[] Arguments { get; set; }
  9. }
  10. Class containing the information of the function equipped with Aspect

4- How to use ?

  1. Objects must be run through AspectProxy in order for Aspects to be activated.
  2. The class to initialize must have a constructor without parameters.
  3. public static IT AspectProxy<T>.As<IT>(); -> T must be class and attend from IT. IT must be interface
  4. public static IT AspectProxy<T>.As<IT>(object[] args); -> Function inputs for parameterized constructor classes.
  5. -> Example
  6. public class ProductManager : IProductService
  7. {
  8. public ProductManager()
  9. {
  10. }
  11. public ProductManager(object arg)
  12. {
  13. //Definations
  14. }
  15. [LogAspect]
  16. public void Add(object arg)
  17. {
  18. //Commands
  19. }
  20. }
  21. class Program
  22. {
  23. static void Main(string[] args)
  24. {
  25. IProductService instance = AspectProxy<ProductManager>.As<IProductService>(); // Parameterless Constructor
  26. object[] parameters = new object[]
  27. {
  28. //arguments
  29. }
  30. IProductService instanceWithParameters = AspectProxy<ProductManager>.As<IProductService>(parameters);
  31. }
  32. }