• C# .Net AOP 演进历史POP OOP 代码细节篇


    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

    系列 :AOP多种实现方式 七大原则,24种设计模式,aop多种实现方式_云草桑的博客-CSDN博客

    文章目录


    前言

    AOP面向切面编程,是OOP的补充,why

    一、AOP是什么?AOP解决什么问题?

    Aspect Oriented Programming
    AOP 面向切面编程,是 OOP 的补充, why
    写代码怕的不是功能复杂,怕的是变化 ---MySQL 切换到 SQLServer--- 面向对象的
    常见套路: IDBHelper 抽象 --- 实现多个版本 --- 通过工厂 /IOC 去获取实例 ---- 避免上
    层代码的修改 ---But ,这里的 扩展靠的是类的替换,以类为单位,类本身是没有扩
    展的 -- 面向对象里面设计模式 80% 靠的都是这招 ----why ?因为类是 OOP 的最小
    单位,是他的根基,所以不能动!
    有局限! ---Save 的时候写个日志,来个 try-catch---OOP 能做还是类替换 --- 但是如
    果这个扩展功能希望能随便增减,随便排序 ---OOP 就真的搞不定了 --- 原因在于类
    是固定的,是写死的 --- 变态需求 :既不破坏现有封装,又能扩展新功能 答案就
    AOP 面向切面编程

    二、AOP演进历史

    1.AOP历史

    POP OOP
    POP  Procedure Oriented Programming
    POP 面向过程编程,事物比较简单,
    可以用 线性的思维 去解决问题。
    1. ///
    2. /// POP面向过程编程
    3. ///
    4. public class POPShow
    5. {
    6. ///
    7. /// 模拟用户注册,3个步骤
    8. /// 面向过程式编程
    9. /// 线性思维的体现
    10. /// 不能应对变化
    11. ///
    12. public static void Show()
    13. {
    14. Console.WriteLine("用户注册,提交信息");
    15. Console.WriteLine("1 参数检查");
    16. Console.WriteLine("2 数据入库");
    17. Console.WriteLine("3 记录日志");
    18. }
    19. }

    OOP  Object Oriented Programming
    OOP 面向对象编程是一种编程思想,
    是考虑问题的方式
    1. ///
    2. /// OOP面向对象编程演练
    3. ///
    4. public class OOPShow
    5. {
    6. ///
    7. /// OOP面向对象编程
    8. /// 1 考虑有几个对象
    9. /// 2 封装屏蔽细节
    10. /// 3 还可以继承完成代码复用
    11. /// 4 支持多态,尤其是面向抽象编程
    12. ///
    13. /// 内部升级---面向对象的内部还是面向过程的
    14. ///
    15. public static void Show()
    16. {
    17. Console.WriteLine("用户注册,提交信息");
    18. UserInfo userInfo = new UserInfo()
    19. {
    20. Account = "Administrator",
    21. Name = "Eleven",
    22. Password = "888888"
    23. };
    24. //还可以把校验也封装
    25. if (userInfo.Account == null)
    26. {
    27. Console.WriteLine("注册失败");
    28. return;
    29. }
    30. //MySQLDBHelper dbHelper = new MySQLDBHelper();
    31. //封装好了---但是有需求变更,增加日志、异常处理、缓存---只有2种方式:
    32. //1 修改方法---破坏封装,影响稳定性,违背开闭原则
    33. //2 面向抽象---来个新的类实现,然后替换一下,类似于IOC---大功告成---
    34. //但这就是OOP的局限性!只能以类为单位来做拓展,因为类是OOP的组成原子,不容修改--但是扩展需求,就只能抽象+替换类(90%设计模式)----
    35. //有没有这么一个玩法:既不破坏类的封装(也不替换类),但又能扩展功能,有!这个就叫AOP
    36. //AOP是OOP的补充,用来扩展OOP对象通用功能(非业务功能)---
    37. IDBHelper dbHelper = SimpleFactory.CreateDBHelper();//当然也可以IOC
    38. dbHelper.Save(userInfo);
    39. LogHelper.Log("用户注册成功");
    40. }
    41. }
    42. public class SimpleFactory
    43. {
    44. public static IDBHelper CreateDBHelper()
    45. {
    46. //return new MySQLDBHelper();
    47. return new MysqlDBHelperV2();
    48. //return new SqlServerDBHelper();
    49. }
    50. }

    面向切面编程AOP

    1. ///
    2. /// 面向切面编程AOP
    3. ///
    4. public class AOPShow
    5. {
    6. public static void Show()
    7. {
    8. {
    9. Console.WriteLine("用户注册,提交信息");
    10. UserInfo userInfo = new UserInfo()
    11. {
    12. Account = "Administrator",
    13. Name = "清光",
    14. Password = "888888"
    15. };
    16. IDBHelper dBHelper = new DBHelperProxy(); //new MySQLDBHelper();
    17. dBHelper.Save(userInfo);
    18. }
    19. {
    20. //动态代理的
    21. DynamicProxy.Show();
    22. //可以动态的为各种类扩展功能,哇 很强大
    23. //1 更灵活的功能扩展,程序设计聚焦于业务逻辑
    24. //2 代码复用,方便维护和团队管理
    25. //AOP能做什么 AOP不能做什么?
    26. //几乎业务逻辑之外的东西,都可以靠AOP实现
    27. //AOP不能做业务逻辑,不能新增业务逻辑,只能扩展通用逻辑---
    28. }
    29. }
    30. }
    31. #region 静态代理
    32. ///
    33. /// 既没有修改对象 也没有替换对象 但是增加了功能--AOP---静态AOP
    34. /// ----对,我是修改了proxy
    35. ///
    36. public class DBHelperProxy : IDBHelper
    37. {
    38. private IDBHelper _iDBHelper = new MySQLDBHelper();
    39. private ILogHelper helper = new LogConsole();
    40. private void Before()
    41. {
    42. helper.Log("Prepare Save");
    43. }
    44. public int Save(UserInfo userInfo)
    45. {
    46. Before();
    47. int iResult = _iDBHelper.Save(userInfo);
    48. After();
    49. return iResult;
    50. }
    51. private void After()
    52. {
    53. helper.Log("After Save");
    54. }
    55. }
    56. #endregion
    57. #region 动态代理
    58. ///
    59. /// 利用 .NetRemoting动态代理
    60. /// .NET Core已经没有了,可以用DispatchProxy代替
    61. ///
    62. /// 通过动态代理,完成通用功能的扩展
    63. /// 有了这个之后,AOP是不是就可以实现了!
    64. ///
    65. public class DynamicProxy
    66. {
    67. public static void Show()
    68. {
    69. UserInfo userInfo = new UserInfo()
    70. {
    71. Account = "Administrator",
    72. Name = "清光",
    73. Password = "888888"
    74. };
    75. {
    76. IDBHelper dBHelper = new MySQLDBHelper();
    77. dBHelper.Save(userInfo);
    78. }
    79. {
    80. IDBHelper dbHelper = new MySQLDBHelper();
    81. dbHelper = TransparentProxy.Create(dbHelper);
    82. dbHelper.Save(userInfo);
    83. }
    84. }
    85. }
    86. ///
    87. /// 真实代理
    88. ///
    89. ///
    90. public class MyRealProxy<T> : DispatchProxy
    91. {
    92. public T _Instance = default;
    93. //不同的类 不同的方法 增加不同的扩展----类/方法 上面加特性,这里检测特性
    94. protected override object? Invoke(MethodInfo? targetMethod, object?[]? args)
    95. {
    96. //System.Reflection.Emit
    97. //IP检测---HttpContext---权限检测
    98. BeforeProceede(targetMethod);//Log try-catch
    99. Console.WriteLine($"This is {targetMethod?.Name}-Invoked");
    100. if (args != null & args.Length > 0)
    101. Console.WriteLine($"参数信息:{JsonConvert.SerializeObject(args)}");
    102. var result = targetMethod?.Invoke(_Instance, args);
    103. Console.WriteLine("方法自身被调用");
    104. //日志---性能统计---缓存一下--事务/异常处理
    105. AfterProceede(targetMethod);
    106. return result;
    107. }
    108. public void BeforeProceede(MethodInfo? targetMethod)
    109. {
    110. Console.WriteLine("方法执行前可以加入的逻辑");
    111. }
    112. public void AfterProceede(MethodInfo? targetMethod)
    113. {
    114. Console.WriteLine("方法执行后可以加入的逻辑");
    115. }
    116. }
    117. ///
    118. /// 透明代理
    119. ///
    120. public static class TransparentProxy
    121. {
    122. public static T Create<T>(T t)
    123. {
    124. dynamic tProxy = DispatchProxy.Create>()!;
    125. tProxy!._Instance = t;
    126. return tProxy;
    127. }
    128. }
    129. #endregion

    2.AOP 应用

    代码如下(示例):

    1. try
    2. {
    3. Console.WriteLine("AOP代码演进");
    4. {
    5. //Console.WriteLine("*************POPShow**********");
    6. //POPShow.Show();
    7. //Console.WriteLine();
    8. //Console.WriteLine("*************OOPShow**********");
    9. //OOPShow.Show();
    10. //Console.WriteLine();
    11. //Console.WriteLine("*************AOPShow**********");
    12. //AOPShow.Show();
    13. //Console.WriteLine();
    14. }
    15. {
    16. CastleAOPShow.Show();
    17. }
    18. }
    19. catch (Exception)
    20. {
    21. throw;
    22. }


    总结

    AOP 的价值
    AOP 面向切面编程,通过预编译或者运行时动态代理的方式,做到既不破坏现有
    封装,又能扩展新功能,是 OOP 补充
    AOP 能带来什么好处?
    1 方便扩展,可以轻松扩展各种通用功能:日志、安全、缓存、异常处理、参
    数检查 and so on ------Filter
    2 开发时可以聚焦在业务逻辑,然后通过 AOP 的形式扩展通用功能,还能集中
    维护通用功能模块 ------Filter

     以上就是今天要讲的内容,本文仅仅简单介绍了AOP的使用,而AOP提供了大量能使我们快速便捷地处理编程。

  • 相关阅读:
    [论文阅读] SADGA: Structure-Aware Dual Graph Aggregation Network for Text-to-SQL
    DAG(有向无环图)的实现方法
    如何用jxTMS开发一个功能(六)
    SpringBoot使用的时间与空间计量单位
    离线安装wireshark2.6.10
    Python机器学习实战-建立Gradient Boosting模型预测肾脏疾病(附源码和实现效果)
    13SpringMVC中拦截器的配置(拦截规则)和多个拦截器的preHandle,postHandle执行顺序原理详解
    【新知实验室-TRTC开发】实时音视频之欢度世界杯
    数据结构----线性表之队列
    车载软件架构——基础软件供应商&开发工具链(一)
  • 原文地址:https://blog.csdn.net/cao919/article/details/126747021