Thursday, February 28, 2008

Unit Test Object Properties

Unit testing of entities is something that never gets done very well for a number of reasons, the objects are pretty simplistic, developers get board righting that type of code, etc. It seems that one area unit tests of this type of business object fails is in testing their properties. The test covers setting and retrieving the properties but what about Business Objects with CRUD behavior? What about testing Data Access layer save functionality. Just because the save method does not error on you does not mean the save works. To really have a good unit test you need to save the object and then get the object again and compare all the properties to make sure each property was saved as expected.

To test each property I often see developers writing code that does a one to one compare on the two objects (the object they tried to save and the object that was saved). This gets boring really quickly and can explain why a lot of the time you do not see full code coverage. To help make this easier I recently wrote  a simple piece of code to handle all this property comparison. With the code below you can simply pass it two objects of the same type and it will do a property compare on each object to make sure each matching property has the same value.

public void CompareObjectProperties(Object newOjbect, Object dbObject)
    PropertyInfo[] propInfo = newOjbect.GetType().GetProperties();

    foreach (PropertyInfo prop in propInfo)
        PropertyInfo dbPropinfo = dbObject.GetType().GetProperty(prop.Name);
        string newProp = prop.GetValue(newOjbect, null).ToString();
        string dbProp = dbPropinfo.GetValue(dbObject, null).ToString();

        // Make sure each value was saved to the database correctly
        Assert.AreEqual(newProp, dbProp, "The value on the object did not match the value saved to the second object");

Tuesday, February 19, 2008

WPF Improvements on the way

Found this information today on Scott Gu's blog. All great news about WPF improvements that are on their way.

WPF Performance Improvements

This summer we are also planning to release a servicing update to WPF that includes a bunch of performance optimizations that improve its text, graphics, media and data stack. These include:

- Moving the DropShadow and Blur bitmap effects, which are currently software rendered, to be hardware accelerated (making them many times faster). The APIs for these effects will stay the same as they are today (which means you do not need to change any code nor recompile your apps to take advantage of these improvements).

- Text scenarios, especially when used in Visual and DrawingBrush scenarios, will be substantially faster. The APIs for these scenarios also stay the same (which means you do not need to change any code nor recompile to take advantage of the performance improvements).

- Media and video performance scenarios will also be much faster (also no need to change any code nor recompile to take advantage of the improvements).

- We’ll be including a new WriteableBitmap API that enables real-time bitmap updates from a software surface. We’ll also be adding support for a powerful new effects API that enables you to build richer graphics scenarios.

- We’ll also be including new data scalability improvements that can be leveraged for data editing scenarios. These include container recycling and data virtualization support that make it easier to build richer data visualization controls.

WPF Control Improvements

Later this year we are also planning to release a number of new controls for WPF.  Included in the list we are working on are DataGrid, Ribbon, and Calendar/DatePicker controls.

VS 2008 WPF Designer Improvements

We are also planning to release a servicing update of VS 2008 that includes a number of feature additions to its WPF designer. These include event tab support within the property grid for control events, toolbox support within source mode, and a variety of other common asks and improvements.

Thursday, February 14, 2008

WPF Validation

I recently had to build out a validation solution for a WPF project I was working on. For those of you that have not worked with WPF (you are missing out) validation works differently. At first I was unsure about the way validation works in WPF and it seemed a little incomplete. I still believe it is incomplete, mainly because there is no built in way to do complete form validation unless you implement a custom solutions. This brings us to the point of this post. I recently wrote up an example of a few different ways to do complete form validation using WPF. I created a Validation manager that allows me to do this. The validation manager utilizes the creation of custom validation rules. You can download the whole paper and the example solution here. The paper includes validation approaches other developers have done as well.

The validation manager that is used in this approach allows you to handle validation at the form level and allows you to have your validation logic separated from your business objects. This is one thing the other approaches do not let you have. The validation manager is also not coupled at all with the DataContext of the page so it gives you more flexibility. The IDataError approach Paul Stovell uses can be helpful but is tightly coupled with the DataContext. There is also one other very nice feature the Validation Manager I created gives you. It gives you the ability to invalidate a field on the page programmatically. Even if all the custom validation rules that are bound to in pass you can still use the Validation manager to invalidate the field and put an error message on it. This gives you great extensibility to build out a validation framework and use the validation manager to handle invalidating whatever field needs to be invalidated.

Tuesday, February 12, 2008

CollectionView Filter

For those of you who have worked with data collections in WPF if you have probably learned how helpful a CollectionView can be. CollectionViews let you filter, group and sort a collection of data without any requiring of the data. Setting a CollectionView up to filter or group data is actually pretty easy. Here is an example of how you would setup a Collection into a CollectionView to be filtered.

ListCollectionView _view; 

public Window1() 


     string[] myValues = new string[] { "Red Car", "Red Truck", "Blue Car", "Yellow Truck" }; 
     _view = new ListCollectionView(myValues); 
     this.DataContext = _view; 

private void FilterTrucks(Object sender, RoutedEventArgs args) 

	if (_view.CanFilter && (ShowTrucks.IsChecked == true)) 
		_view.Filter = new Predicate<object>(IsValueTruck); 

		 // Remove the filter 
		 _view.Filter = null; 


public bool IsValueTruck(Object value) 

     return (value.ToString().Contains("Truck")); 

You can see that when the "Show only Trucks" checkbox is check the list is automatically filtered to show only items that are trucks. You do not need to write any code that refreshes the UI this is all handled for you by the WPF binding and the ListCollectionView. You can try changing the ListCollectionView to just a CollectionView. If you do this you will see that the UI does not refresh the list when the check box is selected. This is because CollectionView itself does not handle the UI level view update. You can use ListCollectionView, CollectionViewSource or BindingListCollectionView (this type is used for binding and filtering objects like datasets) to filter and bind to lists. We have covered ListCollectionView binding and filter. In future posts I will cover the other types as well as how to set up grouping and sorting.

Monday, February 11, 2008

New Code Site

I have been creating a lot of coding examples on my personal live spaces account. I have recently come to the realization that spaces just does not give me enough flexibility to really create good examples. It is also mixed in with all my other thoughts on life. Because of that I have decided to create a new blog dedicated to thoughts on code. I plan on uploading different code samples here that will be on different topics such as WPF, Silverlight, LINQ and more. It is really for my own enjoyment and to give me a place to store examples I create on how to do things. Hopefully it will benefit others out there as well. I will be spending time porting previous examples from my spaces account to here. Part of this port will include layout the examples as well as including links to the source code for all projects.

I hope you all enjoy!