Matthew Groves delivered part 2 of his 5-part live webinar series on AOP in .NET recently to support his upcoming book Aspect Oriented Programming in .NET, out from Manning Publications in June. The series covers insights found in his book and a variety of real-world coding examples using PostSharp.
In part 2, Matt demonstrates the method interception aspect, and looks at a real-world application of the aspect that can help with data transactions – followed by a lively Q&A session.
Q: With the use of Aspect Interceptors, does the behavior of stepping through and debugging the code in either the Interceptor or calling code change? If so, how?
A: With PostSharp, debugging works great! When PostSharp rewrites your code with IL manipulation it also makes appropriate changes to the debugging file. Which means, if you set a breakpoint in an aspect or in a method that has an aspect on it, that breakpoint will be hit when expected and you’ll get the full stack with that as well.
Q: Can you put a PostSharp attribute on an aspect’s method?
Q: Can we apply interceptors globally to all the methods in a class?
A: Absolutely. You can apply the attribute to the class and by default it will be applied to every method within that class. There are lots of customization available with PostSharp. You can customize how you want the attributes to be applied – even at assembly level.
Q: Is it possible to apply an aspect to DLL methods to which you don’t have the source?
A: In theory yes, but this scenario is not supported so you will be on your own in case of problem.
Q: Can we apply multiple aspects to a given method, and in what order are they executed?
A: In some situations, for instance if you have caching and authorization, the order in which the aspect is applied is very important. PostSharp gives us a few ways to deal with that. The simplest one is called AspectPriority where you specify a number to indicate the order in which aspects are run, and there are more robust features that PostSharp has as well to manage dependencies between aspects and aspect composition.
Q: What are some other examples of practical uses of aspects?
A: Logging, data transactions, caching, INotifyPropertyChanged, exception handling, authorization, validation, etc. In fact, PostSharp Ultimate comes with a whole suite of aspects that have been written for you. It’s a new feature in PostSharp 3 so you don’t have to spend time writing these aspects and can simply drop them in and start using them right away.
Q: Can you add an Attribute to a parent object and have it apply to all child objects?
A: Yes, using aspect inheritance.
Q: Will you show how to TDD aspects in this series, and do you show it in your book?
A: Not TDD specifically, but I have dedicated an entire chapter of my book to unit testing aspects and episode 5 in the series (May 30th) will focus on that.
Q: Does the aspect class name need to end with Attribute, as per .NET convention?
A: No, it’s not necessary. As long as you inherit from the Attribute base class, .NET will recognize it as an Attribute.
Q: Can you use PostSharp in Windows Phone 7 applications?
Q: What are the best practices for bringing dependencies into aspects?
A: That question will be covered at length during the Unit Testing Aspects episode on May 30th.
Q: When will your book be published?
A: June 14th.
If you want to ask Matt any question you may have about AOP in .NET and/or PostSharp be sure to sign-up for the series and join us every Thursday throughout the month of May.
Last Thursday our good friend Matthew Groves delivered part 1 of his 5-part live webinar series on AOP in .NET. He has a new book coming out next month called Aspect Oriented Programming in .NET from Manning Publications and the series will cover some of the practical AOP insights found in his book and a variety of real-world coding examples using PostSharp.
In part 1, Matt does an excellent job of demonstrating the basics of Aspect-Oriented Programming, including a number of live coding examples using ASP.NET and PostSharp – all in under 30 minutes.
Q: Can you provide any hints on unit testing with aspects involved?
A: We’re going to talk about unit testing aspects – unit testing code that aspects are applied to – in part 5 of this series [May 30th]. In fact, I have dedicated an entire chapter of my book to unit testing aspects.
Q: Is PostSharp the only AOP framework covered in your book?
A: My book is focused on AOP in .NET and PostSharp is a large part of that but I cover other .NET tools like Castle DynamicProxy and the ASP.NET examples [shown in the video]. The idea is that once you learn the principles of AOP, you will be comfortable using tools other than what’s covered in the book. So anything you learn about PostSharp or Castle you can apply to other frameworks as well.
I will be adding new recordings each week but, if you want to catch them live and have the chance to ask Matt any question you may have about AOP in .NET and/or PostSharp be sure to sign-up for the series and join us every Thursday throughout the month of May.
We’re excited to announce that PostSharp 3 RTM is now available for download from Visual Studio Gallery and NuGet. After a successful RC cycle, PostSharp 3 now enters the stable quality band and replaces PostSharp 2.1 as our featured version.
PostSharp 3 marks a huge evolution from previous versions. Designed to deliver more value with less learning, PostSharp 3 is now much more than an awesome framework: using simple smart tags in the Visual Studio code editor, you can add to your code ready-made implementations of some of the most common design patterns. Other improvements include first-class support for Windows Store, Windows Phone and Silverlight apps.
Improved Visual Studio Integration
The single most striking feature of PostSharp 3 is its deeper integration with Visual Studio.
For instance, move the caret to the name of a class or a method. Visual Studio will display a smart tag proposing actions that make sense in the current context: apply a threading model, add logging, implement INotifyPropertyChanged, …
A wizard then guides you through the configuration of the aspect.
The wizard installs all required NuGet packages and generates the code for you – so you don’t have to study documentation to figure out how it works.
Of course, you can still add aspects to your code the old good way.
Ready-Made Pattern Implementations
While talking to customers, we figured out that the Pareto law also applies to our product: 80% of customers use the same top 20% aspects. We thought we would provide more value to our customers by providing ready-made implementations for the most common patterns. The results are our three first pattern libraries:
Windows Store, Windows Phone and Silverlight
In previous versions of PostSharp, support for alien .NET platforms was quite limited. The raise of Windows Phone and Windows Store stressed the importance of supporting new trends, so we decided to invest in providing first-class support for Windows Store, Windows Store and Silverlight.
I’m very pleased that we could this through Portable Class Libraries.
The first challenge was to develop a portable alternative to the system BinaryFormatter and its [Serializable] attribute. The result is the PortableFormatter class and its [PSerializable] attribute – an interesting piece of software in itself.
Deployment through NuGet and Visual Studio Gallery
When PostSharp 1 was released seven years ago, it seemed mandatory to have a setup program. Over the years, source control and build servers have conquered the world, changing requirements on deployment of development tools.
Despites important limitations, Microsoft’s NuGet and Visual Studio Gallery are becoming the de-facto standards for deployment of development components and their user interface, so we decide to get aligned with Microsoft’s lead.
We are aware of some negative consequences and will be happy to work with customers who may be affected by this choice.
Subscription-Based Maintenance Model
PostSharp 3 marks also the move from a per-version to a per-year maintenance model, as described in a previous blog post. A maintenance subscription is now included as a mandatory part of the product fee. The maintenance subscription not only gives right to major versions, but also to bug fixes.
We believe that part of our failure to deliver support for Visual Studio 2012 in time was due to our per-version revenue model. The new revenue model will allow us to release features as soon as they are needed, irrespective of the current major version number.
For more information regarding licensing and support, please read our FAQ page or contact our sales team.
Tip: Upgrade Before Summer
PostSharp 3 is now stable and it’s a good time to upgrade. During the next 2 months, the team will be ready to answer your questions and address support issues, without any other big project in the pipeline. We may be a bit slower during the vacation period, so it’s a good idea not to delay the upgrade if you can do it now.
PostSharp 3 is a big turn for our company, and so far we are very happy with the feedback and the businesses we received.
It’s official: PostSharp 3 RC was a success and we are scheduling to move PostSharp 3 to the “stable” quality band on May 2nd. That means that, on May 2nd, the stable release of the PostSharp package on NuGet will be version 3.0. Because of the way the NuGet client is implemented and how people usually author their NuGet packages, this day may turn into a little apocalypse for some of you.
Problem 1. NuGet client in Visual Studio
The NuGet user interface in Visual Studio is hardwired to always give you the latest stable version of a Package. There is no way to specify that you want to stay in an old branch. Even if NuGet allegedly supports semantic versioning, NuGet is going to try to upgrade you to a new major version even if it not backward compatible with the old one (semantic versioning says that backward compatibility can be broken when the major version number changes).
However, PostSharp 3 is not fully backward-compatible with PostSharp 2:
- There are very minor API changes, and
- You will need to acquire a new license
Problem 2. Packages with dependencies on PostSharp
NuGet packages that have a dependency on PostSharp 2 may install incorrectly after the PostSharp 3 release because NuGet is going to install PostSharp 3 instead of PostSharp 2.
To prevent NuGet from installing PostSharp 3, you need to edit your .nuspec file and add an upper bound to the PostSharp version:
<dependency id="PostSharp" version="[2.1,3)" />
That’s it. Happy PostSharping!
If there’s one topic that is subject to almost religious observance, this is unit testing.
There has been much hesitation in the community about how PostSharp aspects should be tested and how aspects can consume dependencies from a dependency injection container.
Obviously, we have a lot of experience testing aspects. I finally took the time to write down a set of practices that can be used to create efficient tests of aspects. I also show techniques that allow aspects to consume services from a dependency injection container. Last but not least, I publish and document a test framework for aspect build-time logic, especially logic emitting build-time errors and warnings. The framework is derived from the one we are using internally.
More on our online documentation.
Some of you expressed the wish of to be able to read the PostSharp documentation offline on their tablet or ebook reader, so we did it. Our conceptual documentation is now available as a print-ready PDF document, and looks awesome on screen and paper.
It’s incredible the content renders to 140 pages, and it does not even include the class reference, which is still only available online or for download as a CHM.
But there’s an issue: although it looks like a book, it has been designed as a reference document and is not the best introduction to PostSharp. Online tutorials are still the preferred way to get started, and they are not included in the PDF.
Mark your calendar for Thursdays in May as we kickoff a new series of live webinars with our good friend, Matthew Groves.
His new book Aspect Oriented Programming in .NET from Manning Publications debuts in June and we’ve put together a 5-part webinar series that will give you a taste of the practical aspect-oriented programming insights found in his book and a variety of real-world coding examples as implemented by PostSharp.
Read on to learn more about the upcoming schedule and a special offer from Manning Publications.
Sign-up for the PostSharp live webinar series today >>
1) You May Already Be Using AOP
Thursday, May 2nd at 9:00 AM PDT |12:00 PM EDT | 4:00 PM GMT
Learn the basics of Aspect-Oriented Programming and see live coding examples of AOP frameworks that come with ASP.NET. We will also look at how to write a simple PostSharp aspect using a pattern that is similar to the ASP.NET framework.
2) Call This Instead: Intercepting Methods
Thursday, May 9th at 9:00 AM PDT |12:00 PM EDT | 4:00 PM GMT
One of the most common types of aspect is the method interception aspect, which runs a piece of code instead of the intended method. In this episode, we will look at a real-world application of the method interception aspect that can help with data transactions.
3) Before and After: Boundary Aspects
Thursday, May 16th at 9:00 AM PDT| 12:00 PM EDT | 4:00 PM GMT
A PostSharp method boundary aspect can execute code at its borders (e.g. before a method, after a method, on exception). In this episode, we will look at a real-world application of a method boundary aspect to help with caching.
4) Under the Hood of a Post-Compiler
Thursday, May 23rd at 9:00 AM PDT| 12:00 PM EDT | 4:00 PM GMT
The details of how a post-compiler like PostSharp works can be complex, but it's important to have a basic understanding of what's happening in order to make an informed decision about where and when to use it. In this episode, we will look at how to decompile a .NET assembly and view code with/without a PostSharp aspect with ILSpy.
5) Unit Testing and Thin Aspects
Thursday, May 30th at 9:00 AM PDT| 12:00 PM EDT | 4:00 PM GMT
The benefits of unit testing are numerous. When aspects are involved, keeping those aspects "thin" is a key to keeping your code easy to unit test. In the final episode we will explore some of the implications of unit testing when a post-compiler tool is involved.
Now there's even more reason to attend Matt's webinar series. The generous folks at Manning Publications are offering a big discount on AOP in .NET to all live webinar attendees. Seats are limited and this series is sure to sell out so reserve your spot today.
If you want to learn what difference AOP and PostSharp can make in your projects, and take advantage of Manning’s special discount offer, join us every Thursday throughout the month of May.
Reserve your seat for the PostSharp live webinar series now >>
We’re happy to announce that PostSharp 3 has finally reached the RC quality band. This means that it is now sufficiently documented, internal testing is completed, all known bugs (except some minor ones) have been fixed, and the rate of new bug reports has significantly decreased. We plan to move to RTW with two weeks, unless important bugs delay this objective.
It is now a good time for small teams to upgrade to the new versions. The procedure is described in our online documentation. We are planning more articles and webinars regarding upgrades and deployment of PostSharp 3 in the next weeks.
We reached one of the last milestones before the RC: publishing the documentation! You can now browse it online from http://doc.postsharp.net/.
Yet this is not a good time for all of you to rush on PostSharp 3 because I’ll have vacation (till April 7th) before the dead march to RTM. Support will be provided as usually, but we won’t do bug fixing releases until I’m back.
I like to write the documentation myself because it forces me to do a last API review and makes sure all concepts line up. Documenting is a never-ending job, and I hope this first version of the documentation cover the most important questions. Yet, there are still important holes. Let me know if you find one that is specially important to you, so I can prioritize.
This being done, the road is now clear for RC and then RTM. Finally!
We just pushed PostSharp 3.0.13 online, and this is just to inform you of the awful update procedure from previous builds.
Why is the upgrade procedure cumbersome?
PostSharp 3.0.12 included NuGet 2.1 in its payload. Unfortunately, because of limitations of the Visual Studio Extension Installer (VsixInstaller), it was not possible to install PostSharp when NuGet 2.2 was already installed. We had other problems with the way that VsixInstaller handled dependencies (I’m quite confident to say that it is a broken feature), so we decided to handle dependencies manually, without relying on VsixInstaller.
Because of the dependency to NuGet, PostSharp needed to be installed for All Users, requiring elevation during installation. But since we don’t have the dependency to NuGet anymore, PostSharp does not need to be installed with elevated privileges anymore. This is one more situation that VsixInstaller does not handle properly.
What is the proper procedure?
Since Visual Studio Extension Installer is not able to upgrade PostSharp 3.0.12 to 3.0.13, you will have to uninstall PostSharp manually.
- Start Visual Studio with elevated privileges.
- Go to Tools / Extensions and Updates, click on PostSharp, then click Uninstall
- Install the new PostSharp
That’s it. Perhaps that was not so awful.
Remember that this procedure updates only the Visual Studio Extension for PostSharp. You can upgrade the compiler itself using NuGet.