PostSharp Documentation

PostSharp Documentation

PostSharp is a tool that allows development teams to achieve more with less code in Microsoft .NET:

  1. Software developers will write clean, stable, efficient, and concise code. Fewer lines of code mean less time and fewer defects.

  2. Software architects will be able to deliver partially or fully executable patterns, not just coding guidelines. After having carefully identified and selected design patterns, architects can code, in C# or Visual Basic, how the pattern should be implemented. Depending on the complexity of the pattern, PostSharp will automatically implement the pattern and/or will automatically validate that its manual implementation respects predefined validity rules.

This topic contains the following sections.

About Design Pattern Automation

PostSharp can be categorized as a Design Pattern Automation tool.

Design Pattern Automation is the use of tools to optimize productivity of development teams implementing software based on patterns. Software patterns are general reusable solutions to a commonly occurring problem. Software design patterns are patterns in the context of software design.

Typically, the architecture team would identify and select design patterns in an early phase of a project. Architects would then create designs that implement the design patterns. These designs typically include class diagrams, and instructions specifying how developers should implement the design. Design specifications are usually expressed in natural language. Design Pattern Automation refers to the ability to express designs in a formal language and use tools to help implementing the design.

Tools can help developers and architects in implementing designs derived from patterns in two ways:

  • Tools can automatically implement some of the artifacts and behaviors required by the design, which then don't appear in source code. Design Pattern Automation therefore raises the level of abstraction of the source code, and makes the design intent more apparent in source code.

  • Tools can validate hand-written code against design rules, from simple naming conventions to complex rules involving analysis of source code. Without tools, this activity would have only relied on code review.

Design Pattern Automation can extend to patterns that are not strictly considered design patterns:

  • Design Pattern Automation extends to application-specific patterns (also named custom patterns), which are specific to the language, framework, and problem domain of an application. Although these patterns are not general to the whole software engineering industry, their implementation can also be automatically generated and/or validated.

  • Design Pattern Automation extends to implementation patterns, which are mere repetitions of code. Logging and exception handling belong to that category.

PostSharp components

PostSharp is composed of the following frameworks and libraries:

  • PostSharp Pattern Libraries provide ready-made, commoditized implementations of some of the most common patterns in .NET. For more information, see section [patterns].

  • PostSharp Aspect Framework allows you to automate the implementation of other code patterns and address code repetitions that are specific to your own applications, or simply that are not available off-the-shelf in a pattern library. PostSharp Aspect Framework is built on the principle of Aspect-Oriented Programming (AOP), a well-established programming paradigm, orthogonal to (and non-competing with) object-oriented programming or functional programming, that allows to modularize the implementation of some features that would otherwise cross-cut a large number of classes and methods. PostSharp contains the most advanced AOP framework for Microsoft .NET. For more information, see sections Developing Custom Aspects and Adding Aspects to Code.

  • PostSharp Architecture Framework is a static analysis tool that allows you to automate the validation of design pattern implementations, to enforce design intend, or simply to verify coding guidelines. The framework allows you to create constraint classes that encapsulate the validation logic and that can be applied to code artifacts. The framework provides tools to analyze the relationships between code artifacts and have access to the AST of method bodies. For more information, see section Enforcing Design Rules.

How does PostSharp work?

PostSharp inserts itself in the build process and enhances or validates the output of the C# or VB compiler. Although this might sound magic or dangerous, PostSharp's MSIL technology is stable and mature, and has been used by tens of thousands of projects since 2004. Other .NET products relying on MSIL transformation or analysis include Microsoft Code Contracts, Microsoft Code Analysis, and Microsoft Code Coverage.