(NOTE: For those of you that don't know the Repository Factory, unfortunately I'm not going to go into much detail in this post. Rather see codeplex for further details on the VS2005 version, and here regarding the VS2008 version. )
The following generated code was throwing a wobbly on "CastDBNull", as it seemed the factory didn't generate such a method.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using Microsoft.Practices.Repository;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.SqlClient;
namespace Community.Service.WCF.DataAccess.BusinessEntities.UserRepositoryArtifacts
{
///
///
internal sealed class GetISOCommunityUserByUserGCNFactory : ISimpleDomainObjectFactory
{
public User Construct(Database db, DbCommand command)
{
User user = new User();
user.IsFollowMeEnabled= CastDBNull.To<Boolean>(db.GetParameterValue(command, "IsFollowMeEnabled") , false);
return user;
}
}
}
When you look at the above code, its obvious its trying to handle scenarios where your database returns DBNull and where your business entity doesn't.
To fix this, I decided to implement my own "CastDBNull" method, so I goolged the method name and found a few usefull blogs on the topic, with one more notable than others due to its simplicity (see http://petesbloggerama.blogspot.com/2006/12/it-aint-null-until-i-say-its-null.html)
public static class CastDBNull
{
public static T To <>
{
return (value != DBNull.Value) ? (T)value : defaultValue;
}
}
Thats all for now folks!
1 comment:
Good post Rogues! We should also blog about that problem we had with the T4 template.
Post a Comment