You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

Dapper.CustomTypeHandlers

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

Dapper.CustomTypeHandlers

Dapper custom type handlers to serialize/deserialize objects to Xml and Json.

9.0.0
Source
nugetNuGet
Version published
Maintainers
1
Created
Source

Dapper.CustomTypeHandlers NuGet Version

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

Technologies

List of technologies, frameworks and libraries used for implementation:

License

This project is licensed under the MIT License.

Keywords

dapper

FAQs

Package last updated on 13 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts