Dapper.CustomTypeHandlers
9.0.1
dotnet add package Dapper.CustomTypeHandlers --version 9.0.1
NuGet\Install-Package Dapper.CustomTypeHandlers -Version 9.0.1
<PackageReference Include="Dapper.CustomTypeHandlers" Version="9.0.1" />
<PackageVersion Include="Dapper.CustomTypeHandlers" Version="9.0.1" />
<PackageReference Include="Dapper.CustomTypeHandlers" />
paket add Dapper.CustomTypeHandlers --version 9.0.1
#r "nuget: Dapper.CustomTypeHandlers, 9.0.1"
#:package Dapper.CustomTypeHandlers@9.0.1
#addin nuget:?package=Dapper.CustomTypeHandlers&version=9.0.1
#tool nuget:?package=Dapper.CustomTypeHandlers&version=9.0.1
Dapper.CustomTypeHandlers 
Dapper custom type handlers to serialize/deserialize objects to Xml and Json. Can be also used to map GUID to a string.
Installation
Use NuGet Package Manager
Install-Package Dapper.CustomTypeHandlers
or .NET CLI
dotnet add package Dapper.CustomTypeHandlers
or just copy into the project file to reference the package
<PackageReference Include="Dapper.CustomTypeHandlers" Version="2.3.0" />
How to use
- Create class that implements IXmlObjectType or IJsonObjectType interface
public class Book
{
public long Id { get; set; }
public string Title { get; set; }
public BookDescription Description { get; set; }
}
public class BookDescription : IXmlObjectType
{
public Learn Learn { get; set; }
public string About { get; set; }
public Features Features { get; set; }
}
public class Learn
{
public List<string> Points { get; set; }
}
public class Features
{
public List<string> Points { get; set; }
}
- Register these new classes in Startup.cs
services.RegisterDapperCustomTypeHandlers(typeof(Book).Assembly);
- Create table in a database that contains a column of the XML type (SQL Server)
CREATE TABLE [dbo].[Books](
[Id] bigint IDENTITY(1,1) NOT NULL,
[Title] nvarchar(200) NOT NULL,
[Description] xml NULL
CONSTRAINT [PK_Books] PRIMARY KEY CLUSTERED
(
[Id] ASC
)
)
To persist object as a JSON, you can use the nvarchar field (class should implement IJsonObjectType)
CREATE TABLE [dbo].[Books](
[Id] bigint IDENTITY(1,1) NOT NULL,
[Title] nvarchar(200) NOT NULL,
[Description] nvarchar(max) NULL
CONSTRAINT [PK_Books] PRIMARY KEY CLUSTERED
(
[Id] ASC
)
)
- Use Dapper to save object data in the database
public async Task SaveBook(Book book)
{
using (var conn = _connectionFactory.Connection())
{
await conn.ExecuteAsync(_@"INSERT INTO Books (Title, Description) VALUES (@Title, @Description)", book);
}
}
How to Test
Every commit or pull request is built and tested on the Continuous Integration system.
To test locally:
- Download and install .NET 8.0 SDK
- Clone or download source code
git clone https://github.com/kubagdynia/Dapper.CustomTypeHandlers.git
- Start tests from the command line
dotnet test ./Dapper.CustomTypeHandlers/
Code Examples
- DapperMappers - An example of using Dapper with the custom Xml and Json mappers https://github.com/kubagdynia/DapperMappers
Technologies
List of technologies, frameworks and libraries used for implementation:
- .NET 8.0 (platform)
- Dapper (micro ORM)
- System.Text.Json (JSON serialization/deserialization)
- NUnit (testing framework)
- SQLite (database for testing purpose)
- FluentAssertions (fluent API for asserting the result of unit tests)
License
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net7.0
- Dapper (>= 2.1.66)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- System.Text.Json (>= 8.0.6)
-
net8.0
- Dapper (>= 2.1.66)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.8)
- System.Text.Json (>= 9.0.8)
-
net9.0
- Dapper (>= 2.1.66)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.8)
- System.Text.Json (>= 9.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
9.0.1 | 183 | 8/30/2025 |
9.0.0 | 348 | 2/13/2025 |
2.3.1 | 169 | 10/9/2024 |
2.3.0 | 202 | 9/9/2024 |
2.2.1 | 153 | 7/15/2024 |
2.2.0 | 243 | 1/17/2024 |
2.1.0 | 267 | 11/18/2023 |
2.0.0 | 187 | 10/31/2023 |
1.2.0 | 850 | 2/15/2020 |
1.1.1 | 710 | 1/25/2020 |
1.1.0 | 689 | 1/25/2020 |
1.0.2 | 600 | 1/19/2020 |
1.0.1 | 614 | 1/19/2020 |
1.0.0 | 608 | 1/19/2020 |
Upgrade to .NET 9.0