Simulating AOP injection with domain events

Jiedao jdon 2021-05-04 14:52:46
simulating aop injection domain events

Welcome to watch KylinORM Series articles :

Chapter one : Use Tag Network maintains entity relationship

Second articles : Based on business driven domain services

Third articles : Use domain event simulation AOP Inject

Use AOP Weaving domain objects makes it easy to cascade load 、 data verification 、 cache 、 Log and other functions , You don't have to put the code in the base class of domain objects or warehouse objects .

Use AOP Creating domain objects dynamically Proxy example , And implement the specified interface as needed .

unfortunately AOP(.NET) Must be used at runtime IL Bytecode dynamically creates classes , Or let the target object inherit ContextBoundObject. Is there a simpler way ? Look at this code :

public class OrderInfo
public IAspectMapper<OrderInfo> Mapper; // Register entity mapper 
public OrderInfo()
this.Mapper = AspectMapper.Inject(this, "OrderID") // Inject entities and intercept domain events 
.Before(MapperEvent.EntityCreate, Validator.Validate) // Call permission control module before entity creation. 
.AfterReturning(MapperEvent.EntityCreate, Logger.Log); // After the entity is created, the system log module is called. 

OrderInfo It's a normal entity (POCO), Declare a mapper in the entity for administration OrderInfo Association with other entities . And then use Before Interceptor interception EntityCreate event , Use AfterReturning Interceptor interception EntityCreate event .

When creating an entity 、 to update 、 Delete and so on , The data layer first checks the interceptors registered on the entity mapper , Call the relevant response method , According to the return value of the method, decide whether to perform the real operation .

The response to the incident Logger.Log Any number of overloads can be specified , The event source should automatically match the most appropriate method according to the entity and operation information .

public class Logger
public static void Log(OrderInfo sender, AfterReturningEventArgs e)
//e.EventType Entity operations to respond to 
if (e.EventType == MapperEvent.EntityCreate)
Debug.WriteLine(string.Format(" Order {0} Created ", sender.OrderNo));
public static void Log(object sender, AfterReturningEventArgs e)
Debug.WriteLine(" test ");

Data mapper can proxy any operation of any object , Just put the operation TargetClass.DO Change to AspjectManager.Invoke(TargetClass.DO) that will do . stay Invoke Before AspjectManager Will check the TargetClass.Mapper Interceptors registered on .

Although we need to POCO Object to register a small property , But this is not the cost and dynamic type 、 Inheriting base classes is trivial .

For examples and documentation, see

[ The quilt gameboyLV On 2012-04-10 23:19 A modified ]

本文为[Jiedao jdon]所创,转载请带上原文链接,感谢

  1. Help, Java how to get all the current processes of the system
  2. Has anyone ever used JMeter or written tests in httpUnit????
  3. Living in a mountain village in late spring
  4. Partridge day, spring of HKUST
  5. JavaScript异步编程4——Promise错误处理
  6. 海康摄像SDK开发笔记(一):海康威视网络摄像头SDK介绍与模块功能
  7. JavaScript asynchronous programming 4 -- promise error handling
  8. Haikang video SDK development notes (1): introduction and module functions of Hikvision webcam SDK
  9. JOP:用于FPGA的嵌入式实时系统中的Java优化处理器内核
  10. Spring Boot源码:使用MongoDB MongoTemplate公开REST在几分钟内实现CRUD功能
  11. Spring Boot应用程序事件教程 - reflectoring
  12. 带有Resilience4j断路器的Spring云网关 - rome
  13. 经验分享:Apache Kafka的缺点与陷阱 - Emil Koutanov
  14. 通过Spring Boot Webflux实现Reactor Kafka
  15. 从Java 8升级到Java 11应该注意的问题
  16. Jop: Java optimized processor core for FPGA embedded real time system
  17. Spring boot source code: use mongodb mongotemplate to open rest to realize crud function in a few minutes
  18. Spring boot application event tutorial - reflecting
  19. Spring cloud gateway with resilience4j circuit breaker - ROM
  20. Experience sharing: shortcomings and pitfalls of Apache Kafka - Emil koutanov
  21. Realization of reactor Kafka through spring boot Webflux
  22. RPC框架设计----Socket与I/0模型
  23. Problems in upgrading from Java 8 to Java 11
  24. RPC framework design -- socket and I / 0 model
  25. RPC框架设计----I/0模型
  26. RPC framework design: I / 0 model
  27. RPC框架设计----NIO编程缓冲区Buffer
  28. RPC框架设计----NIO编程缓冲区Buffer
  29. RPC framework design -- NiO programming buffer
  30. RPC framework design -- NiO programming buffer
  31. Java多线程基础
  32. Java multithreading Foundation
  33. 码农飞升记-00-Java发展历程
  34. Development history of coder-00-java
  35. 码农飞升记-00-Java发展历程
  36. Development history of coder-00-java
  37. Spring and Autumn Moon
  38. Node.js与Spring Boot比较? - Ryan Gleason
  39. Spring WebFlux的明显陷阱 - ŁukaszKyć
  40. Spring创始人Rod大叔对YAML的真实想法
  41. Compare node.js with spring boot- Ryan Gleason
  42. Obvious pitfalls of spring Webflux- Ł ukaszKy ć
  43. Spring founder uncle rod's real thoughts on yaml
  44. 码农飞升记-02-OracleJDK是什么?OracleJDK的版本怎么选择?
  45. What is manong feisheng-02-oracle JDK? How to choose the version of Oracle JDK?
  46. Spring tide surging Xinanjiang
  47. Linux内核软中断
  48. Linux kernel soft interrupt
  49. Linux内核软中断
  50. Linux kernel soft interrupt
  51. Java multithreading Foundation
  52. The construction of Maven private library nexus
  53. I / O stream in Java
  54. JDK 16:Java 16的新功能 - InfoWorld
  55. 在Java中本地进行线程间数据传输的三种方式和源码展示
  56. jdon导致cpu 99%最后tomcat死掉---banq给予回复
  57. 用领域事件模拟AOP注入
  58. JDK 16: new function of Java 16 - InfoWorld
  59. Cartoon: from JVM lock to redis distributed lock
  60. Spring 3.1 终于加入了Cache支持