You can download PostSharp 4 today from Visual Studio Gallery and update your projects using NuGet.
PostSharp 4 applies the success of pattern-driven development to the realm of multithreaded applications and provides a thread-safe extension to C# and VB. Application developers can annotate their classes with custom attributes such as [Immutable], [Actor] or [Synchronized] and have their code automatically validated against the model. Any violations result in build-time errors or run-time exceptions instead of data corruptions.
PostSharp 4 makes it incredibly easy to implement undo/redo in desktop and mobile applications by automatically recording changes in model objects. Additionally, it includes more than 15 other improvements and enhancements.
Information in this release announcement includes:
- Threading Models;
- Undo/Redo;
- Aggregation and Composition;
- Other Improvements;
- More Free Aspects in PostSharp Express;
- Platforms Newly Supported;
- Platforms No Longer Supported.
Threading Models
When comparing modern object-oriented programming to assembly language, one can see that notions like classes, fields, variables or methods really simplify developers’ lives and increase their productivity. But when it comes to multithreading, one cannot state that object oriented programming (OOP) delivers so much value. As a result, multithreading is still being addressed at a low level of abstraction with locks, events or interlocked accesses. The resulting cost for the industry, in terms of development effort and number of defects, is enormous.
Alternatives to OOP like functional programming and actor programming have been proposed to address the challenges of multithreading. However, these paradigms are not as appropriate as OOP for business and mobile applications. They have failed to gain wide adoption, existing only in narrow niches where high performance wins over high development cost.
Instead of trying to replace OOP, PostSharp 4 extends the C# and VB languages with custom attributes like [Immutable], [ThreadAffine], [Synchronized] or [Actor], which allow developers to assign a threading model to a class. Based on these custom attributes, the compiler extension validates the code against the model and generates synchronization or dispatching instructions.
PostSharp 4 implements the following threading models:
- Immutable and Freezable;
- Synchronized and Reader-Writer Synchronized;
- Actor;
- Thread Affine and Thread Unsafe.
What are the benefits?
Thanks to threading models, source code is safer and simpler. Threading models bring the same kind of productivity gains than the stricter C#/VB memory model brought to C++. Source code is more concise and easier to reason about and there are far fewer chances for non-deterministic data races. Threading models make it easier for everyone on the team, not just for multithreading experts, to build robust business applications.
Why is this a significant innovation?
We believe that our approach to multithreading is innovative in many ways, and that its significance exceeds the scope of the PostSharp community. Points of interest include the focus on business applications and average developers (instead of critical software and expert developers); the idea that several threading models can co-exist in the same application as an extension of a mainstream language (instead of requiring a rewrite in a specialized language); and the use of UML aggregation as a foundation to select the synchronization granularity.
See our technical white paper for more details about our approach. We think this new approach could be applied to other object-oriented languages and environments.
Additional Resources
For more information, see:
- Blog post: Immutable and Freezable Done Well
- Technical White Paper: A Thread-Safe Extension to Object-Oriented Programming
- Reference Documentation: PostSharp Threading Pattern Library
Undo/Redo
A feature that always scores at the top of each the wish list of each application is undo/redo. Oddly enough, the feature usually remains unaddressed for years because it is awfully painful to implement using conventional technologies. It simply generates too much boilerplate code.
PostSharp 4 takes the boilerplate code out of way. Just add the [Recordable] attribute to your model classes, and they will start appending any change into a Recorder. You can then use the Undo and Redo methods of the Recorder, or add the off-the-shelf WPF buttons to your toolbar.
What are the benefits?
PostSharp 4 makes undo/redo affordable for virtually any application, even line-of-business software that typically has a large object model and a small user base. Previously, the feature had a prohibitive cost and would pay off only in simple applications or software with a large customer base.
The Recordable pattern offers more flexibility than the traditional Memento pattern, and its implementation can be completely automated by the compiler. It demonstrates how better compiler technologies can lead to simpler source code and reduced development effort.
Additional Resources
For more information, see:
- Blog post: Undo/Redo – part 1, part 2, part 3, part 4
- Reference Documentation: Implementing Undo/Redo
Aggregation and Composition
When we built our threading models and undo/redo feature, we realized that we needed a notion of parent-child relationships. When we kept thinking deeper about that, it became clear that most object designs actually relied on this notion. It’s not surprising if one considers that aggregation and composition are core concepts of the UML specification. Yet, it’s a pity it has not been implemented in programming languages. We needed to fix that.
Classes that can be involved in parent-child relationships must be annotated with the [Aggregatable] custom attribute, or with any other attribute that implicitly provides the Aggregatable aspect. Then fields that hold a reference to another object must be annotated with [Child], [Reference] or [Parent].
The principal role of the Aggregatable aspect is simply to automatically set the parent property when a child is assigned to a parent and to implement a child visitor.
For more information, see:
- Blog post: Aggregation and Composition Patterns
- Reference Documentation: Implement Parent/Child Relationships
Other Improvements
In addition to the previously described new features, we significantly enhanced the PostSharp Aspect Framework and worked on other components as well:
- 4x runtime performance enhancement on our NotifyPropertyChanged aspect.
- Improved reliability and scope of our deadlock detection policy.
- Dynamic advices: see IAdviceProvider, IntroduceInterface, ImportLocation, ImportMethod, IntroduceMethod.
- Aspect Repository and Late Validation.
- OnInstanceConstructed advice.
- Faster advice state lookup: see DeclarationIdentifier.
More Free Aspects in PostSharp Express
PostSharp 4 generalizes the idea of giving our ready-made pattern libraries for free for small projects. PostSharp Express 4 now includes the following features for free – for up to 10 classes per Visual Studio project:
- NotifyPropertyChanged
- Recordable, EditableObject (undo/redo)
- Aggregatable, Disposable
- Threading (threading models, dispatching)
- Code Contracts
- Logging (up to 50 methods)
If you like the new features and want to use them more, you can buy them as a part of PostSharp Ultimate or as an individual product. More on our pricing page.
Platforms Newly Supported
PostSharp 4 adds full support for:
- Win RT both on Windows 8 (the previous support was error-prone and needed a complete redesign) and Windows Phone 8.1.
- Windows Phone 8.1 “Silverlight”.
- C++ “mixed mode” assembly references.
Platforms No Longer Supported
We decided that new releases of PostSharp would not support platforms for which Microsoft no longer provides mainstream support at the time of a PostSharp RTM release.
On development workstations and build machines, PostSharp 4 no longer supports:
- Windows XP (Vista, 7 or 8 is required).
- Visual Studio 2010 (2012 or 2013 is required).
- .NET Framework 4.0 (4.5 is required).
On end-user devices, PostSharp 4 no longer supports:
- .NET Framework 2.0 (3.5, 4.0 or 4.5 is required).
- Windows Phone 7 (8 or 8.1 is required).
- Silverlight 4 (5 is required).
Note that PostSharp 3.1 still supports these platforms.
Summary
While Microsoft has lately been catching up with its competitors in the consumer market, enterprise developers may feel neglected. At PostSharp, our focus is to increase productivity of large .NET development teams. It’s only when you start counting lines of code with 6 or 7 digits – and you realize that every line may cost your employer between $10 and $20 – that you appreciate and receive the full benefits of automating design patterns.
PostSharp 4 marks a significant innovation in the realm of multithreading. It raises the level of abstraction by defining models so the machine can do more and humans must think less. But instead of requiring developers to switch to a different language, it respects their investments in C# and VB and extends the environment in a backward compatible fashion.
PostSharp 4 includes plenty of important new features including undo/redo, parent-child relationships, performance improvements, new advices and more.
It’s an exciting time to be a developer.
Happy PostSharping!
-gael