Validating a property on Set
-
My ability to google and comprehend things today appears to be not what it should - I've been up since 4am so go easy on me :) Anyway, suppose I have a very simple class with a single property:
public string SiteName {
get{return siteName;}
set{siteName = value;}
}Now SiteName has some very specific format requirements (ie not longer than 12 characters, first 4 chars are alpha only for example). Within the setter I can do some validation with a regex or whatever, and for validating this single property everything is great. It also seems to make sense to me that this is where the validation should take place. However, it occurs to me that I might want to do a similar kind of validation on another property, perhaps even within in a separate class - or even multiple classes, and could get very messy with the same (or very similar) logic appearing all over the place. This looks to me like an ideal candidate for re-factoring... and this is where my googling has come unstuck! What I'm really after is an insight on to tried an trusted techniques for validating properties, and also whether my intent is actually going to cause bigger problems than they're worth. I had a quick read about Custom Attributes, but I was not particularly clear on how to hook these up to know when a property is being modified, or how they interact with the property in question... As always, any help greatly appreciated.
-
My ability to google and comprehend things today appears to be not what it should - I've been up since 4am so go easy on me :) Anyway, suppose I have a very simple class with a single property:
public string SiteName {
get{return siteName;}
set{siteName = value;}
}Now SiteName has some very specific format requirements (ie not longer than 12 characters, first 4 chars are alpha only for example). Within the setter I can do some validation with a regex or whatever, and for validating this single property everything is great. It also seems to make sense to me that this is where the validation should take place. However, it occurs to me that I might want to do a similar kind of validation on another property, perhaps even within in a separate class - or even multiple classes, and could get very messy with the same (or very similar) logic appearing all over the place. This looks to me like an ideal candidate for re-factoring... and this is where my googling has come unstuck! What I'm really after is an insight on to tried an trusted techniques for validating properties, and also whether my intent is actually going to cause bigger problems than they're worth. I had a quick read about Custom Attributes, but I was not particularly clear on how to hook these up to know when a property is being modified, or how they interact with the property in question... As always, any help greatly appreciated.
-
From the article: "Take note – in the screens above, the code that triggers the error to be shown is in the business object, not in the GUI. I didn't have to write a single line of GUI code – it's already done by data binding and IDataErrorInfo." Today's epiphany sorted :)
-
From the article: "Take note – in the screens above, the code that triggers the error to be shown is in the business object, not in the GUI. I didn't have to write a single line of GUI code – it's already done by data binding and IDataErrorInfo." Today's epiphany sorted :)
Yup - binding's so cool - and so bloody useful.
Deja View - the feeling that you've seen this post before.
-
ou install the utility and navigate to any url in CP from where you can post a message, the signature field is automatically filled with one of the quotes from an XML file. All you need to do is to write some quotes in an XML file either manually or using the CodePr