diff --git a/samples/TinyHelpers.Dapper.Sample/Models/Post.cs b/samples/TinyHelpers.Dapper.Sample/Models/Post.cs index 2d29cb8..8107e81 100644 --- a/samples/TinyHelpers.Dapper.Sample/Models/Post.cs +++ b/samples/TinyHelpers.Dapper.Sample/Models/Post.cs @@ -13,7 +13,7 @@ public class Post public IEnumerable Authors { get; set; } - public DateTime Date { get; set; } + public DateOnly? Date { get; set; } public IList Reviews { get; set; } } diff --git a/samples/TinyHelpers.Dapper.Sample/Program.cs b/samples/TinyHelpers.Dapper.Sample/Program.cs index 1c45796..fea3aa1 100644 --- a/samples/TinyHelpers.Dapper.Sample/Program.cs +++ b/samples/TinyHelpers.Dapper.Sample/Program.cs @@ -5,9 +5,12 @@ using TinyHelpers.Dapper.Sample.Models; using TinyHelpers.Dapper.TypeHandlers; -const string ConnectionString = @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=Sample;Integrated Security=True"; +const string ConnectionString = @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=SampleDB;Integrated Security=True"; using var connection = new SqlConnection(ConnectionString); + +DateOnlyTypeHandler.Configure(); +TimeOnlyTypeHandler.Configure(); JsonTypeHandler>.Configure(); StringEnumerableTypeHandler.Configure(); @@ -21,7 +24,7 @@ Title = "TinyHelpers3", Content = "New Description", Authors = new string[] { "Andrea", "Calogero" }, - Date = DateTime.UtcNow + Date = DateOnly.FromDateTime(DateTime.Now) }; await connection.ExecuteAsync("INSERT INTO Posts(Id, Title, Content, Date, Authors, Reviews) VALUES(@Id, @Title, @Content, @Date, @Authors, @Reviews)", diff --git a/samples/TinyHelpers.Dapper.Sample/TinyHelpers.Dapper.Sample.csproj b/samples/TinyHelpers.Dapper.Sample/TinyHelpers.Dapper.Sample.csproj index 052407a..92e0d53 100644 --- a/samples/TinyHelpers.Dapper.Sample/TinyHelpers.Dapper.Sample.csproj +++ b/samples/TinyHelpers.Dapper.Sample/TinyHelpers.Dapper.Sample.csproj @@ -6,7 +6,8 @@ - + + diff --git a/src/TinyHelpers.Dapper/README.md b/src/TinyHelpers.Dapper/README.md index ccb7442..479c120 100644 --- a/src/TinyHelpers.Dapper/README.md +++ b/src/TinyHelpers.Dapper/README.md @@ -12,27 +12,30 @@ A collection of helper methods and classes for Dapper that I use every day. I ha The library is available on [NuGet](https://www.nuget.org/packages/TinyHelpers.Dapper). Just search for *TinyHelpers.Dapper* in the **Package Manager GUI** or run the following command in the **.NET CLI**: +```shell dotnet add package TinyHelpers.Dapper +``` **Usage** The library provides some Type Handlers to handle data types that are not natively supported by Dapper. They can be explicitly used calling the corresponding `Configure` methods: - // using TinyHelpers.Dapper.TypeHandlers; +```csharp +// using TinyHelpers.Dapper.TypeHandlers; - // Automatically serializes and deserializes Person class in JSON string. - JsonTypeHandler.Configure(); +// Automatically serializes and deserializes Person class in JSON string. +JsonTypeHandler.Configure(); - // Handles all string[] properties. - StringArrayTypeHandler.Configure(); +// Handles all string[] properties. +StringArrayTypeHandler.Configure(); - // Handles all IEnumerable properties. - StringEnumerableTypeHandler.Configure(); - - // Handles DateOnly and TimeOnly propertites. - DateOnlyTypeHandler.Configure(); - TimeOnlyTypeHandler.Configure(); +// Handles all IEnumerable properties. +StringEnumerableTypeHandler.Configure(); +// Handles DateOnly and TimeOnly properties. +DateOnlyTypeHandler.Configure(); +TimeOnlyTypeHandler.Configure(); +``` **Contribute** The project is constantly evolving. Contributions are welcome. Feel free to file issues and pull requests on the repo and we'll address them as we can. diff --git a/src/TinyHelpers.Dapper/TinyHelpers.Dapper.csproj b/src/TinyHelpers.Dapper/TinyHelpers.Dapper.csproj index 8748e83..8fcdc23 100644 --- a/src/TinyHelpers.Dapper/TinyHelpers.Dapper.csproj +++ b/src/TinyHelpers.Dapper/TinyHelpers.Dapper.csproj @@ -1,7 +1,7 @@  - netstandard2.0;net6.0;net7.0 + netstandard2.0;net6.0;net7.0;net8.0 latest enable enable @@ -21,8 +21,8 @@ - - + + diff --git a/src/TinyHelpers.Dapper/TypeHandlers/JsonTypeHandler.cs b/src/TinyHelpers.Dapper/TypeHandlers/JsonTypeHandler.cs index 2a2e315..c4b5895 100644 --- a/src/TinyHelpers.Dapper/TypeHandlers/JsonTypeHandler.cs +++ b/src/TinyHelpers.Dapper/TypeHandlers/JsonTypeHandler.cs @@ -2,17 +2,13 @@ using System.Text.Json; using System.Text.Json.Serialization; using Dapper; +using TinyHelpers.Json.Serialization; namespace TinyHelpers.Dapper.TypeHandlers; -public class JsonTypeHandler : SqlMapper.TypeHandler +public class JsonTypeHandler(JsonSerializerOptions? jsonSerializerOptions = null) : SqlMapper.TypeHandler { - private readonly JsonSerializerOptions jsonSerializerOptions; - - public JsonTypeHandler(JsonSerializerOptions? jsonSerializerOptions = null) - { - this.jsonSerializerOptions = jsonSerializerOptions ?? JsonOptions.Default; - } + private readonly JsonSerializerOptions jsonSerializerOptions = jsonSerializerOptions ?? JsonOptions.Default; public override T Parse(object value) { @@ -20,7 +16,7 @@ public override T Parse(object value) return JsonSerializer.Deserialize(json, jsonSerializerOptions)!; } - public override void SetValue(IDbDataParameter parameter, T value) + public override void SetValue(IDbDataParameter parameter, T? value) { var json = JsonSerializer.Serialize(value!, jsonSerializerOptions); parameter.Value = json; @@ -32,7 +28,7 @@ public static void Configure(JsonSerializerOptions? jsonSerializerOptions = null if (useUtcDate) { - jsonSerializerOptions.Converters.Add(new Json.Serialization.UtcDateTimeConverter()); + jsonSerializerOptions.Converters.Add(new UtcDateTimeConverter()); } if (serializeEnumAsString) diff --git a/src/TinyHelpers.Dapper/TypeHandlers/StringArrayTypeHandler.cs b/src/TinyHelpers.Dapper/TypeHandlers/StringArrayTypeHandler.cs index 83d8a5e..4bc8baa 100644 --- a/src/TinyHelpers.Dapper/TypeHandlers/StringArrayTypeHandler.cs +++ b/src/TinyHelpers.Dapper/TypeHandlers/StringArrayTypeHandler.cs @@ -3,24 +3,17 @@ namespace TinyHelpers.Dapper.TypeHandlers; -public class StringArrayTypeHandler : SqlMapper.TypeHandler +public class StringArrayTypeHandler(string separator = ";") : SqlMapper.TypeHandler { - private readonly string separator; - - public StringArrayTypeHandler(string separator = ";") - { - this.separator = separator; - } - public override string[] Parse(object value) { var content = value.ToString()!; return content.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries); } - public override void SetValue(IDbDataParameter parameter, string[] value) + public override void SetValue(IDbDataParameter parameter, string[]? value) { - var content = string.Join(separator, value); + var content = string.Join(separator, value!); parameter.Value = content; } diff --git a/src/TinyHelpers.Dapper/TypeHandlers/StringEnumerableTypeHandler.cs b/src/TinyHelpers.Dapper/TypeHandlers/StringEnumerableTypeHandler.cs index 7dec089..55bb8aa 100644 --- a/src/TinyHelpers.Dapper/TypeHandlers/StringEnumerableTypeHandler.cs +++ b/src/TinyHelpers.Dapper/TypeHandlers/StringEnumerableTypeHandler.cs @@ -3,24 +3,17 @@ namespace TinyHelpers.Dapper.TypeHandlers; -public class StringEnumerableTypeHandler : SqlMapper.TypeHandler> +public class StringEnumerableTypeHandler(string separator = ";") : SqlMapper.TypeHandler> { - private readonly string separator; - - public StringEnumerableTypeHandler(string separator = ";") - { - this.separator = separator; - } - public override IEnumerable Parse(object value) { var content = value.ToString()!; return content.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries); } - public override void SetValue(IDbDataParameter parameter, IEnumerable value) + public override void SetValue(IDbDataParameter parameter, IEnumerable? value) { - var content = string.Join(separator, value); + var content = string.Join(separator, value!); parameter.Value = content; } diff --git a/src/TinyHelpers.Dapper/version.json b/src/TinyHelpers.Dapper/version.json index a90330f..e08a3a2 100644 --- a/src/TinyHelpers.Dapper/version.json +++ b/src/TinyHelpers.Dapper/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "2.0", + "version": "3.0", "publicReleaseRefSpec": [ "^refs/heads/master$" // we release out of master ],