这个问题与我的另一个问题有关:将iBATIS.NET与通用自定义集合接口和Unity一起使用

问题似乎是,如果iBATIS.NET是自定义集合的具体实例而不是接口,则它将仅填充自定义集合(即具有自定义集合条的QueryForObject(“Select_Foo”))。有谁知道这是iBATIS.NET的限制还是有办法做到这一点?

谢谢,

有帮助吗?

解决方案

如果我理解你,那么你想完全控制iBatis如何映射到某个对象。

您可以使用 ITypeHandlerCallback 执行此操作。请查看 PDF文档在”3.5.5节“中。自定义类型处理程序“。

我已经使用DataTables做了类似的事情。您的实现可能与此类似:

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; }
}

最后一点:iBatis可用于构建数据传输对象(DTO)。如果您尝试上述内容,您可能已经开始采用业务对象方法。这可能会让iBatis感到痛苦。目前(好吧......已经有几个月了,由于时间不够)我正在评估NHibernate作为替代方案。我认为NHibernate比iBatis更顺畅地处理业务对象方法。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top