Around these parts, iBatis.NET is the reigning ORM. No fear, just turn your iBatis swag on like I did and you’ll be okay. I’ve been working with this frameworkd for only a few months; with the help of what little Java I remember I’ve been able to get through a lot of online material to help me feel reasonably comforable with iBatis.NET. I’ve decided to post this in case anyone out there is interested in using this ORM and wonders why there is a lack of information out there for .NET projects.I am always open-minded about new tools/frameworks, my most valuable 5×1ll as a Software developer is, the ability to learn, everything else is a by-product of this 5×1ll and learning can’t happen with a closed-mind. If you’re interested, let’s get into it:
You: Need to implement an iBatis custom Type HandlerFor any number of reasons you have determined a Custom Type Handler is the way to go. In case you can’t make a decision, seeing how straight forward it is may help you get over that analysis-paralysis. It is either this or keep your Mapper to very simple types and then transform Object to Object. sometimes the database schema binds your hands. Here is an example of such an instance.
Scenario: Your Database uses Y or N values as indicator fields. Previously in 1.1 you would have to implement a custom type or if you haven’t taken a JP course or use the flyweight, you may even be tempted to use the Enum ( don’t do it - you will only face endless derision and ridicule ) . 2.0 has a little more swag to it thanks to Nullable types.
- Your Class definition
- Implementing ITypeHandlerCallback interface
- Register the new TypeHandler
- What your ResultMap is ‘posed to look like
VB.NET
Dim _myYesNoIndicator As Nullable(Of Boolean)
C#
bool? _myYesNoIndicator;You will need to create a class and implement the logic to map from your DB type to your POCO. This is straight forward enough as the interface has enough comments to tell you how it will be used. It is used in the “CustomTypeHandler” class and that will tell you a lot about what to expect when your handler is called.
public class NullableBooleanTypeHandlerCallback : ITypeHandlerCallback { private object _nullValue; public void SetParameter(IParameterSetter setter, object parameter) { setter.DataParameter.DbType = DbType.String; bool? param = (bool?)parameter; if (!param.HasValue) setter.Value = DBNull.Value; else { if (param.Value) setter.Value = "Y"; if (!param.Value) setter.Value = "N"; } } public object GetResult(IResultGetter getter) { return GetValue(getter.Value); } private object GetValue(object value) { bool? nullableBool = null; if (Convert.ToString(value) == "Y") nullableBool = true; if (Convert.ToString(value) == "N") nullableBool = false; return nullableBool; } public object ValueOf(string s) { return GetValue(s); } public object NullValue { get { return _nullValue; } }
This element will register your TypeHandler. Put this in your “sqlMap.config” file
<typeHandlers> <typeHandler callback="NullableBooleanTypeHandlerCallback" dbType ="varchar" type="bool?"/> </typeHandlers>
Note, even if your project is VB, since iBatis is implemented using C# you MUST use the C# keyword for the nullable type here. In this case it is “bool?”. This resultMap would be referred to in your “
<resultMaps> <resultMap id="MyResultMap" class="MyClass"> <result property="MyClassProperty" column="MyDatabaseColumn" type="bool?" typeHandler="NullableBooleanTypeHandlerCallback"/> </resultMap> </resultMaps>
I should state that iBatis handles NullabeTypes inherently, and the custom TypeHandler does not have to be used with Nullable Types at all, this example simply leverages these two features to deliver a more concise object model without having to create a domainObject e.t.c. Your situation may require more -but this should at least get the ball rolling.

















Be The First To Comment
Related Post
Please Leave Your Comments Below