From 89b2be9edb950b462b194ab8e1f79fe4177377cd Mon Sep 17 00:00:00 2001 From: Amy Date: Sun, 6 Oct 2024 13:47:39 +0100 Subject: [PATCH] docs: updated the documentation --- README.md | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c23eacd..cb83e31 100644 --- a/README.md +++ b/README.md @@ -36,14 +36,22 @@ public class MyClass var dataReader = new SqlCommand("SELECT * FROM MyTable", connection).ExecuteReader(); -List results = dataReader.To(); +List results = dataReader.To(); // Generic method + +// or + +List results = dataReader.ToMyClass(); // Direct method ``` 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 `` 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 `To()` method above - is an `IDataReader` extension method generated at compile time. + * The naming convention is `To()` where `T` is your class name marked with `[GenerateDataReaderMapper]`, e.g. `MyClass`. + * Thanks to [@5andr0](https://github.com/5andr0) for the suggestion of how to add the generic version of this method. +* The `ToMyClass()` method is functionally identical to the `To()` method but maintained for backwards compatability. + * The naming convention is `ToCLASSNAME()` + * You can even "go to definition" in Visual Studio and examine the code for either of these two methods. +* Maps properties 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`. @@ -85,10 +93,10 @@ If you're already using the awesome [Dapper ORM](https://github.com/DapperLib/Da public static List Query(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; //use MapDataReader + return cn.ExecuteReader(sql, parameters).To() as List; //use MapDataReader if (typeof(T) == typeof(AnotherClass)) //another class we have enabled? - return cn.ExecuteReader(sql, parameters).ToAnotherClass() as List; //again + return cn.ExecuteReader(sql, parameters).To() as List; //again //fallback to Dapper by default return SqlMapper.Query(cn, sql, parameters).AsList();