Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for parsing IDs from decimals to dapper #120

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/StronglyTypedIds.Templates/int-dapper.typedid
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
return value switch
{
int intValue => new PLACEHOLDERID(intValue),
long longValue when longValue < int.MaxValue => new PLACEHOLDERID((int)longValue),
short shortValue => new PLACEHOLDERID(shortValue),
long longValue and < int.MaxValue and > int.MinValue => new PLACEHOLDERID((int)longValue),
decimal decimalValue and < int.MaxValue and > int.MinValue => new PLACEHOLDERID((int)decimalValue),
string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new PLACEHOLDERID(result),
_ => throw new global::System.InvalidCastException($"Unable to cast object of type {value.GetType()} to PLACEHOLDERID"),
};
Expand Down
4 changes: 3 additions & 1 deletion src/StronglyTypedIds.Templates/int-full.typedid
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@
return value switch
{
int intValue => new PLACEHOLDERID(intValue),
long longValue when longValue < int.MaxValue => new PLACEHOLDERID((int)longValue),
short shortValue => new PLACEHOLDERID(shortValue),
long longValue and < int.MaxValue and > int.MinValue => new PLACEHOLDERID((int)longValue),
decimal decimalValue and < int.MaxValue and > int.MinValue => new PLACEHOLDERID((int)decimalValue),
string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new PLACEHOLDERID(result),
_ => throw new global::System.InvalidCastException($"Unable to cast object of type {value.GetType()} to PLACEHOLDERID"),
};
Expand Down
1 change: 1 addition & 0 deletions src/StronglyTypedIds.Templates/long-dapper.typedid
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
long longValue => new PLACEHOLDERID(longValue),
int intValue => new PLACEHOLDERID(intValue),
short shortValue => new PLACEHOLDERID(shortValue),
decimal decimalValue and < long.MaxValue and > long.MinValue => new PLACEHOLDERID((long)decimalValue),
string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new PLACEHOLDERID(result),
_ => throw new global::System.InvalidCastException($"Unable to cast object of type {value.GetType()} to PLACEHOLDERID"),
};
Expand Down
1 change: 1 addition & 0 deletions src/StronglyTypedIds.Templates/long-full.typedid
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
long longValue => new PLACEHOLDERID(longValue),
int intValue => new PLACEHOLDERID(intValue),
short shortValue => new PLACEHOLDERID(shortValue),
decimal decimalValue and < long.MaxValue and > long.MinValue => new PLACEHOLDERID((long)decimalValue),
string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new PLACEHOLDERID(result),
_ => throw new global::System.InvalidCastException($"Unable to cast object of type {value.GetType()} to PLACEHOLDERID"),
};
Expand Down
1 change: 1 addition & 0 deletions test/IntegrationLibraries.props
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Dapper" Version="2.0.90" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
22 changes: 22 additions & 0 deletions test/StronglyTypedIds.IntegrationTests/IntIdTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -266,6 +267,27 @@ public async Task WhenDapperValueConverterUsesValueConverter()
Assert.Equal(new ConvertersIntId(123), value);
}

[Fact(Skip = "Requires localdb to be available")]
public async Task WhenDapperValueConverterUsesValueConverterWithSqlServer()
{
using var connection = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30");
await connection.OpenAsync();

var results = await connection.QueryAsync<ConvertersIntId>("SELECT CAST (123 AS numeric(38,0))");

var value = Assert.Single(results);
Assert.Equal(new ConvertersIntId(123), value);
}

[Fact]
public void WhenDapperValueConverterAndDecimalUsesValueConverter()
{
var handler = new ConvertersIntId.DapperTypeHandler();
var value = handler.Parse((decimal) 123L);

Assert.Equal(new ConvertersIntId(123), value);
}

[Theory]
[InlineData(123)]
[InlineData("123")]
Expand Down
22 changes: 22 additions & 0 deletions test/StronglyTypedIds.IntegrationTests/LongIdTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -278,6 +279,27 @@ public async Task WhenDapperValueConverterUsesValueConverter()
Assert.Equal(value, new ConvertersLongId(123));
}

[Fact(Skip = "Requires localdb to be available")]
public async Task WhenDapperValueConverterUsesValueConverterWithSqlServer()
{
using var connection = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30");
await connection.OpenAsync();

var results = await connection.QueryAsync<ConvertersIntId>("SELECT CAST (123 AS numeric(38,0))");

var value = Assert.Single(results);
Assert.Equal(new ConvertersIntId(123), value);
}

[Fact]
public void WhenDapperValueConverterAndDecimalUsesValueConverter()
{
var handler = new ConvertersLongId.DapperTypeHandler();
var value = handler.Parse((decimal) 123L);

Assert.Equal(new ConvertersLongId(123), value);
}

#if NET6_0_OR_GREATER
[Fact]
public void WhenConventionBasedEfCoreValueConverterUsesValueConverter()
Expand Down
Loading