Webinar: How to Stay DRY with AOP and PostSharp – with Video and Q&A!
Wow! What a turnout for my first webinar! Thank you all for attending, I had a great time presenting it, and will continue to do more webinars in the future. The webinar was recorded, and the video is now online for your viewing:
As promised, here are the answers to the great questions you asked, which I didn’t have the chance to answer during the live webinar. As always, you can always ask your questions on our Support Forums.
Q: Any reason the [aspect] class must be Serializable?
A: At (post) compilation, the aspect is serialized using binary serialization, and added as a resource to the target process. Then at runtime, the aspect is deserialized. PostSharp will emit a compilation error if you forget to mark your aspect as Serializable.
Q: What is the typical use case for creating interfaces using PostSharp?
A: One typical scenario for implementing interfaces with PostSharp is implementing INotifyPropertyChanged in WPF to support Data Binding.
Q: Are the OnExit and OnEnter aspects thread safe? How would one make it thread safe if it is not?
A: While the aspects themselves are thread-safe, shared state (i.e. custom fields) within the aspects is not. To safely pass data around between OnEnter and OnExit events, you can use the MethodExecutionArgs.MethodExecutionTag property.
Q: What about signed DLLs and PostSharp?
A: PostSharp has no problems with signed DLLs – they will be re-signed after the post-compilation process.
Q: What about Silverlight DLL's, is it also available?
A: Of course, PostSharp comes with the Silverlight assemblies included, you need to add a reference to the PostSharp.SL.dll instead of PostSharp.dll. For more information, please refer to the online documentation.
Q: Can you provide a good definition of aspect oriented programming?
A: Aspect Oriented Programming complements traditional OOP by allowing us to encapsulate infrastructure code (such as logging, security, validation, etc.) into small modules called aspects, and then apply those aspects throughout the entire system in such way that the business logic is completely separated from those concerns.
So go ahead, download the free starter edition now!
Happy PostSharping!
-Igal
Live Webinar: How to Stay DRY with AOP and PostSharp
Boilerplate code. It’s all around us, polluting our business logic, and forcing us to write the same code over and over again. Join us on Thursday, January 26th, as our very own Igal Tabachnik shows you how to stay DRY (Don’t-Repeat-Yourself) by using aspect-oriented programming (AOP) and PostSharp to remove boilerplate code that’s duplicated in and across systems.
Attendees will learn:
- Why the DRY principle matters
- How AOP helps to remove boilerplate code
- How to use PostSharp to produce cleaner code
- Real-world AOP usage examples
Oh, and that’s not all – we’re giving away two PostSharp licenses to two lucky attendees!
So hurry up, sign up here: https://www3.gotomeeting.com/register/724468246
Feature Focus: Showing message locations in the source code
During compilation, PostSharp takes great care in making sure that everything works correctly. When something goes wrong, PostSharp will report it as an error or a warning. Until now, however, whenever an error or a warning occurred, the developer had to manually navigate to that place in code.
We are excited to announce that with PostSharp 2.1 we’ve enhanced the errors and warnings with the exact location information, allowing you to simply double-click on the message and you’ll be taken to the error line!
To enable this feature, go to Tools – Options – PostSharp, and under Experimental, set Resolve Message Location to True:

Then, simply rebuild your solution, and if there are any warnings or errors, you’ll be able to see exactly where they are:

This is accomplished by specifying the member (in this case, the method) that is responsible for the message, in the aspect’s CompieTimeValidate method:
// Validate the attribute usage.
public override bool CompileTimeValidate( MethodBase method )
{
// Don't apply to constructors.
if ( method is ConstructorInfo )
{
Message.Write( method, SeverityType.Error, "CX0001",
"Cannot cache constructors." );
return false;
}
MethodInfo methodInfo = (MethodInfo) method;
// Don't apply to void methods.
if ( methodInfo.ReturnType.Name == "Void" )
{
Message.Write( method, SeverityType.Error, "CX0002",
"Cannot cache void methods." );
return false;
}
// Does not support out parameters.
ParameterInfo[] parameters = method.GetParameters();
for ( int i = 0; i < parameters.Length; i++ )
{
if ( parameters[i].IsOut )
{
Message.Write( method, SeverityType.Error, "CX0003",
"Cannot cache methods with return values." );
return false;
}
}
return true;
}
Aspect developers are encouraged to include the member in error/warning messages. For more information, please refer to the documentation on Working with Errors, Warnings and Messages.
Please note that this is not enabled by default as it is still experimental and might have an impact on performance. Please let us know how it works out for you!
Happy PostSharping!
-Igal
Feature Focus: View source code enhanced by PostSharp
As most of you know, PostSharp’s transformation (weaving) of aspects into your assemblies happens after the compilation. One of the most requested features is the ability to see the actual code that is produced by PostSharp. Until now, you had to manually open the newly compiled assembly in your favorite decompiler to see the produced code.
We are delighted to announce that we’ve made viewing the source code in your favorite decompiler much easier – via a single click on the enhanced class or method:

By clicking the See enhanced source code link for the first time, you will be asked to select the decompiler you want to use (we currently support dotPeek, ILSpy and Reflector). You can always change the decompiler later from Tools – Options – PostSharp:

From now on, when you click on the See enhanced source code, your chosen decompiler will open and show you exactly the source code, as it was modified by PostSharp!

This feature is available in the latest version of PostSharp!
Happy PostSharping!
-Igal
Free Course from Donald Belcham on Pluralsight

If learning more about Aspect Oriented Programming is on your to-do list for 2012, we have some very good news for you.
We’ve teamed up with the good folks at Pluralsight to make it easier than ever for developers to get up to speed on developing with aspects. PostSharp users can sign-up to receive a free 30-day trial membership good towards one Pluralsight online course.
Start learning about Aspect Oriented Programming for .NET today >>
Pluralsight, “the leader in high-quality online training for hardcore developers,” has a huge library of online courses to choose from but, naturally, the course we recommend is Aspect Oriented Programming for .NET by PostSharp MVP and Pluralsight Author, Donald Belcham.
Donald is a senior software developer, independent contractor, author, and trainer based in Edmonton, Canada. He spoke about the topic of Aspect Oriented Programming at NDC and DevTeach Montreal earlier this year, and published the online course on Pluralsight in June.
The course is divided into four parts:
- Introduction to AOP
- AOP using Interceptors
- AOP using IL Code Weaving
- AOP beyond decorators
From Donald: “My course is perfect for developers who want to learn how to avoid code repetition by implementing AOP in .NET projects using IL Code Weaving frameworks like PostSharp, Interception frameworks like Castle Windsor, and even how to implement AOP without following the traditional decorator pattern.”
Apply for your free 30-day Pluralsight trial membership today and, upon approval, we’ll email you a unique login code plus a link to a special sign-in page for PostSharp users.
Start your free 30-day trial membership today >>
-Britt