SmartDate validation - want broken rule for invalid date rather than exception

SmartDate validation - want broken rule for invalid date rather than exception

Old forum URL: forums.lhotka.net/forums/t/2339.aspx


JustinThirkell posted on Tuesday, February 13, 2007

I'm sure someone has overcome this but I can't find anything in the forums (except for the same problem - no solution though).

I've got a SmartDate and when I set the .Text to an invalid date it throws an ArgumentException.  I understand that this is documented and by design. 

But... if someone sets an invalid date, surely it would be better to add a broken rule rather than throw an exception?!  Because I'm exposing my business objects through some services I'm just dealing with string as a transport type and I can't trust that the service consumer is going to constrain the UI to only allow valid date formats. 

Because an exception is being thrown I get none of the goodness with the broken rules functionality - no property name, no invalid value, etc to tell the client what really went wrong.  At the moment I have to parse the exception message just to get the name of the property to send back to the service consumer... yuch.

My thinking is that I need to extend SmartDate so that any invalid cast in the .Text setter causes the storage of the invalid value (for later use in error messages) and creates a new broken rule.  Is this the best way to handle it?

Thanks

Justin

JoeFallon1 replied on Wednesday, February 14, 2007

Rather than deal with all of that I decided to just set invalid dates to blank. I also check for SQL Server smalldattime dates.

Here is the modified code:

'JF 10/12/06 - The SmartDate Structure cannot be inherited so we need to change CSLA2 itself.
'If Value is not a date then set it to Date.MinValue so that "" will be returned.
'Effectively this blanks out invalid values in the UI.
'Also, ensure that Value is in the range 1/1/1900 to 6/6/2079 else SQL Server smalldatetime value is violated.

Public Shared Function StringToDate( _
 
ByVal value As String, ByVal emptyValue As EmptyValue) As Date

If Len(value) = 0 Then
 
If emptyValue = SmartDate.EmptyValue.MinDate Then
   
Return Date.MinValue
 
Else
   
Return Date.MaxValue
 
End If
ElseIf IsDate(value) Then
 
'JF 10/12/06 - This is the change:
 
If CDate(value) >= CDate("1/1/1900") AndAlso CDate(value) <= CDate("6/6/2079 11:59 PM") Then
   
Return CDate(value)
 
Else
   
Return Date.MinValue
  
End If
Else
 
Select Case LCase(Trim(value))
   
Case My.Resources.SmartDateT, My.Resources.SmartDateToday, "."
     
Return Now
   
Case My.Resources.SmartDateY, My.Resources.SmartDateYesterday, "-"
     
Return DateAdd(DateInterval.Day, -1, Now)
   
Case My.Resources.SmartDateTom, My.Resources.SmartDateTomorrow, "+"
     
Return DateAdd(DateInterval.Day, 1, Now)
   
Case Else
      'JF 10/12/06 - This is the change:

       If emptyValue = SmartDate.EmptyValue.MinDate Then
        
Return Date.MinValue
     
Else
       
Return Date.MaxValue
     
End If

     ' 'Throw New ArgumentException(My.Resources.StringToDateException)
   
End Select
End If
End Function

Joe

 

 

kenb replied on Wednesday, September 30, 2009

I want my business objects to behave the way Justin describes them.  I'm using CSLA 3.0.4.

I'm looking for advice on my approach.  I've already created a SmartNullable(T) which is based on Nullable(T) with the addition of allowing string-value assignment and tracking invalid values/state.

A validation rule will be able to ask the structure if it has an invalid value and provide an appropriate description (e.g. 'hello world' is not a valid date.)

Does this seem like a well-designed solution?

Copyright (c) Marimer LLC