by justin
26. August 2010 11:37
For those of you who know what INotifyPropertyChanged is and also know what kind of a maintenance burden the OnPropertyChanged("Property") calls are I would like to share my favorite implementation of the ObservableObject.
public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SetAndNotify<T>(ref T field, T value, Expression<Func<T>> property)
{
if (!object.ReferenceEquals(field, value))
{
field = value;
this.OnPropertyChanged(property);
}
}
protected virtual void OnPropertyChanged<T>(Expression<Func<T>> changedProperty)
{
if (PropertyChanged != null)
{
string name = ((MemberExpression)changedProperty.Body).Member.Name;
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
Instead of using strings for property change notifications you use lambda expressions. This is beneficial because refactoring tools will pick this up instead of having to manually change the magic strings. It also will give you compile time errors if you have gotten something wrong. It also lets you maintain 1 statement setters in properties instead of multiple statements. Using the ObservableObject looks something like this:
public class Customer : ObservableObject
{
private string name;
public string Name
{
get { return this.name; }
set { this.SetAndNotify(ref this.name, value, () => this.Name); }
}
}
Simple and effective.
1f959512-2b86-4eb7-b6ba-1e48b6f7c673|1|4.0
Tags:
C# | .NET | WPF
by justin
6. August 2010 18:57
I have extended UnitDriven to provide support for (the current beta release of) Windows Phone 7. This means you can write a unit test that runs on .NET, Silverlight and Windows Phone. This is an Alpha release for now, hopefully if we get a few people to use it I can make a more stable release when the final version of the WindowsPhone7 SDK is released.
Download: http://unitdriven.codeplex.com/releases/view/50214
Here is a screenshot of UnitDriven running tests on the WindowsPhone7 emulator.

One interesting thing is that all I had to do to support this was link files from the Silverlight version of UnitDriven into a new Phone project. It all compiled and ran on the first try.
However, even though it ran it wasn’t actually usable. I had to create new versions of the Views to accomodate the smaller screen size and default layout differences (buttons are relatively bigger for example). Also the scroll bars are only visible while scrolling and you have to click and drag on a Circle or the text to actually do the scrolling.
Please feel free to comment on the UnitDriven forums if you have any comments or questions!
by justin
30. July 2010 04:58
We all new that the underscore ('_') was a valid character in C# identifiers but what I didn’t know is that it is legitimate to have nothing but underscore characters. I found this out while doing some code generation and identifier scrubbing. By that I mean if you are trying to create class names or namespaces based on user input you have to scrub out invalid characters and I chose to replace them with underscores.
I decided to mess around with this and I came up with a quick Morse Code API based on my misuse of underscore identifiers.
// SOS
Console.WriteLine("Transmitting: SOS");
Morse.Code._._._.o.___.___.___.o._._._.Play();
| Symbol |
Name |
| ._ |
dit |
| .___ |
dah |
| .o. |
Letter |
| .ooooo. |
Word |
So, SOS above is “dit dit dit, dah dah dah, dit dit dit”. It plays by using Console.Beep and letter and word boundaries use Thread.Sleep. Here’s a more complicated example taken from wikipedia!
Console.WriteLine("Transmitting: Morse Code");
Morse.Code.
// 1 2 3 4 5 6 7 8
//12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
//M------ O---------- R------ S---- E C---------- O---------- D------ E
//===.===...===.===.===...=.===.=...=.=.=...=.......===.=.===.=...===.===.===...===.=.=...=
___.___.o.___.___.___.o._.___._.o._._._.o._.ooooo.___._.___._.o.___.___.___.o.___._._.o._
// ^ ^ ^ ^ ^
// | dah dit | |
// symbol space letter space word space
.Play();
It actually sounds pretty good!
Download:
7f7a00a9-c475-4c1d-b4b3-1095165a1eb5|0|.0
Tags:
.NET | C# | Misc