IDataReader Complex Mapping
I followed your instructions for type switching per row but in my case I have a multiple result set where the subsequent result sets are all easy objects that do not require type switching.
I know I can read them in through the Parse functionality but then I lose out on the nice feature from GridReader for caching and quite a bit of the optimizations.
Is there any way I can transform the IDataReader to a GridReader after I have completed my type switching?
there's very little context here; what are you trying to accomplish? pretend that the API you want existed: how would you use it? I'm trying to understand what you're after, and I'm struggling a bit; understanding what you want to achieve is key to answering:
- does it exist today
- if not, could it exist in the future
- and if so, what would it look like
- and where would it exist (AOT vs vanilla)
Wow!
thanks for the fast reply -- big fan...
anyways, lets say I am going to run a stored procedure
using (IDataReader reader = await db.ExecuteReaderAsync("foo", new { }, commandType: CommandType.StoredProcedure))
and it will return two datasets like below
select 'abc' as Name, 1 as Type, 3.0 as Value
union all
select 'def' as Name, 2 as Type, 4.0 as Value
select '123' as ChildName, 1 as ParentType
union all
select '456' as ChildName, 2 as ParentType
I can use the code sample you provided to read in the first, type switching per row
if (reader.Read())
{
var toFoo = reader.GetRowParser<BaseType>(typeof(Foo));
var toBar = reader.GetRowParser<BaseType>(typeof(Bar));
var col = reader.GetOrdinal("Type");
do
{
switch (reader.GetInt32(col))
{
case 1:
result.Add(toFoo(reader));
break;
case 2:
result.Add(toBar(reader));
break;
}
} while (reader.Read());
}
But what do I do with the second dataset of child objects?
I know I can call
reader.Parse<T>();
after the fact to process the second dataset
but from what I can tell Parse<T> is going to be less performant than ReadRow under SqlMapper.GridReader because its not caching the types.
so my question is, is there anyway I can do the Type switching per row from the documentation using IDataReader but then leverage the power of SqlMapper.GridReader for processing other result sets that come back from the same stored procedure that do not require the Type switching.
Does that make any sense?