MapDataReader 1.0.5
See the version list below for details.
dotnet add package MapDataReader --version 1.0.5
NuGet\Install-Package MapDataReader -Version 1.0.5
<PackageReference Include="MapDataReader" Version="1.0.5" />
paket add MapDataReader --version 1.0.5
#r "nuget: MapDataReader, 1.0.5"
// Install MapDataReader as a Cake Addin #addin nuget:?package=MapDataReader&version=1.0.5 // Install MapDataReader as a Cake Tool #tool nuget:?package=MapDataReader&version=1.0.5
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; }
}
//ToMyClass() method is generated at compile time
List<MyClass> result = dbconnection.ExecuteReader("SELECT * FROM MyTable").ToMyClass();
//.ExecuteReader is just a helper method from Dapper ORM, you're free to use other ways to create a datareader
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
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? use MapDataReader
return cn.ExecuteReader(sql, parameters).ToMyClass() as List<T>;
if (typeof(T) == typeof(AnotherClass)) //another class we have enabled? use MapDataReader
return cn.ExecuteReader(sql, parameters).ToAnotherClass() as List<T>;
//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. (go read the article linked above). 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 cache the reflection objects and are almost as fast as source-generated IDataReader
mapping after the caches have been filled, 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 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 faster app startup helps a lot.
Also, reflection-caching causes memory pressure becasue of the concurrent dictionaries used for caching.
And even with 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.
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
- Microsoft.CodeAnalysis.CSharp (>= 4.3.1)
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,474 | 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 |