im in ur web, enriching ur code

 
 

April 2007 Entries

Filtering PropertyChanged events

Recently, while working on a CMS project, I had the need to update a part of the UI if certain values of my business object changed. Basically, the scenario was that if the name of a page changed, the navigational sitemap should also update to reflect the change.

The objects in our business library implement the INotifyPropertyChanged, so it's simple enough to subscribe to a PropertyChanged event. But I only wanted to determine if a select few of the properties changed so that I could trigger the display update. One way is to subscribe to the object's PropertyChanged event and simply write an if .. else or switch statement based on the property name being passed along.

Another was is to encapsulate the same functionality into a filter object; and that's just what I did by building the PropertyChangeFilter to pass those pesky PropertyChanged events through.

How to use the PropertyChangeFilter

/// instance of a MyObject business object that 
/// implements INotifyPropertyChanged
MyObject myObject = new MyObject();
 
/// create the PropertyChangeFilter to watch myObject
/// for changes in the Name and Title properties
PropertyChangeFilter filter = new PropertyChangeFilter( myObject, "Name", "Title" );
 
// make the necessary changes to the object
myObject.Name = nameTextbox.Text;
myObject.Title = titleTextbox.Text;
/// .. snip other property assignments
myObject.Save();
 
if( filter.HasChanged )
{
    // handle changes to UI, clear a cached item, etc...
}

In the above example, we simply test to see if one of the properties we were listening for has changed. If either the 'Name' or 'Title' changed, the filter's HasChanged property would have been updated to suit.

Passing events through the filter

The PropertyChangeFilter also implements the INotifyPropertyChanged interface, you can also subscribe to the PropertyChanged events of your object by using the PropertyChangeFilter as a proxy. So instead of getting all the PropertyChanged notifications, you get the ones your interested in.

In this example, which we follow on from our code above, we subscribe to the filters PropertyChanged event, which is raised only when the watched object's Name or Title change.

filter.PropertyChanged += delegate( object sender, PropertyChangedEventArgs e )
{
    Console.WriteLine( "The '{0}' property has changed!", e.PropertyName );
};

It's worth noting that the sender argument is the object the event originated from and not our filter i.e. the sender is our instance of myObject.

Getting a log of the changed properties

One last feature, if you could find a use for it, is to enable the TrackChanges property on the filter like so:

filter.TrackChanges = true;
myObject.Name = "Mouse";
myObject.Lives = 1;
myObject.Name = "Cat";
myObject.Lives = 9;
myObject.Title = "I R Weasel";
myObject.Name = "Amoeba";
 
string[] changes = filter.Changes.ToArray();
Console.WriteLine( "Log of properties changed: {0}", string.Join( ", ", changes ) );
// prints "Log of properties changed: Name, Name, Title, Name"

That's about all there is to it. It's simple, but yet another little C# Vitamin to help you along your way. You can download the full source code file for the class or the zip which includes a small demo console application.

kick it on DotNetKicks.com

posted @ Monday, April 30, 2007 9:31 PM | Feedback (0)
Filed Under [ C#, Utilities, .NET, Source Code ]

Out with Blogger. In with Subtext.

Although only a new blogger myself, my choice for entry into this realm wasn't the best considering my background. I chose google's Blogger service to get started quickly and just get posting. It worked too, with practically no set up, I could start writing and the results were automatically published via FTP to my location of choice. Awesome.

But by the second or third post, I found that I was becoming increasingly frustrated with the editor provided. Being a web developer that took up early with the 'web standards' cause, I have a fundamental requirement to have a document (such as a blog post) that is semantically correct. So Blogger's <span> with font-size 130% etc to replace H1 through H6 just didn't cut it; and doing everything in 'source view' is way too hard-core these days.

That, coupled with my RSS being published as ATOM several times, and large blogging organisation being somewhat difficult to contact or give feedback to, was enough to sap my desire to use it.

So I did what any self respecting web developer would do and decided to build one myself. And because I love developing, and had some new business logic techniques I was keen on using, not to mention that I like to have things running lean and smooth, building it was going to be Awesome! Then, as it has happened before with other good intentions just like this one, real work and a severe lack of free time, convinced me out of it.

So I did what any busy self respecting web developer would do. I chose a popular open source project that is built on my development platform of choice. Something proven and that I could possibly contribute to. So now I run Subtext, have a proper blog design thanks to Rina, and can have all the H1's through H6's that I can poke a stick at - ain't it grand! If the worst comes to worst, I can always pop over and leave Phil a message. The last time I submitted a bug report, he fixed it that night! Now that's Awesome!

It really goes to show that not having the right tools goes a long way to sapping your creativity. And without further adieu, welcome Subtext, now I can get on with posting the backlog of C# Vitamins I'm accumulating.

kick it on DotNetKicks.com

posted @ Saturday, April 28, 2007 10:13 PM | Feedback (6)
Filed Under [ Off Topic ]

 

About C# Vitamins

Dave has been working in the industry for around 10 years, and has a focus on Javascript, C#, ASP.NET and SQL Server web development; not to mention being a standards driven type of guy.

C# Vitamins is the result of a lot of years hard work in the web industry and a desire to share with the community; and if it was traced back far enough, you might say it might not have existed if he hadn't taken such an interest in id Software's original Quake.

Related Links

Below is a list of related links of Dave's other sites.