MapDataReader 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package MapDataReader --version 1.0.4
NuGet\Install-Package MapDataReader -Version 1.0.4
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MapDataReader" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MapDataReader --version 1.0.4
#r "nuget: MapDataReader, 1.0.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MapDataReader as a Cake Addin
#addin nuget:?package=MapDataReader&version=1.0.4

// Install MapDataReader as a Cake Tool
#tool nuget:?package=MapDataReader&version=1.0.4

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.

.NET Nuget

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

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 code is generated at compile time
List<MyClass> result = dbconnection.ExecuteReader("SELECT * FROM MyTable").ToMyClass();

Some notes

  • The ToMyClass() method above - is an 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#).
  • Maps properies with public setters only.
  • The reader is being closed after mapping, so don't reuse it.
  • The example above uses .ExecuteReader method from Dapper, but you can generate the reader in any way you want, e.g. the plain SqlCommand.ExecuteReader will also work.
  • 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.
  • netstandard 2.0
  • Contributions are very welcome.

P.S. 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)
{
	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 MDR
		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.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 17 5/1/2024
1.0.12 97 4/26/2024
1.0.11 5,925 5/5/2023
1.0.10 7,018 1/20/2023
1.0.9 387 1/8/2023
1.0.8 3,041 11/28/2022
1.0.6 493 11/1/2022
1.0.5 368 10/28/2022
1.0.4 2,032 10/24/2022
1.0.3 627 10/13/2022
1.0.2 395 10/11/2022
1.0.1 393 10/10/2022
1.0.0 386 10/10/2022