MapDataReader 1.0.13
dotnet add package MapDataReader --version 1.0.13
NuGet\Install-Package MapDataReader -Version 1.0.13
<PackageReference Include="MapDataReader" Version="1.0.13" />
paket add MapDataReader --version 1.0.13
#r "nuget: MapDataReader, 1.0.13"
// Install MapDataReader as a Cake Addin #addin nuget:?package=MapDataReader&version=1.0.13 // Install MapDataReader as a Cake Tool #tool nuget:?package=MapDataReader&version=1.0.13
MapDataReader
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.
Benchmarks
20X faster than using reflection, even with caching. Benchmark for a tiny class with 5 string properties:
Method | Mean | Error | StdDev | Gen0 | Allocated |
---|---|---|---|---|---|
Reflection | 951.16 ns | 15.107 ns | 0.828 ns | 0.1459 | 920 B |
MapDataReader | 44.15 ns | 2.840 ns | 0.156 ns | 0.0089 | 56 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 anIDataReader
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 onint
and other implicit casting (sometimes a DataReader may decide to returnbyte
for small integer database value, and it maps toint
perfectly via some unboxing magic) - Properly maps
DBNull
tonull
. - 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
Method | Mean | Error | StdDev | Allocated |
---|---|---|---|---|
SetPropReflection | 98.294 ns | 5.7443 ns | 0.3149 ns | - |
SetPropReflectionCached | 71.137 ns | 1.9736 ns | 0.1082 ns | - |
SetPropMapDataReader | 4.711 ns | 0.4640 ns | 0.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
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.
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)
Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
---|---|---|---|---|---|---|
DapperWithCache | 142.09 us | 8,013.663 ns | 439.256 ns | 9.0332 | 1.2207 | 57472 B |
MapDataReader | 133.22 us | 28,679.198 ns | 1,572.004 ns | 9.0332 | 1.2207 | 57624 B |
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on MapDataReader:
Package | Downloads |
---|---|
Thelegend107.SQL.Data.Lib
TheLegend107 SQL Database library. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.13 | 5,472 | 5/1/2024 |
1.0.12 | 145 | 4/26/2024 |
1.0.11 | 9,447 | 5/5/2023 |
1.0.10 | 9,109 | 1/20/2023 |
1.0.9 | 418 | 1/8/2023 |
1.0.8 | 3,280 | 11/28/2022 |
1.0.6 | 503 | 11/1/2022 |
1.0.5 | 403 | 10/28/2022 |
1.0.4 | 2,073 | 10/24/2022 |
1.0.3 | 661 | 10/13/2022 |
1.0.2 | 424 | 10/11/2022 |
1.0.1 | 425 | 10/10/2022 |
1.0.0 | 422 | 10/10/2022 |