这个问题与我的另一个问题有关:Using iBATIS.NET with generic custom collection interfaces and Unity
问题似乎是 iBATIS.NET 只会填充自定义集合(即 QueryForObject("Select_Foo") ,它具有自定义的 Bars 集合),如果它是自定义集合的具体实例而不是接口。有谁知道这是否是 iBATIS.NET 的限制,或者是否有办法做到这一点?
谢谢,
保罗
这个问题与我的另一个问题有关:Using iBATIS.NET with generic custom collection interfaces and Unity
问题似乎是 iBATIS.NET 只会填充自定义集合(即 QueryForObject("Select_Foo") ,它具有自定义的 Bars 集合),如果它是自定义集合的具体实例而不是接口。有谁知道这是否是 iBATIS.NET 的限制,或者是否有办法做到这一点?
谢谢,
保罗
如果我理解正确,那么您希望完全控制 iBatis 映射到某个对象的方式。
您可以使用ITypeHandlerCallback执行此操作。在“3.5.5. 自定义类型处理程序”部分的PDF 文档中查看完整描述。
我对 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 更顺畅地处理业务对象方法。