Why SmartXYZ.DBValue?

Why SmartXYZ.DBValue?

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


SonOfPirate posted on Sunday, June 10, 2007

I have a two part question - one for clarification and one for education.

I understand that the various SmartXYZ types that have been created expose a DBValue property to overcome the "No mapping exists from object type Csla.SmartXYZ to a known managed provider native type" exception.  But why go this route and not implement whatever it takes to provide this mapping so that the type can be used outright like their System counter-parts?

And with that in mind, what is it that the framework classes do or implement that allow them to be mapped?  Is it via an interface, a type converter or something else?

I implemented IConvertible.GetTypeCode and return System.TypeCode.DateTime then tried to use the SmartDate type directly but it still failed.  Placing a breakpoint in GetTypeCode never broke execution, telling me this isn't the key.  So how do the built-in classes do it?

 

SonOfPirate replied on Tuesday, June 12, 2007

No one has any feedback/thoughts on this?

 

Brian Criswell replied on Tuesday, June 12, 2007

The .DBValue property is there to provide a DBNull to the DbCommand when .Empty == true.  Without it, I imagine that there is not much reason to have the SmartXYZ in the first place...

I imagine that the Data Providers are what map the built in .NET types to the data types of the data provider.

SonOfPirate replied on Tuesday, June 19, 2007

Sorry for the delay responding...

I realize what purpose the DBValue properties serve, what I would like to know is if they are really necessary or if some other means is available to accomplish the same requirement.

In tracing through the SqlClient classes using ILDASM to see what is done with property values, I found that there is a GetMetaDataFromType method that appears to use the specified type's GetTypeCode() value in a switch statement (Select Case in VB) to translate the type to the applicable SqlType.  If the return type code is not one of the switch cases, the aforementioned exception is thrown.

It appears, but I'm not sure, that there is a second test to validate the type of the specified object with an exception thrown if it doesn't match.  For instance, if GetTypeCode returns TypeCode.DateTime there is a test to verify that (obj is System.DateTime).  This may be where the problem lies - even though I still don't get a hit when I set a breakpoint in the GetTypeCode method for the SmartDateTime struct.  So, can we return the Object type code instead and circumvent this?

Or am I barking up the wrong tree entirely?

 

Copyright (c) Marimer LLC