Imagine that all the bussines logic codes of the project you wrote are finished and you do not have time to breathe, and at the same time, the necessary tests are coming. Consider that although the program is complete, there are performance problems in some methods and you will have to write the necessary log or tools to examine it. In such a case, how do you do this in a nice way without breaking the Bussines Logic codes?
As a result of your monitoring, you found a method that causes performance problems due to its slow operation, and you cannot do it in debug because it is online. To track it, you do a tracking in log form:
Looks fine. After solving the problem here, you noticed another problem, “Handle” and then you’re reviewing it again:
Although the two blocks of code above are ugly, you managed to fix the problem. But coding like this is not appropriate:
- They are not reusable and there is constant code duplication.
- Logging, Performance monitoring, they are confusing by occupying the Business Logic codes.
After realizing these issues, your second form of code would look like this:
Seems like a good solution but still doesn’t solve the main problem. Even though common works are under “common”, it still breaks Bussines Logic codes in many places. The hierarchical structure of this code is as in the picture below:
Aspect-Oriented Programming (AOP)
What is AOP?
- Abbreviation: Abbreviation for Aspect Oriented Programming.
- Application scenario: Provides good reuse and separation for methods not related to any Bussines Logic. For example security control, exception handling. But it must be operated on a global scale.
- Core concept: AOP thinking is to operate around the main idea. Aspect is to prevent some operations of the target object, to prevent the operations of some other parts of the system on the target object and to make a real performance measurement by doing some operations before and after the Bussines code.
- Implementation mode: AOP implementation is divided into two. “static” and “dynamic”. Static, placing the necessary codes in the IL code through the compiler. Dealing with a dynamic proxy object at runtime.
Note: This topic may seem complicated, it is useful to read AOP from different sources.
What is the Cut Noodles?
Aspect Oriented Programming has 6 characters and the most difficult is to understand Aspect.
- In real life, a large watermelon is cut in half with a knife and its integrity is broken. The flesh of the watermelon is completely opened to you and you can eat it however you want, but you cannot eat it without cutting it.
- In the computer world class(object) grasps several methods and with this direction it looks like a whole watermelon. You split it by choosing an entry point. The method is completely open to you, you can do any blocking according to your needs. But if you can’t break the method, you can’t do these interceptions. There is no blade available in the computer world of course, but there is another abstract concept, as explained below:
Vertical inheritance for methods interference:
The problems here are the ones we talked about earlier. That’s why we don’t repeat.
Using AOP horizontal method manipulation with a dynamic Proxy class.
Of course, the OderProxy object here is dynamically created at runtime with the help of a framework. The client does not know that the called object is actually an OrderProxy. An entire process is shown below:
- Target: The target class to be proxyed.
- Join Point: The point to be connected, the methods that may require intervention.
- Point Cut: The entry point obtained by the Connection Point.
- Aspect Class: Provides common tasks for process management, logging, performance monitoring and other operations.
- Advice: Notification point, improved code.
- Weaving: Applies enhancements to the target object, where a new Proxy object is created.
- Proxy: Proxy class.
Using AOP in Unity3D
There are many good AOP Frameworks for .NET but none for Unity yet. Be aware that many technologies will not be supported as it is Cross-Platform. So we will implement it in a different form. From what we understand, we should focus on two points:
- Since there is no framework to create a dynamic proxy object, we will create it ourselves.
- The proxy object will be used to manipulate the method. In this case, we will send the proxy object how it will interfere with the method by making a delegate like a strategy.
Proxy identification, a class to be proxyed, a certain method and intervention strategy:
Interface of the intervention strategy:
An implemented example:
Now for the use case, let’s imagine we’re going to interfere with the test method in the repository. It will print the log before and after: