Skip to content

Commit

Permalink
Update TinyHelpers.Dapper to .NET 8.0 #160
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva committed Nov 21, 2023
1 parent 336d66a commit 109b8d5
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 48 deletions.
2 changes: 1 addition & 1 deletion samples/TinyHelpers.Dapper.Sample/Models/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Post

public IEnumerable<string> Authors { get; set; }

public DateTime Date { get; set; }
public DateOnly? Date { get; set; }

public IList<Review> Reviews { get; set; }
}
Expand Down
7 changes: 5 additions & 2 deletions samples/TinyHelpers.Dapper.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IList<Review>>.Configure();
StringEnumerableTypeHandler.Configure();

Expand All @@ -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)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.1" />
<PackageReference Include="Azure.Identity" Version="1.10.4" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.2" />
</ItemGroup>

<ItemGroup>
Expand Down
25 changes: 14 additions & 11 deletions src/TinyHelpers.Dapper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person>();
// Automatically serializes and deserializes Person class in JSON string.
JsonTypeHandler.Configure<Person>();

// Handles all string[] properties.
StringArrayTypeHandler.Configure();
// Handles all string[] properties.
StringArrayTypeHandler.Configure();

// Handles all IEnumerable<string> properties.
StringEnumerableTypeHandler.Configure();

// Handles DateOnly and TimeOnly propertites.
DateOnlyTypeHandler.Configure();
TimeOnlyTypeHandler.Configure();
// Handles all IEnumerable<string> 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.
Expand Down
6 changes: 3 additions & 3 deletions src/TinyHelpers.Dapper/TinyHelpers.Dapper.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand All @@ -21,8 +21,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.151" />
<PackageReference Include="TinyHelpers" Version="2.0.35" />
<PackageReference Include="Dapper" Version="2.1.21" />
<PackageReference Include="TinyHelpers" Version="3.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 5 additions & 9 deletions src/TinyHelpers.Dapper/TypeHandlers/JsonTypeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Dapper;
using TinyHelpers.Json.Serialization;

namespace TinyHelpers.Dapper.TypeHandlers;

public class JsonTypeHandler<T> : SqlMapper.TypeHandler<T>
public class JsonTypeHandler<T>(JsonSerializerOptions? jsonSerializerOptions = null) : SqlMapper.TypeHandler<T>
{
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)
{
var json = value.ToString()!;
return JsonSerializer.Deserialize<T>(json, jsonSerializerOptions)!;
}

public override void SetValue(IDbDataParameter parameter, T value)
public override void SetValue(IDbDataParameter parameter, T? value)
{
var json = JsonSerializer.Serialize<object>(value!, jsonSerializerOptions);
parameter.Value = json;
Expand All @@ -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)
Expand Down
13 changes: 3 additions & 10 deletions src/TinyHelpers.Dapper/TypeHandlers/StringArrayTypeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,17 @@

namespace TinyHelpers.Dapper.TypeHandlers;

public class StringArrayTypeHandler : SqlMapper.TypeHandler<string[]>
public class StringArrayTypeHandler(string separator = ";") : SqlMapper.TypeHandler<string[]>
{
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;
}

Expand Down
13 changes: 3 additions & 10 deletions src/TinyHelpers.Dapper/TypeHandlers/StringEnumerableTypeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,17 @@

namespace TinyHelpers.Dapper.TypeHandlers;

public class StringEnumerableTypeHandler : SqlMapper.TypeHandler<IEnumerable<string>>
public class StringEnumerableTypeHandler(string separator = ";") : SqlMapper.TypeHandler<IEnumerable<string>>
{
private readonly string separator;

public StringEnumerableTypeHandler(string separator = ";")
{
this.separator = separator;
}

public override IEnumerable<string> Parse(object value)
{
var content = value.ToString()!;
return content.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries);
}

public override void SetValue(IDbDataParameter parameter, IEnumerable<string> value)
public override void SetValue(IDbDataParameter parameter, IEnumerable<string>? value)
{
var content = string.Join(separator, value);
var content = string.Join(separator, value!);
parameter.Value = content;
}

Expand Down
2 changes: 1 addition & 1 deletion src/TinyHelpers.Dapper/version.json
Original file line number Diff line number Diff line change
@@ -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
],
Expand Down

0 comments on commit 109b8d5

Please sign in to comment.