Framework Design Guidelines: Dependency Properties
Continuing in our weekly blog post series that highlights a few of the new additions to the Framework Design Guidelines 2nd edition.. This content is found in the Dependency Properties section of Chapter 9: Common Design Patterns. Phil offers some great additions to the base pattern.
The following guidelines describe details of Dependency Property dependency property design.
DO inherit from DependencyObject, or one of its subtypes, when implementing Dependency Properties. The type provides a very efficient implementation of a property store and automatically supports WPF data binding.
DO provide a regular CLR property and public static read-only field storing an instance of System.Windows.DependencyProperty for each Dependency Propertydependency property.
public class TextButton : DependencyObject {
public string Text {
get { return (string)this.GetValue(TextProperty); }
set { this.SetValue(TextProperty,value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text",typeof(string),
typeof(TextButton));
}
}
DO implement Dependency Propertiesdependency properties by calling instance methods DependencyObject.GetValue and DependencyObject.SetValue.
public class TextButton : DependencyObject {
public string Text {
get { return (string)this.GetValue(TextProperty); }
set { this.SetValue(TextProperty,value); }
}
public static readonly DependencyProperty TextProperty = …
}
DO name the Dependency Propertydependency property static field by suffixing the name of the property with “Property.”.
The first parameter to the DependencyProperty.Register method should be the name of the wrapper property.
public class TextButton : DependencyObject {
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text",typeof(string),
typeof(TextButton));
}
Comments
Anonymous
March 02, 2009
PingBack from http://www.clickandsolve.com/?p=16927Anonymous
March 02, 2009
Continuing in our weekly blog post series that highlights a few of the new additions to the FrameworkAnonymous
March 02, 2009
Thank you for submitting this cool story - Trackback from DotNetShoutoutAnonymous
March 02, 2009
Erm...why can't dependency objects be datacontexts in silverlight (and work properly) when INotifyPropertyChanged implementations can? I like the dependency approach but would like it more if silverlight supported dependency object data contexts.Anonymous
March 02, 2009
If you use PostSharp you will not need any of the setter/getter messAnonymous
March 03, 2009
This new "guideline" addition left me wondering if the FDG 2 has gotten polluted with too many implementation nuances of transient technologies. I gladly purchased and read the first FDG book, because it provided such a concise and clear guidance on the fundamental aspects of the general design and left out numerous technology-specific details. This was a welcome break from the avalanche of "click on menu item xyz" books that tell you nothing about the underlying concepts and instead load your brain with a slew of mouse click instructions that will be largely irrelevant in the version 2.x of the same product/technology. I feel that these latest "guidelines" belong in the MSDN library describing details of the WPF technology, not in the timeless FDG book. If I pick up your book 5 or 10 years from now, the nitty-gritty WPF-specific suggestions will most likely have no real value to me, but the class vs. interface discussion or the exception handling topics hopefully would still make a lot of sense. Don't get me wrong: I still love the book and permanently keep FDG 2 on my Safari bookshelf. I believe that EVERY developer should use this book as a daily reference.Anonymous
March 03, 2009
I found it interesting you used UI controls in your example. Given the class browser doesn't support subclassing UI controls and there are also various issue in coding them out like you have that need to be worked around. Furthermore WPF which I assume is the direction microsoft is taking, at least today, composition UI controls aren't subclassed which IMHO is a huge issue.Anonymous
March 04, 2009
The comment has been removedAnonymous
March 05, 2009
The comment has been removedAnonymous
March 05, 2009
The comment has been removedAnonymous
March 22, 2009
Thanks for sharing the guidelines. In my opinion, however, one essential guideline is missing: DO NOT implement any additional logic in dependency property get and set accessors apart from calling instance methods DependencyObject.GetValue and DependencyObject.SetValue.Anonymous
May 04, 2009
The comment has been removedAnonymous
May 04, 2009
@cwalina @We have many guidelines that we developed internally, that are not even close to be timeless, but I think they can be very useful to the community. So publish a .NET 3.0 Foundation Class Libraries Usage Design Guideleins book,