What's New in PostSharp 4.0
The principal focus of PostSharp 4 was to redesign the Threading Pattern Library from the ground up and make it a real solution to write thread-safe code with C# and VB. Additionally, we've introduced the undo/redo feature into the Model Pattern Library. To achieve these objectives properly, we had to implement a good old concept from UML and object-oriented modeling: aggregation and composition. We introduced significant improvements in the PostSharp Aspect Framework to support these new features.
PostSharp 4.0 includes the following improvements:
- Aggregatable pattern
- Disposable pattern
- Immutable threading model
- Freezable threading model
- Synchronized threading model
- Redesign of reader-writer-synchronized, actor, and thread-unsafe threading models
- Recordable pattern (undo/redo)
- Dynamic location imports
- Aspect repository
- OnInstanceConstructed advice
- InitializeAspectInstance advice
- NotifyPropertyChanged optimization
As it turns out, multiple patterns rely on the notion of parent-child relationships. These concepts are a part of the UML specification, where it is known as aggregation, but even modern programming languages don't implement the notions. We fixed that in PostSharp 4.0 with our AggregatableAttribute aspect. For details, see Parent/Child, Visitor and Disposable.
Once we have a notion of parent-child relationship, it is easy to build an aspect that recursively disposes a whole object tree. This is our DisposableAttribute aspect. For details, see Automatically Disposing Children Objects (Disposable).
The Immutable patterns made functional languages popular for its great usefulness in multi-threaded programs. Unfortunately, the concept has traditionally been difficult to object-oriented programming. PostSharp 4.0 provides a pragmatic implementation with the ImmutableAttribute aspect. For details, see Immutable Threading Model.
Even a well-implemented Immutable pattern can be too strict for some object-oriented scenarios. In this case, the Freezable patterns may be more suitable. Based on the Aggregatable pattern, the FreezableAttribute aspect makes it possible to build freezable object trees. For details, see Freezable Threading Model.
We took the right way in PostSharp 3.0 with threading models, but the vision was not yet fully consistent and the implementation was only partial. With PostSharp 3.2, we felt we had a better understanding of what we wanted to achieve, and completely revisited our threading models. Based on the Aggregatable pattern, and based on a consistent object model, the Threading Pattern Library is now much more powerful and consistent.
To allow to import several fields and properties into a single aspect field (which was not possible using ImportMemberAttribute, we added the IAdviceProvider interface and the ImportLocationAdviceInstance class.
The new IAspectRepositoryService service exposes the list of all aspects added to the code model, both using custom attributes or IAspectProvider, and offer a way to execute validation logic after all aspects have been discovered.
The OnInstanceConstructedAdvice custom attribute allows you to define an advice that is executed after the instance constructor exits.
The InitializeAspectInstanceAdvice custom attribute allows you to define an advice that is similar to RuntimeInitializeInstance() but passes information about the reason why the aspect is initialized (constructor, clone, deseralization).
Our NotifyPropertyChangedAttribute is four times faster at runtime on average.