Validation techneques?
-
I have decided that I need a better way of validating data. I currently have various objects that can validate information, for example:
if(TextBoxValidator.IsValidTextBox(tbText))...
Also I tend to use the Debug.Assert() methods often, making sure that null or invalid states of variables are shown. Its starting to get repetitive when I validate:
if(fileDetails.ToString() == string.Empty || map == null) return Common.ReturnResult.Failed;
if(map.StaticImages == null || map.Tiles == null) return Common.ReturnResult.Failed;
if(map.Infomation.Columns == 0 || map.Infomation.RootContentFolder == string.Empty || map.Infomation.Rows == 0 || map.Infomation.TileSize == 0)
return Common.ReturnResult.Failed;There must be a better way to validate methods input and such than this! :-D Any suggestions, Thanks :doh:
-
I have decided that I need a better way of validating data. I currently have various objects that can validate information, for example:
if(TextBoxValidator.IsValidTextBox(tbText))...
Also I tend to use the Debug.Assert() methods often, making sure that null or invalid states of variables are shown. Its starting to get repetitive when I validate:
if(fileDetails.ToString() == string.Empty || map == null) return Common.ReturnResult.Failed;
if(map.StaticImages == null || map.Tiles == null) return Common.ReturnResult.Failed;
if(map.Infomation.Columns == 0 || map.Infomation.RootContentFolder == string.Empty || map.Infomation.Rows == 0 || map.Infomation.TileSize == 0)
return Common.ReturnResult.Failed;There must be a better way to validate methods input and such than this! :-D Any suggestions, Thanks :doh:
One way is to avoid nulls in the first place, but if you need to check , here is a start, it is longer but clearer to read:
...
bool Success(footype map) //This isn't the best name
{
if(map.StaticImages == null)
return false;
if(map.Tiles == null)
return false;
if(map.Information.RootContentFolder == "")
return false;
if(map.Infomation.Rows == 0)
return false;
if(map.Infomation.TileSize == 0)
return false;
}public Foo()
{
if(fileDetails.ToString() == string.Empty)
return Common.ReturnResult.Failed;
if(map == null)
return Common.ReturnResult.Failed;
if(!Success(map))
return Common.ReturnResult.Failed;
return Common.ReturnResult.OKorWhatever;}
Can you change the
map
object? If so theSuccess
Status is probably better off there as a property, you could also break this down into further sub-properties such asHasImages
,HasInformation
etcSort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-
A Dead ringer for Kate Winslett[^] -
I have decided that I need a better way of validating data. I currently have various objects that can validate information, for example:
if(TextBoxValidator.IsValidTextBox(tbText))...
Also I tend to use the Debug.Assert() methods often, making sure that null or invalid states of variables are shown. Its starting to get repetitive when I validate:
if(fileDetails.ToString() == string.Empty || map == null) return Common.ReturnResult.Failed;
if(map.StaticImages == null || map.Tiles == null) return Common.ReturnResult.Failed;
if(map.Infomation.Columns == 0 || map.Infomation.RootContentFolder == string.Empty || map.Infomation.Rows == 0 || map.Infomation.TileSize == 0)
return Common.ReturnResult.Failed;There must be a better way to validate methods input and such than this! :-D Any suggestions, Thanks :doh:
-
I have decided that I need a better way of validating data. I currently have various objects that can validate information, for example:
if(TextBoxValidator.IsValidTextBox(tbText))...
Also I tend to use the Debug.Assert() methods often, making sure that null or invalid states of variables are shown. Its starting to get repetitive when I validate:
if(fileDetails.ToString() == string.Empty || map == null) return Common.ReturnResult.Failed;
if(map.StaticImages == null || map.Tiles == null) return Common.ReturnResult.Failed;
if(map.Infomation.Columns == 0 || map.Infomation.RootContentFolder == string.Empty || map.Infomation.Rows == 0 || map.Infomation.TileSize == 0)
return Common.ReturnResult.Failed;There must be a better way to validate methods input and such than this! :-D Any suggestions, Thanks :doh:
What about an "
IValidable
" interface defining a method "Validate()
"? When "map" and its properties implement it, you could simply callmap.Validate()
which would encapsulate that logic there. -
What about an "
IValidable
" interface defining a method "Validate()
"? When "map" and its properties implement it, you could simply callmap.Validate()
which would encapsulate that logic there.Thanks for the reply,the problem is that I would have allot of validation objects everywhere, which wouldn't be very fun to use...
-
I have decided that I need a better way of validating data. I currently have various objects that can validate information, for example:
if(TextBoxValidator.IsValidTextBox(tbText))...
Also I tend to use the Debug.Assert() methods often, making sure that null or invalid states of variables are shown. Its starting to get repetitive when I validate:
if(fileDetails.ToString() == string.Empty || map == null) return Common.ReturnResult.Failed;
if(map.StaticImages == null || map.Tiles == null) return Common.ReturnResult.Failed;
if(map.Infomation.Columns == 0 || map.Infomation.RootContentFolder == string.Empty || map.Infomation.Rows == 0 || map.Infomation.TileSize == 0)
return Common.ReturnResult.Failed;There must be a better way to validate methods input and such than this! :-D Any suggestions, Thanks :doh: