PostSharp: Everything you need to know

25 juni 2021 om 10:00 by ParTech Media - Post a comment

Before we dive deep into PostSharp, you must first understand the concept of Aspect-Oriented Programming (AOP). This programming style allows the separation of distinct and independent functions.

Such a style strives for cohesive functionality as the code modularity is increased. These distinct functions are classified as cross-cutting concerns which classify the major parts of the whole application like data model, user interface, or business logic.

Such high-level encapsulation forms the core concept of AOP. With that basic understanding of AOP, let us jump into the article and understand what is PostSharp tool.

Table of contents

  • What is a PostSharp tool?
  • Why use PostSharp?
  • Benefits of using PostSharp
  • Problems tackled by PostSharp
  • How does PostSharp work?
  • Key Concepts used by PostSharp
  • Conclusion

What is a PostSharp tool?

PostSharp tool is one of the most commonly used software development tools used in the .NET world that uses the concept of Aspect-Oriented Programming. It provides the ability to encapsulate repetitive code into custom Attribute classes. These classes are referred to as various types of Aspect classes in PostSharp.

PostSharp offers tools required to develop a clean and precise chunk of code in fewer lines than normal. Such concise codes can be successfully written because the PostSharp method of operation is typically categorized as Design Pattern Automation where it implements applications based on patterns and optimizes the productivity of development teams.

Why use PostSharp?

Here are some other benefits of the PostSharp tool -

  • Code will be cleaner
  • Boiler plating is reduced i.e., repetitive code will be eliminated.
  • Abstraction and modularity of the code is increased
  • Improves maintenance which makes debugging easier and performance can be tracked easily.
  • Reduces the cost of maintenance due to fewer defects
  • The resilience of the code is increased when new functionality is added or an old one is removed
  • Organized readability increases the productivity of the team

Problems tackled by PostSharp

The concept of pattern is not used while doing conventional coding. Hence patterns are often hand-coded which further results in boilerplate code.

Following are the impacts of Boilerplate code:

High Development Effort

  • Large codebases: Few applications feature a large amount of boilerplate code or repetitive code when implemented using the existing mainstream compiler technologies.
  • Reinventing the wheel: As there are no reusable options within conventional programming languages, the solution to problems like INotifyPropertyChanged is always reinvented.

Poor Quality Software

  • A high number of defects: There is a high possibility of defects in each line of code that is written. However, the code which is the result of copy-paste is more likely to have errors as subtle differences tend to get ignored in such cases.
  • Multithreading issues: A lot of value is not delivered when it comes to using object-oriented programming to develop multithreaded applications. PostSharp considers the issues at a lower level of abstraction with events, interlocked accesses, or locks which can eventually result in random data races or deadlocks.
  • Lack of robustness: Exception handling or caching is often deliberately omitted in Enterprise-grade features due to large amounts of source code. This is unintentionally forbidden in certain parts of applications that remain untested and hence are unreliable.

Difficulty in adding or modifying functionalities

  • Unreadable code that is difficult to maintain: Reading the business code becomes a lot more difficult when the initial developer leaves. It is often littered with non-functional low-level requirements, which makes it difficult to understand and maintain.
  • Strong coupling: Duplicate code is formed due to poor problem decomposition. Strong coupling makes it a lot more difficult to change the implementation of exception handling, INotifyPropertyChange, logging, etc because it is scattered among tons of files.

A slow ramp-up of new team members

  • A lot of knowledge required: When new teammates join to work on a particular feature, they need to first know about threading, caching, and other technical issues before they start contributing to the business value. This is an example of a bad division of labor.
  • Long feedback loops: Even in small teams, the patterns like logging, threading, diagnostics, undo, redo, etc. might be dealt with differently by different developers. Hence the architect needs to see that new team members follow the design standards and utilize most of their time reviewing the manual code.

The below image shows the different problems that can be solved by PostSareharp:

How does PostSharp work?

Conceptually speaking, you can think of PostSharp as an extension to the VB compiler or C#. However, practically the compiler of Microsoft itself is not extensible but the built process can be extended. A similar thing happens in PostSharp - when it inserts itself into the build process, it post-processes the output of the compiler.

Let’s see how it happens:

  • MSBuild Integration: Using PostSharp.targets, PhotoSharp integrates itself within the build process. PostSharp.targets are imported in every project by the NuGet installation script named install.ps1 using PostSharp. Few steps are added to the build process by PostSharp.targets. Post-processing is the most important step of the compiler's output handled by Postsharp itself.
  • MSIL Rewriting: By reading and disassembling the intermediate assembly, validations, executing the required transformations, and writing the final assembly to the disk, PostSharp post-processes the compiler output. Though it might sound magical, remember the MSIL technology of PostSharp has been used by thousands of developers since 2004 as it is very stable and mature.

Key concepts used by PostSharp

To leverage design pattern automation, PostSharp uses a combination of several concepts as given below:

  • Metaprogramming: Writing a code that analyzes and transforms itself or other programs is Metaprogramming. A .NET program is internally represented as a .NET object by PostSharp, hence it can be considered as a tool used for metaprogramming.
  • Aspect-Oriented Programming: AOP is a programming style that is orthogonal to OOP and allows modularization of certain features that would otherwise crosscut a huge number of methods and classes. It can be confidently said that PostSharp is an advanced AOP framework.
  • Static program analysis: The analysis of a program without executing it is called static program analysis. The static analysis tool has two families viz. structural static analysis and behavioral static analysis. PostSharp has tools for structural static analysis only.
  • Dynamic program analysis: The analysis of a program during its execution is called dynamic program analysis. It is used to detect issues as early as possible to prevent bigger damages. To check the program against threading models, dynamic analysis is used by PostSharp.

Conclusion

From the benefits seen in this post, you can safely conclude that PostSharp is an amazing tool that can be used to eliminate any boilerplates. It also helps in separating concerns between different sets of codes. Even after several years of its development, it is still a trending tool that is used by various leading companies across the globe.

Nieuwste