In the previous article, we saw how to use PostSharp to help us to use the configuration sections, with the use of two aspects. In this article, I will focus on the same objective. But this time, we will use only one aspect, which will be a mix of the two aspects of the last article.
Tag - AOP
Tuesday 7 December 2010
[PostSharp] Ease the creation of configuration sections - Update
By Michael DELVA on Tuesday 7 December 2010, 09:56 - C#
Wednesday 1 December 2010
[PostSharp] Ease the creation of configuration sections
By Michael DELVA on Wednesday 1 December 2010, 14:00 - C#
In this article, I'll show yet another use of PostSharp, this time focusing on the ConfigurationSection class. If you look in the MSDN at the articles which explain how to use the ConfigurationSection, you may have noticed that the code you need to write is repetitive, tedious, and error-prone:
public class PageAppearanceSection : ConfigurationSection
{
[ConfigurationProperty("remoteOnly", DefaultValue = "false", IsRequired = false)]
public Boolean RemoteOnly
{
get { return (Boolean)this["remoteOnly"]; }
set { this["remoteOnly"] = value; }
}
[ConfigurationProperty("font")]
public FontElement Font
{
get { return (FontElement)this["font"]; }
set { this["font"] = value; }
}
[ConfigurationProperty("color")]
public ColorElement Color
{
get { return (ColorElement)this["color"]; }
set { this["color"] = value; }
}
}
As you can see, you have to put the ConfigurationPropertyAttribute on top of each property, and to make calls to an indexer to the Get and Set methods of the properties, using the same name you used in the attribute.
Doesn't this repetition look like to be a good candidate for an aspect?
Friday 1 October 2010
[PostSharp] Don't NotifyPropertyChanged all properties
By Michael DELVA on Friday 1 October 2010, 14:00 - C#
This article will show you another improvement which can be made to the DataBinding sample aspect found on SharpCrafters, after the Fire INotifyPropertyChanged.OnPropertyChanged for read-only properties depending on other properties article.
By default, the aspect will fire the PropertyChanged event each time any property is changed. But what if you don't want some of them to fire the event? Or what if, as it happened to me, you set the value of a property, and must do some work in the property setter, even if the new value of the property is the same as the old one?
The latter example is a bit odd, but in the default implementation of the aspect, in the OnPropertySet method, you have this code:
if ( args.Value == args.GetCurrentValue() ) return;
args.ProceedSetValue();
If the new value of the property is the same, the aspect will return, and then never call the args.ProceedSetValue(); method, which will avoid your code to be called in the property setter.
In this short article, I'll show you how to change the aspect, to call the OnPropertyChanged event on all properties, excepted those which are marked with a custom attribute.
Wednesday 1 September 2010
[PostSharp] Fire INotifyPropertyChanged.OnPropertyChanged for read-only properties depending on other properties
By Michael DELVA on Wednesday 1 September 2010, 15:53 - C#
One problem of the INotifyPropertyChanged aspect concerns automatic notification of read-only properties which depend on other properties.
Indeed, say you have this class, on which the aspect is applied:
[NotifyPropertyChanged]
public class Test
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get { return FirstName + " " + LastName; } }
}
If you change the values of FirstName or LastName, the OnPropertyChanged event will be fired, and the view will be able to display the new values of the properties. But if a control of your view is bound to the FullName property, it won't be updated. A solution would be to declare FullName as an automatic property, and update it in the setters of FirstName and LastName. Not very practical.
In this article, I will show you how to extend the aspect, in order to make it fire the event for dependent properties when a "parent" property is modified.
Wednesday 4 August 2010
[PostSharp] How to virtualize all the methods of a class
By Michael DELVA on Wednesday 4 August 2010, 14:00 - C#
When using NHibernate, it is recommended to declare the public and protected properties and methods of your entities as virtual, to be able to use them with proxies.
But missing a virtual keyword for a member of a POCO is easily forgettable, and unfortunately, you will only be warned of your mistake on runtime, when your mappings are being built.
In this article, I will show you how to 'not care' anymore about that, and let PostSharp do this stuff for us.
Thursday 15 July 2010
[PostSharp] [Caliburn] - Automatically call NotifyOfPropertyChange on the view models
By Michael DELVA on Thursday 15 July 2010, 14:42 - C#
Here, I won't explain what are Caliburn and PostSharp. Their respective documentations, examples and samples already do that very well. But I will show you how to create an aspect which will be applied by PostSharp on the view models of a WPF assembly.
And as a result, on runtime, each time a public property will be changed, the event PropertyChanged of the interface INotifyPropertyChanged will be fired, and the WPF view will be able to refresh its state.
If you already know PostSharp, or have been on their website, you have certainly noticed that they already propose an aspect which intends to implement the interface INotifyPropertyChanged, and to call PropertyChanged when the value of a property is changed.
And if you already know Caliburn, you know that the classes you must inherit from to implement your view models inherit themselves from an abstract class named PropertyChangedBase, which implements INotifyPropertyChanged, and which proposes a function named NotifyOfPropertyChange, to which you give as a parameter the name of the modified property.
The aspect I'm going to show you will allow us to mix these 2 methods.
Tuesday 22 June 2010
[PostSharp] [Caliburn] - Appeler automatiquement NotifyOfPropertyChange pour les ViewModels
By Michael DELVA on Tuesday 22 June 2010, 14:28 - C#
Dans cet article, je ne vais pas vous expliquer ce que sont Caliburn et PostSharp, leurs documentations et exemples respectifs le font déjà très bien, mais je vais expliquer comment créer un aspect que Postsharp va appliquer à des classes que nous définirons, et qui aura pour résultat d'implémenter l'interface INotifyPropertyChanged dans ces classes, et d'appeler automatiquement l'évènement PropertyChanged lorsque la valeur des propriétés publiques de ces classes va être modifiée.
Si vous connaissez déjà Postsharp, ou avez été regardé sur leur site, vous aurez sûrement remarqué qu'il existe déjà un aspect qui permet d'implémenter l'interface INotifyPropertyChanged, et d'appeler PropertyChanged à chaque changement de valeur de la propriété.
De même, si vous connaissez Caliburn, vous aurez également remarqué que les classes dont vous devez dériver pour implémenter vos ViewModels dérivent elles-même d'une classe abstraite nommée PropertyChangedBase, qui implémente INotifyPropertyChanged, et qui propose une fonction nommée NotifyOfPropertyChange, à laquelle vous passez comme paramètre le nom de la propriété qui a été modifiée.
L'aspect que je vais vous montrer va nous permettre de mixer ces 2 approches.