Glass-lu/Glass.Sitecore.Mapper — pull request #13 by codeputty (2012-06-15 17:38:05+00:00)
Title: generic handler for nullable types

On our project, we have nullable types in our domain model. int? DateTime? etc.

I created this wrapper class to handle the mapping for any nullable type.

public class SitecoreFieldNullableHandler : AbstractSitecoreField
where THandler : AbstractSitecoreField, new()

TNullable is the nullable type you want to handle and THandler is the existing handler you want to use for non-null cases.

To wire it up, you add it to your loader:

var handlers = new AbstractSitecoreField[] {
new SitecoreFieldNullableHandler(),
new SitecoreFieldNullableHandler(),
new SitecoreFieldNullableHandler(),
/* etc */
};
foreach (var handler in handlers)
{
loader.AddDataHandler(handler);
}