Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Validate A Class
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01458686
Message ID:
01458816
Views:
41
>>>>What's the best way to validate that all required properties on a class are completed correctly? The properties will eventually be
>>>>mapped to UI fields, but the class itself is not visual. It's possible to instantiate the class, set some or all properties, and then call
>>>>methods on it. I want to ensure that all required properties are complete before certain methods are run.
>>>>
>>>>The question is what to do if a required property is not set or is set wrong. Throw an exception?
>>>
>>>The best route is to catch stuff like this in the UI where possible so that, for example, a button that runs a method isn't enabled until all rewuired property data is present.
>>>
>>>But is this for WinForms or ASP.NET since they have very different options ?
>>
>>I'm not sure I'm in agreement with you and Bonnie as to where to put the validation code, and here's why. What if you get all the validation code complete and your boss decided to rebuild the app from WinForms to ASP.Net? Now you have you move all the validation code.
>
>I didn't mean to imply that no validation should be done in other layers - just that UI should be the first stop. This is probably more true with ASP.NET - stopping any wasted postbacks has to be a good thing.
>
>>My question was more to the issue of how to deal with a property that isn't set or is incorrect? Do you throw an exception, or maybe
>>set some property that tells what the issue is? Then, any other class or UI that's using is can check it.
>>
>>If you create a cusom exception for each required property, then throw the error, your UI's error handling scheme would catch it.
>
>Anyway, for WinForms look at the ErrorProvider class - this can be used across layers in the way you want
>For ASP.NET there are several available validation controls (such as the RequiredFieldValidator) that will automatically generate server and client side checking but they are more tied to UI.
>FWIW my vote would be against throwing exceptions for invalid or missing data.....

Why not? Inquring minds wanna know....
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform