Branded.Integrations 1.0.4

dotnet add package Branded.Integrations --version 1.0.4
                    
NuGet\Install-Package Branded.Integrations -Version 1.0.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Branded.Integrations" Version="1.0.4">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Branded.Integrations" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="Branded.Integrations">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Branded.Integrations --version 1.0.4
                    
#r "nuget: Branded.Integrations, 1.0.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Branded.Integrations@1.0.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Branded.Integrations&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=Branded.Integrations&version=1.0.4
                    
Install as a Cake Tool

Library integrations for Branded.SourceGenerator

Overview

Branded.Integrations is a C# source package that complements the Branded.SourceGenerator package by adding converters for a few common libraries. The goal is to make using branded types as seamless as possible. For example, when deserializing a JSON document, the properties that are identifiers can be of a branded type instead of being int or string.

This package is distributed as a source-only package, which means that the source code of the integrations is directly added to your project. The advantage of doing so is that the source files also apply the assembly attributes needed to add the integration, making this library plug-and-play.

List of integrated libraries

The following is a list of all the library integrations that are included in this package. The package uses custom msbuild conditions to ensure that only the integrations for the libraries that are in use by the project are included.

System.Text.Json

The integration consists of a set of JsonConverter for branded types with the following underlying types: int, uint, long, ulong and string.

Example
using System.Text.Json;

namespace Branded.Showcase;

public record Widget(
    WidgetIdentifier Id,
    UserIdentifier Owner
);

public readonly partial record struct WidgetIdentifier(int Id);
public readonly partial record struct UserIdentifier(string Username);

public static class Program
{
    public static void Main()
    {
        var json = JsonSerializer.Serialize(new Widget(new(42), new("alex")));
        Console.WriteLine($"json: {json}");

        var parsed = JsonSerializer.Deserialize<Widget>(json);
        Console.WriteLine($"parsed: {parsed}");
    }
}
Output
json: {"Id":42,"Owner":"alex"}
parsed: Widget { Id = 42, Owner = alex }

Newtonsoft.Json

The integration consists of a JsonConverter for branded types of any underlying type.

Example
using Newtonsoft.Json;

namespace Branded.Showcase;

public record Widget(
    WidgetIdentifier Id,
    UserIdentifier Owner
);

public readonly partial record struct WidgetIdentifier(int Id);
public readonly partial record struct UserIdentifier(string Username);

public static class Program
{
    public static void Main()
    {

        var json = JsonConvert.SerializeObject(new Widget(new(42), new("alex")));
        Console.WriteLine($"json: {json}");

        var parsed = JsonConvert.DeserializeObject<Widget>(json);
        Console.WriteLine($"parsed: {parsed}");
    }
}
Output
json: {"Id":42,"Owner":"alex"}
parsed: Widget { Id = 42, Owner = alex }

Dapper

The integration consists of an helper class, BrandedTypeRegistry, that registers type handlers for branded types. The registration must be performed manually by calling the Dispatch method on each branded type, passing BrandedTypeRegistry.Instance. Usually this is performed on the static constructor of the class that uses the branded types.

Example
using Branded.Integrations.Dapper;
using Dapper;
using Microsoft.Data.SqlClient;

namespace Branded.Showcase;

public record Widget(
    WidgetIdentifier Id,
    UserIdentifier Owner
);

public readonly partial record struct WidgetIdentifier(int Id);
public readonly partial record struct UserIdentifier(string Username);

public static class Program
{
    /*
    CREATE TABLE [dbo].[Widgets](
        [Id] [int] NOT NULL,
        [Owner] [nvarchar](50) NOT NULL
    )
    */

    public static void Main()
    {
        // Register the type handlers
        WidgetIdentifier.Dispatch(BrandedTypeRegistry.Instance);
        UserIdentifier.Dispatch(BrandedTypeRegistry.Instance);

        using var db = new SqlConnection("Data source=localhost; Integrated Security=SSPI; Initial Catalog=Sandbox; TrustServerCertificate=true");
        db.Execute(
            """
            DELETE FROM Widgets
            
            INSERT INTO Widgets(Id, Owner) VALUES (1, 'alex'),(2, 'alex'),(3, 'rain')
            """
        );

        var owner = new UserIdentifier("alex");

        var results = db.Query<Widget>(
            """
            SELECT Id, Owner
            FROM Widgets
            WHERE Owner = @owner
            """,
            new { owner }
        );

        foreach (var result in results)
        {
            Console.WriteLine(result);
        }
    }
}
Output
Widget { Id = 1, Owner = alex }
Widget { Id = 2, Owner = alex }
Product 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.  net9.0 was computed.  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. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

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
1.0.4 217 7/4/2025