One, why Callhandler need to sort
PIAB provides us with a good way to implement AOP. AOP is designed to achieve the separation of Business logic and non-business infrastructure logic. By Piab, we define these business-independent logic in a callhandler, and then apply the callhandler we need to the corresponding target object by attribute or configuration. In this sense, Piab has a very good flexibility and extensibility. However, as far as I am concerned, PIAB also has some deficiencies, its biggest limitation is that it cannot control the order of execution of multiple methods applied to a method. It is necessary to let Callhandler call in the order we want it to be.
For example, suppose we apply the following 3 Callhandler to a method:
Validationhandler: Used for validation of parameter parameters, such as whether it is null, whether the length of string is longer than it is long, and so on.
Transactionenlisthandler: Used to automatically incorporate operations into a transaction to ensure data consistency.
Auditlogginghandler: Audit Log After the operation was executed successfully.
Obviously, the normal sequence of execution should be this: The parameter validation is invoked at the very beginning, and the Audit log needs to be included in the same transaction as the target method, Validationhandler. So Transactionenlisthandler's call followed, and finally Auditlogginghandler.
The native Piab provided by Microsoft is not achievable, but the Enterprise Library is open source and we can modify Piab's source code to achieve our goals. And it's just a small change. Next we'll discuss how to implement the Callhandler Pipeline that can be sorted.
Ii. How to create sequential Callhandler Pipeline
If we want to understand the implementation of this sequential Callhandler pipeline, we need to have a certain understanding of the implementation mechanism of PIAB. In the second part of this series, I have elaborated on the implementation mechanism of PIAB, where I simply describe how a PIAB is implemented in AOP.
Piab's implementation of AOP can be summed up in one word: Method interception. It is a practical practice to create a real proxy based on target type through Piab factory, and then create the transparent proxy through this real proxy and invoke target through the transparent proxy Instance. In the creation of real proxy, all callhandler that are applied to the type are cached. When the call is made, transparent proxy invokes the Invoke method of real proxy. In this method, a handler Pipeline is formed in the callhandler that will be applied to the current methods. Each callhandler is called sequentially in pipeline order before the target instance is actually invoked.
And the point we're going to make is that after Callhandler pipeline is created, we reorder all the callhander in the order we want them to.