I am trying to implement a custom type handler in my project. I have
followed the documentation and the existing example in the NUnit tests.
Here is my type converter class:
<><><><><><>
public class CharBoolHandlerCallback : ITypeHandlerCallback
{
private const string DB_TRUE_VAL = @"Y";
private const string DB_FALSE_VAL = @"N";
public object ValueOf(string nullValue)
{
if (DB_TRUE_VAL.Equals(nullValue,
StringComparison.OrdinalIgnoreCase))
return true;
else if (DB_FALSE_VAL.Equals(nullValue,
StringComparison.OrdinalIgnoreCase))
return false;
else
throw new DataMapperException();
}
public object GetResult(IResultGetter getter)
{
if (getter.Value == null || getter.Value == DBNull.Value)
throw new DataMapperException();
string dbVal = Convert.ToString(getter.Value);
if (DB_TRUE_VAL.Equals(dbVal,
StringComparison.OrdinalIgnoreCase))
return true;
else if (DB_FALSE_VAL.Equals(dbVal,
StringComparison.OrdinalIgnoreCase))
return false;
else
throw new DataMapperException();
}
public void SetParameter(IParameterSetter setter, object parameter)
{
setter.Value = Convert.ToBoolean(parameter) ? DB_TRUE_VAL :
DB_FALSE_VAL;
}
public object NullValue
{
get { throw new InvalidCastException(); }
}
}
<><><><><><>
I have the following in my main sqlmap.config:
<><><><><><>
<alias>
<typeAlias alias="YesNoToBool"
type="Namespace.CharBoolHandlerCallback, AssemblyName" />
</alias>
<typeHandlers>
<typeHandler type="bool" dbType="Varchar" callback="YesNoToBool"/>
</typeHandlers>
<><><><><><>
And my result map looks like:
<><><><><><>
<resultMaps>
<resultMap id="EquipmentTypeResult" class="EquipmentTypeClass">
<constructor>
<argument argumentName="equipmentTypeId"
column="EQUIPMENT_TYPE_ID" />
<argument argumentName="description" column="DESCRIPTION" />
<argument argumentName="createdUser" column="CREATED_USERNAME"
select="GetUserByName"/>
<argument argumentName="createdDate" column="CREATED_DATE" />
<argument argumentName="updatedUser" column="UPDATED_USERNAME"
select="GetUserByName"/>
<argument argumentName="updatedDate" column="UPDATED_DATE" />
<argument argumentName="active" column="ACTIVE"
type="bool" dbType="Varchar"/>
<argument argumentName="legacy" column="LEGACY"
type="bool" dbType="Varchar"/>
</constructor>
</resultMap>
</resultMaps>
<><><><><><>
All of my iBATIS configuration loads. However, when I try to get an
object instance, iBATIS throws a SqlDataException trying to do the
conversion and the call stack has no indication that my custom type
converter was ever called. This same configuration works fine if I just
bring those fields over as strings and don't try to use the custom type
converter.
I have the same problem if I use the <result> tags with object
properties (instead of the constructor above).
Anyone know what I am doing wrong here?
Tony