https://github.com/kubagdynia/dapper.customtypehandlers
Dapper custom type handlers to serialize/deserialize objects to Xml and Json. Can be also used to map GUID to a string.
https://github.com/kubagdynia/dapper.customtypehandlers
dapper database dotnet-core json-deserialization json-mapper json-serialization orm xml-deserialization xml-mapping xml-serialization
Last synced: about 1 month ago
JSON representation
Dapper custom type handlers to serialize/deserialize objects to Xml and Json. Can be also used to map GUID to a string.
- Host: GitHub
- URL: https://github.com/kubagdynia/dapper.customtypehandlers
- Owner: kubagdynia
- License: mit
- Created: 2020-01-18T14:33:31.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-02-13T19:50:46.000Z (3 months ago)
- Last Synced: 2025-04-11T04:42:54.628Z (about 1 month ago)
- Topics: dapper, database, dotnet-core, json-deserialization, json-mapper, json-serialization, orm, xml-deserialization, xml-mapping, xml-serialization
- Language: C#
- Homepage:
- Size: 104 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Dapper.CustomTypeHandlers [](https://www.nuget.org/packages/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
``````
### How to use
- Create class that implements **IXmlObjectType** or **IJsonObjectType** interface
```csharp
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 Points { get; set; }
}public class Features
{
public List Points { get; set; }
}
```
- Register these new classes in **Startup.cs**
```csharp
services.RegisterDapperCustomTypeHandlers(typeof(Book).Assembly);
```
- Create table in a database that contains a column of the XML type (SQL Server)
```sql
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)
```sql
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
```csharp
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](https://dotnet.microsoft.com/en-us/download/dotnet/8.0)
- 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](https://dotnet.microsoft.com/en-us/download/dotnet/8.0) (platform)
- [Dapper](https://github.com/StackExchange/Dapper) (micro ORM)
- [System.Text.Json](https://www.nuget.org/packages/System.Text.Json) (JSON serialization/deserialization)
- [NUnit](https://nunit.org/) (testing framework)
- [SQLite](https://www.sqlite.org/) (database for testing purpose)
- [FluentAssertions](https://github.com/fluentassertions/fluentassertions) (fluent API for asserting the result of unit tests)### License
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).