Blog: 2015 February

Elegant method parameter validation with Code Contracts support

February 25, 2015, (updated on February 26, 2015), 1 comment, Software Development

Each C# developer knows the drill: Each method parameter has to be validated against null values, wrong value ranges or other contract constraints. When also validating using Code Contracts, the resulting code may take up most of the method body: public void Foo(object a, string b) { Contract.Requires(a != null); Contract.Requires(b != null); Contract.Requires(b != string.Empty); if (a != null) throw new ArgumentNullException("a"); if (string.IsNullOrEmpty(b)) throw new ArgumentNullException("b"); ... } Note: When Code Contracts runtime validation is enabled, the call to Requires may throw an exception and the own code for throwing an ArgumentNullException is not needed. However, often the […] Read more...

Tags: , , , , , ,

A transparent undo/redo mechanism for XAML applications

February 23, 2015, (updated on February 25, 2017), Software Development

In one of my recent projects, I had to implement an undo/redo mechanism for an observable object graph. The usual approach to this problem is to implement commands which operate on the object graph and provide Do() and Undo() methods. The problem was, that a great portion of the business logic was already implemented and was not command-based. This is why I had to find a solution which works transparently on the property change events of an object graph whose objects implement the INotifyCollectionChanged interface and use the ObservableCollection class for collection properties. The solution to the problem was to […] Read more...

Tags: , , , , ,

  Page 1 of 1