views:

291

answers:

1

Hi,

This question is in relation to another question I have: http://stackoverflow.com/questions/603808/using-ibatis-net-with-generic-custom-collection-interfaces-and-unity

The problem seems to be that iBATIS.NET will only populate a custom collection (i.e. QueryForObject("Select_Foo") which has a custom collection of Bars) if it is a concrete instance of custom collection not an interface. Does anyone know if this is a limitation of iBATIS.NET or if there is a way of doing this?

Thanks,

Paul

+1  A: 

If I understood you right then you'd like to get full control over the way how iBatis maps to some object.

You can do this with the ITypeHandlerCallback. Have a look for a full description in the PDF documentation in section "3.5.5. Custom Type Handlers".

I have done something similar with DataTables. Your implementation might look similar to this:

class DataTableBuilder : ITypeHandlerCallback
{
    public object GetResult(IResultGetter getter)
    {
        IDataReader reader = getter.DataReader;

        // (A) define whatever type you want to

        // (B) read rows from DataReader and populate your type from (A)
        while (reader.Read())
        {
            // iterate over the columns of the current row
            for (int i = 0; i < reader.FieldCount; i++)
            {
               // populate your type from (A)
            }                    
        }
        return ...;   // return your type from (A)
    }

    // implement the other members of ITypeHandlerCallback
    // the implementation below actually worked for me
    public object NullValue { get { return null; } }
    public void SetParameter(IParameterSetter setter, object parameter) { }
    public object ValueOf(string s) { return s; }
}

A last note: iBatis is fine for building data transfer objects (DTOs). If you try something like above, you might already moving toward a business object approach. This might get painful with iBatis. Currently (well ... for a few months already, due to lack of time) I am evaluating NHibernate as an alternative. I think that NHibernate is handling business object approaches much more smoothly than iBatis does.

Theo Lenndorff