WCF How to specify max and min value for a data member? [modified]
-
Say, if I have a data member which is string, I want to specify that its length need to be between 6 to 9 characters, or a data member is positive integer which needs to be less than 1000. Are there any attributes I can apply to data members to do this rather than validate this sort in the code? Thanks.
-
Say, if I have a data member which is string, I want to specify that its length need to be between 6 to 9 characters, or a data member is positive integer which needs to be less than 1000. Are there any attributes I can apply to data members to do this rather than validate this sort in the code? Thanks.
I think you need to validate the business object.
Cheers, Karl
» CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home PageJust a grain of sand on the worlds beaches.
-
Say, if I have a data member which is string, I want to specify that its length need to be between 6 to 9 characters, or a data member is positive integer which needs to be less than 1000. Are there any attributes I can apply to data members to do this rather than validate this sort in the code? Thanks.
Karl's exactly right. You wouldn't apply the rules on the data member because this is just a contract - it's not a business rule, because your rules may change, but your contract should remain the same. I would also add a check in your client code as well.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Say, if I have a data member which is string, I want to specify that its length need to be between 6 to 9 characters, or a data member is positive integer which needs to be less than 1000. Are there any attributes I can apply to data members to do this rather than validate this sort in the code? Thanks.
As of right now there is no native Attribute-based method of doing this. However, it is not difficult to create your own framework and set of attributes to encapsulate this behavior. Soon (well more like next year), Microsoft will be releasing the RIA services framework for Silverlight. It includes an attribute based method of providing validation. Eventually, it will be made available for WPF. If you have a more pressing need, this is something I've been looking into and could be convinced to accelerate my process on it. I've been a bit stagnant lately and need to get my finger on the keys (outside of work). Let's connect and I can get information on what you would need a validation framework to do and how you'd like to use it.
-
Karl's exactly right. You wouldn't apply the rules on the data member because this is just a contract - it's not a business rule, because your rules may change, but your contract should remain the same. I would also add a check in your client code as well.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
As of right now there is no native Attribute-based method of doing this. However, it is not difficult to create your own framework and set of attributes to encapsulate this behavior. Soon (well more like next year), Microsoft will be releasing the RIA services framework for Silverlight. It includes an attribute based method of providing validation. Eventually, it will be made available for WPF. If you have a more pressing need, this is something I've been looking into and could be convinced to accelerate my process on it. I've been a bit stagnant lately and need to get my finger on the keys (outside of work). Let's connect and I can get information on what you would need a validation framework to do and how you'd like to use it.