Drizin / Drizin/InterpolatedSql

Interfaces as properties are not working as expected

Open
#1 2 comments 0 reactions 0 assignees View on GitHub
enhancement help wanted
Dominant language
C#
Stars
279
Forks
15
PR merge metrics
No merged PRs in 30d

Description

Hi there!

Following DTO given:

```csharp
public interface IFoo { }
public sealed record FooA : IFoo { }
public sealed record FooB : IFoo { }

public sealed record DTO
{
public int Id { get; set; }
public IFoo? Foo { get; set; }
}
````

And the following query:

```csharp
SqlMapper.AddTypeHandler(SqlMapper.StringTypeHandler _);

var dto = new DTO
{
Foo = new FooA()
};

using var dbConnection = _;

dbConnection.Open();

var commandBuilder = dbConnection.CommandBuiler($@"
INSERT INTO [DTO] ([Id], [Foo])
VALUES (@Id, @Foo);");

commandBuilder.AddObjectProperties(dto);

commandBuilder.Execute();
```

Despite having a custom type handler registered, you'll end up with with:

```
System.NotSupportedException
HResult=0x80131515
Message=The member Foo of type FooA cannot be used as a parameter value
Source=Dapper
StackTrace:
at Dapper.SqlMapper.LookupDbType(Type type, String name, Boolean demand, ITypeHandler& handler) in /_/Dapper/SqlMapper.cs:line 426
```

Executing this with the original `ExecuteAsync` works like a charm:

```csharp
SqlMapper.AddTypeHandler(SqlMapper.StringTypeHandler _);

var dto = new DTO
{
Foo = new FooA()
};

using var dbConnection = _;

dbConnection.Open();

dbConnection.Execute($@"
INSERT INTO [DTO] ([Id], [Foo])
VALUES (@Id, @Foo);",
dto);
```

Any thoughts?

Best
Andreas

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.