Dapper.CustomTypeHandlers
1.1.1
See the version list below for details.
dotnet add package Dapper.CustomTypeHandlers --version 1.1.1
NuGet\Install-Package Dapper.CustomTypeHandlers -Version 1.1.1
<PackageReference Include="Dapper.CustomTypeHandlers" Version="1.1.1" />
paket add Dapper.CustomTypeHandlers --version 1.1.1
#r "nuget: Dapper.CustomTypeHandlers, 1.1.1"
// Install Dapper.CustomTypeHandlers as a Cake Addin #addin nuget:?package=Dapper.CustomTypeHandlers&version=1.1.1 // Install Dapper.CustomTypeHandlers as a Cake Tool #tool nuget:?package=Dapper.CustomTypeHandlers&version=1.1.1
Dapper custom type handlers to serialize/deserialize objects to Xml and Json.
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="1.1.1" />
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 (Travis CI).
To test locally:
- Download and install .NET Core 3.1 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
Technologies
List of technologies, frameworks and libraries used for implementation:
- .NET Core 3.1 (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)
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Dapper (>= 2.0.30)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.1)
- System.Text.Json (>= 4.7.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Fix to registration custom type handlers