Devlooped.Dynamically 1.0.1

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Devlooped.Dynamically --version 1.0.1                
NuGet\Install-Package Devlooped.Dynamically -Version 1.0.1                
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="Devlooped.Dynamically" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Devlooped.Dynamically --version 1.0.1                
#r "nuget: Devlooped.Dynamically, 1.0.1"                
#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.
// Install Devlooped.Dynamically as a Cake Addin
#addin nuget:?package=Devlooped.Dynamically&version=1.0.1

// Install Devlooped.Dynamically as a Cake Tool
#tool nuget:?package=Devlooped.Dynamically&version=1.0.1                

Create record types from dynamic data with a compatible structural shape.

Usage

Create records for your data types:

public record Point(int X, int Y);

public record Line(Point Start, Point End);

public record Drawing(Line[] Lines);

This project will generate a Dynamically class with a factory method to create instances of those records from a data object with a compatible shape, such as:

var data = new
{
    Lines = new[]
    {
        new
        {
            Start = new { X = 50, Y = 0 },
            End = new { X = 0, Y = 100 },
        },
        new
        {
            Start = new { X = 50, Y = 0 },
            End = new { X = 0, Y = 100 },
        }
    }
};

Drawing drawing = Dynamically.Create<Drawing>(data);

In adition to a dynamic object (or an ExpandoObject, for example), you can also pass in objects from other strongly typed values that come from a different assembly as long as it has the same structure. This allows fast in-memory object mapping without any serialization or extra allocations.

The factory works too for Newtonsoft.Json deserialized objects, for example:

// elsewhere, you got an in-memory Json.NET object model, perhaps with:
dynamic data = JsonConvert.DeserializeObject(json);

// Subsequently, you can turn it into your strongly-typed records:
Drawing drawing = Dynamically.Create<Drawing>(data);

You can also optionally customize the mapping for specific records by providing accessible static Create or CreateMany factory methods in your records, so you can selectively customize the mapping by providing them as needed in specific cases. For example:

partial record Drawing
{
    // Customize creation of a single Drawing from a dynamic value
    public static Drawing Create(dynamic value);
    // Customize creation of a list of Drawings from a dynamic value
    public static List<Drawing> CreateMany(dynamic value);
}

How It Works

This package analyzes (at compile-time) the shape of your records and creates a factory that create instances from a dynamic object. For this, it just accesses the properties of the dynamic object and passes them to the record constructor (or its properties). This means that the data must have (at least) the expected values for the conversion to succeed.

The static Dynamically.Create generic method is also generated at compile time and contains a switch statement that dispatches to the correct factory based on the generic argument specified.

In addition, if the records are partial, you also get static Create and CreateMany static methods on the record type itself, for added convenience, such as:

partial record Drawing
{
    public static Drawing Create(dynamic value);
    public static List<Drawing> CreateMany(dynamic value);
}

NOTE: these will only be provided if your record doesn't already have them.

Limitations

This package is not meant to be a full-fledged object mapper. For that, you can use AutoMapper, for example, which is much more flexible and has excelent performance characteristics. This package does provide very fast in-memory object mapping that is far faster and cheaper than going through any sort of serialization.

As mentioned, the provided factories do not provide backwards-compatibility: if you add a property or constructor argument to the record, the factory will fail for payloads without it.

Sponsors

Clarius Org Christian Findlay C. Augusto Proiete Kirill Osenkov MFB Technologies, Inc. SandRock Andy Gocke Shahzad Huq

Sponsor this project  

Learn more about GitHub Sponsors

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

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.1.1 210 2/14/2024
1.1.0 323 8/11/2023
1.0.2 328 11/18/2022
1.0.1 526 11/16/2022
1.0.0 324 11/14/2022
1.0.0-alpha 245 11/14/2022