MapDataReader

January 15, 2026 ยท View on GitHub

Super fast mapping DataReader to a strongly typed object. High performance, lighweight (12Kb dll), uses AOT source generation and no reflection, mapping code is generated at compile time.

.NET Nuget Net stanrdard 2.0

Benchmarks

20X faster than using reflection, even with caching. Benchmark for a tiny class with 5 string properties:

MethodMeanErrorStdDevGen0Allocated
Reflection951.16 ns15.107 ns0.828 ns0.1459920 B
MapDataReader44.15 ns2.840 ns0.156 ns0.008956 B

Install via Nuget

Install-Package MapDataReader

Usage with IDataReader

using MapDataReader;

[GenerateDataReaderMapper] // <-- mark your class with this attribute
public class MyClass
{
	public int ID { get; set; }
	public string Name { get; set; }
	public int Size { get; set; }
	public bool Enabled { get; set; }
}

var dataReader = new SqlCommand("SELECT * FROM MyTable", connection).ExecuteReader();

List<MyClass> results = dataReader.ToMyClass(); // "ToMyClass" method is generated at compile time

Some notes for the above

  • The ToMyClass() method above - is an IDataReader extension method generated at compile time. You can even "go to definition" in Visual Studio and examine its code.
  • The naming convention is ToCLASSNAME() we can't use generics here, since <T> is not part of method signatures in C# (considered in later versions of C#). If you find a prettier way - please contribute!
  • Maps properies with public setters only.
  • The datareader is being closed after mapping, so don't reuse it.
  • Supports enum properties based on int and other implicit casting (sometimes a DataReader may decide to return byte for small integer database value, and it maps to int perfectly via some unboxing magic)
  • Properly maps DBNull to null.
  • Complex-type properties may not work.

Bonus API: SetPropertyByName

This package also adds a super fast SetPropertyByName extension method generated at compile time for your class.

Usage:

var x = new MyClass();
x.SetPropertyByName("Size", 42); //20X faster than using reflection
MethodMeanErrorStdDevAllocated
SetPropReflection98.294 ns5.7443 ns0.3149 ns-
SetPropReflectionCached71.137 ns1.9736 ns0.1082 ns-
SetPropMapDataReader4.711 ns0.4640 ns0.0254 ns-

Tip: Using it with Dapper

If you're already using the awesome Dapper ORM by Marc Gravel, Sam Saffron and Nick Craver, this is how you can use our library to speed up DataReader-to-object mapping in Dapper:

// override Dapper extension method to use fast MapDataReader instead of Dapper's built-in reflection
// mind that we use "SqlConnection" here to abuse C# "type specificty", otherwise you get "ambiguous call" compile error
public static List<T> Query<T>(this SqlConnection cn, string sql, object parameters = null)
{
	if (typeof(T) == typeof(MyClass)) //our own class that we marked with attribute?
		return cn.ExecuteReader(sql, parameters).ToMyClass() as List<T>; //use MapDataReader

	if (typeof(T) == typeof(AnotherClass)) //another class we have enabled?
		return cn.ExecuteReader(sql, parameters).ToAnotherClass() as List<T>; //again

	//fallback to Dapper by default
	return SqlMapper.Query<T>(cn, sql, parameters).AsList();
}

Why the C# compiler will choose your method over Dapper's?

When the C# compiler sees two extension methods with the same signature, it uses the one that's "closer" to your code. "Closiness" - is determined by multiple factors - same namespace, same assembly, derived class over base class, implementation over interface etc. Adding an override like this will silently switch your existing code from using Dapper/reflection to using our source generator (b/c it uses a more specific connection type and lives in your project's namescape), while still keeping the awesomeness of Dapper and you barely have to rewrite any of your code.

Mind that we use "SqlConnection" for the extension method here to abuse C# "type specificty", otherwise you get "ambiguous call" compile error.


P.S. But what's the point?

While reflection-based ORMs like Dapper are very fast after all the reflaction objects have been cached, they still do a lot of reflection-based heavy-lifting when you query the database for the first time. Which slows down application startup significantly. Which, in turn, can become a problem if you deploy the application multiple times a day.

Or - if you run your ASP.NET Core app on IIS - this causes 503 errors during IIS recycles, see https://github.com/dotnet/aspnetcore/issues/41340 and faster app startup helps a lot.

Also, reflection-caching causes memory pressure becasue of all the concurrent dictionaries used for caching.

And even with all the caching, a simple straightforward code like obj.x = y will always be faster then looking up a cached delegate in a thousands-long dictionary by a string key and invoking it via reflection.

Even if you don't care about the startup performance of your app, MapDataReader is still 5-7% faster than Dapper (note - we're not even using Dapper's command-cache store here, just the datareader parser, actual real world Dapper scenario will be even slower)

MethodMeanErrorStdDevGen0Gen1Allocated
DapperWithCache142.09 us8,013.663 ns439.256 ns9.03321.220757472 B
MapDataReader133.22 us28,679.198 ns1,572.004 ns9.03321.220757624 B