Programming is one of the biggest fields in the world, and it is advancing at a very fast rate. Before we get into PostSharp itself, we first need to understand the concept of AOP — Aspect Oriented Programming. To put it in simple words, Aspect Oriented Programming is an extension of the widely popular Object-Oriented Programming, or OOP.
What sets it apart is this: in AOP you can insert reusable code and implementations into any method or function in your application. That means you can use AOP to implement non-functional requirements such as caching, logging, exception handling, and more — without tangling them through your business logic. When you start with PostSharp, the first thing you will need to do is create a few aspects for your application. And with AOP, the best way to do that is to use attributes.
- AOP extends OOP by letting reusable code be injected into any method or function.
- Ideal for non-functional requirements: caching, logging, exception handling.
- PostSharp works on the compiled code, weaving the AOP part in afterwards.
- Your first two aspects should be a logging aspect and a caching aspect.
01PostSharp
PostSharp is one of the best AOP frameworks that you can find, and it can be used to implement AOP in whatever application you desire. Its approach is distinctive: PostSharp first goes through the compiled code, and then implements the AOP part into the code later on. That post-compilation weaving is what lets you keep your source clean while the cross-cutting behaviour is still guaranteed to be applied everywhere it should be.
Logging Aspect
Aspects are an essential part of AOP, and they can be defined as the modularization of anything. If you want to improve your code using PostSharp, one of the first things to do is implement a logging aspect. What this does is log all the relevant data belonging to a method and store it safely. Implementing a logging aspect in a codebase can be tricky, but with the right research it becomes simple — and once in place, every method you mark gets consistent, dependable logging for free.
Caching Aspect
The next thing you need to do is create a caching aspect. In order to build one, you will need to create a class that inherits from the MethodInterceptionAspect class — you can name the class whatever you want. You will also need to write a constructor so that storing objects into the cache, and retrieving them again, is possible. With that wiring done, expensive calls can be answered from the cache transparently, without the calling code ever knowing.
Aspects can be defined as the modularization of anything.The core idea behind PostSharp
02Conclusion
These were a few of the things you should know about PostSharp and how you can solidify your code using the framework. PostSharp is certainly one of the most popular AOP frameworks available today. If you want to enhance your skills in AOP, it is advisable to get comfortable with the PostSharp framework early — the habits it teaches carry over to aspect-oriented thinking everywhere else.
When it comes to PostSharp, it usually deals with the C# programming language; however, AOP itself is also available in other programming languages. Still, PostSharp is certainly one of the best in the business.
Keep exploring: the API World Conference is a great place to visit if you want to learn more about the latest advances in programming.