INotifyPropertyChanged diagnostics

Those of you that use INotifyPropertyChanged may have noticed it's easy to break the code if you choose to refactor/rename as the property name string does not get refactored.

Here is a mechanism to catch this problem at the implementation stage.

 

[code:c#]

#region Debugging Aides

/// <summary>
/// Warns the developer if this object does not have
/// a public property with the specified name. This
/// method does not exist in a Release build.
/// </summary>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public void VerifyPropertyName(string propertyName)
{
    // Verify that the property name matches a real, 
    // public, instance property on this object.
    if (TypeDescriptor.GetProperties(this)[propertyName] == null)
    {
        string msg = "Invalid property name: " + propertyName;

        if (this.ThrowOnInvalidPropertyName)
            throw new Exception(msg);
        else
            Debug.Fail(msg);
    }
}

/// <summary>
/// Returns whether an exception is thrown, or if a Debug.Fail() is used
/// when an invalid property name is passed to the VerifyPropertyName method.
/// The default value is false, but subclasses used by unit tests might
/// override this property's getter to return true.
/// </summary>
protected virtual bool ThrowOnInvalidPropertyName { get; private set; }

#endregion // Debugging Aides

#region INotifyPropertyChanged Members

/// <summary>
/// Raised when a property on this object has a new value.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged = (s, e) => { };

/// <summary>
/// Raises this object's PropertyChanged event.
/// </summary>
/// <param name="propertyName">The property that has a new value.</param>
protected virtual void OnPropertyChanged(string propertyName)
{
    this.VerifyPropertyName(propertyName);
    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));           
}

#endregion // INotifyPropertyChanged Members

[/code]

WPF ListViewItem Commands with MVVM pattern

If you're interested to see how to attach commands to listview items for use with an implementation of the MVVM pattern, have a look at this.

 

[code:c#]

<Style x:Key="Local_OpenEntityStyle"
           TargetType="{x:Type ListViewItem}">
        <Setter Property="acb:CommandBehavior.Event"
                        Value="MouseDoubleClick" />
        <Setter Property="acb:CommandBehavior.Command"
                        Value="{Binding ElementName=uiEntityListDisplay, Path=DataContext.OpenEntityCommand}" />
        <Setter Property="acb:CommandBehavior.CommandParameter"
                        Value="{Binding}" />
</Style>

[/code]

 

Here the command to be fired on the MouseDoubleClick event is set, the CommandParameter, will be the data object that we click on.

Windows special folders enum


Ever want to find out from c# where some "special" folders are located so you can use them in your desktop application?

Here's how.

[code:c#]

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)


[/code]

 

See  Environment.SpecialFolder enumeration for more locations.

Exposing OData with WCF Ria Services 4.0

I was just about to write up a little blog on the how and why of Exposing OData (Open Data Protocol) in WCF 4.0.
But seeming it being a Bank Holiday and, if you follow any of my posts, you'll know I don't spend a lot of time writing them up, they are more like mini blogs Laughing

So I found this excellent article by Brad Abrams

http://blogs.msdn.com/brada/archive/2010/03/16/silverlight-4-ria-services-ready-for-business-exposing-odata-services.aspx

I recommend you have a read because if you've never encounted OData before you'll be amazed how easy it is to expose and consume your domain service (expecially with Excel 2010)