by mgordon
28. June 2007 03:42
As I mentioned in a previous post, I'm relatively new to using SubSonic. I'm working on a web application and the lure of not having to write a ton of boiler plate database access code lead me to use this technology. Recently, I encountered a problem with it, however, and though I'd pass on what happened and the solutions I found.
I had been happily using subsonic within the workflow of the normal Asp.Net page lifecycle. I've even been using some callbacks from pages with no problems at all. It was when I started adding some Asp.Net Ajax functionality to the site that I encountered problems. I set up a call to a webservice from the browser. Within this web service method, I was using SubSonic to populate one of the Active record classes it built and was, then, passing that class back as the return value. When I executed the code, I received an error message saying that an instance of the class I was returning could not be created because one of its properties was inaccessible.
After some investigation, I discovered that the problem was occurring when the object I was returning was being serialized to be passed back to my page. When SubSonic generates the active record classes for you, it generates a property for each column on the table that the class corresponds to. If that column is nullable, the property's type is set as a nullable type, as well. This makes perfect sense to me. However, the XmlSerializer cannot serialize nullable types. This means that if your table has nullable columns in it, the class generated by SubSonic cannot be serialized.
So, I modified my table such that none of the columns were nullable, regenerated my classes and tried to execute the code, again...same error - different property. This time, it seems, the offending property was one that is defined in the AbstractRecord class from which my generated class derives. The "get" was marked as protected and, as such, could not be read by the serializer. I had found some posts in the forums on the SubSonic site that pointed out the problem and the recommendation was to modify the class in the SubSonic source code, recompile the project and then regenerate your classes. Thankfully, though, the problem had already been addressed by the latest version (2.0.1a) of the project and this change is not necessary if you upgrade.
It was a scary moment for me, but now that I understand the limitations of the XmlSerializer in regard to nullable types I can code around the issue and not encounter this problem again.