SmartDate, SetPropertyConvert, and PropertyLoadException: invalid cast from string

SmartDate, SetPropertyConvert, and PropertyLoadException: invalid cast from string

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


decius posted on Thursday, December 08, 2011

I'm trying to gain a better understanding of the concept design behind using SetPropertyConvert<SmartDate, string>.

It doesn't make sense to me why SetPropertyConvert throws and exception if the string can't convert to a date - for some reason I'm expecting the "ill formatted" string value would just not change the pre-existing date value... 

So, since the CSLA design instead throws an exception, how should one handle the scenario of when you bind the string property of the SmartDate to a text box? If the user keys in an invalid date, an exception will be thrown - is there some sort of event that needs to be subscribed to?

Can someone point me to the book and page number explaining this design decision and how to handle it?

 

RockfordLhotka replied on Thursday, December 08, 2011

This is no different than when you bind a text box control to a numeric property and then enter something like "abc". The behavior is intentionally comparable.

In WPF/SL this is solved by telling the binding to handle binding exceptions. I don't remember if there was a decent solution in Windows Forms - perhaps not?

JonnyBee replied on Thursday, December 08, 2011

You can hook into Windows Forms databinding but it's not obvious.

Similar to a ValueConverter in WPF/SL you can hook into the Parse/Format events to do your own value conversion.

You can hook into the Binding.BindingComplete event to do your own error handling
http://msdn.microsoft.com/en-us/library/system.windows.forms.binding.bindingcomplete.aspx 
or hook into BindingSource.BindingComplete that is called for all controls that are attached to the BindingSource.

Events on the BindingSource are visible in the property designer and the "suggested" hook from Microsoft.

decius replied on Friday, December 09, 2011

Thanks Rocky & JonnyBee for your help. This solves my question about binding.

I do have one other question though. A lot of the time, in the DAL I will use the SetPropertyConvert method to load values from the database (or where ever).

In the event that somehow the data has been dirtied with "ill formatted" data, I suppose best practice is to NOT to use the SetPropertyConvert in the DAL and to convert data manually?

 

For example, suppose the business object is being loaded from a file format like a csv or something and a string conversion is necessary in the DAL from string to smartdate. Would a simple helper method be required for me to write like below or is there something in the framework I'm not tapping into here?...

private void SetSmartDateProperty(PropertyInfo<SmartDate> p, string date)
        {
            DateTime d;
            if (DateTime.TryParse(date, out d))
                SetPropertyConvert(p, d);
        }

 

EDIT:

Another approach might be to use string values for all my properties as strings, and then expose a property with GetPropertyConvert for the date values needed during operations. That seems like a lot of work though...

decius replied on Monday, December 12, 2011

Am I going about this completely the wrong way? It just seems like there's something I'm not utilizing in the framework for this kind of situation here...

decius replied on Friday, December 16, 2011

Anyone? Perhaps my question is phrased poorly. 

My problem is that I'm reading dates from a datastore (a csv document) that are string values and I am wanting to store this data in my objects as a SmartDate.  I can't use SetPropertyConvert because it throws an exception, so do I need to manually convert values like this myself with a method like the above or is there something in the framework that I'm not understanding/using?

Any help is appreciated, thanks again.

RockfordLhotka replied on Friday, December 16, 2011

There is nothing in CSLA for parsing text files, no.

But SmartDate has comparable parse and tryparse methods to DateTime, and you should be able to use those to help parse the sting values.

Copyright (c) Marimer LLC