ObjectMapper.psxbox 1.0.2

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

// Install ObjectMapper.psxbox as a Cake Tool
#tool nuget:?package=ObjectMapper.psxbox&version=1.0.2                

ObjectMapper

A simple object mapper to use copy properties to another object.

How to use?

public class Device
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public string? Settings { get; set; }
    public DateTime? Created { get; set; }
    public DateTime? Updated { get; set; }
}

public class DeviceDto
{
    public string? Name { get; set; }
    public Dictionary<string, object>? Settings { get; set; }
    public DateTime? Created { get; set; }
    public DateTime? Updated { get; set; }
}
var device = new Device
{
    Id = 1,
    Name = "TempSensor",
    Settings = "{}",
    Created = new DateTime(2023, 4, 19, 9, 0, 0),
    Updated = null
};

var updateDevice = new DeviceDto
{
    Name = "Kitchen temp sensor",
    Settings = new Dictionary<string, object>
    {
        ["Address"] = 1,
        ["Registry"] = 6,
        ["Scale"] = 0.1f
    },
    Created = null,
    Updated = DateTime.Now
};

var options = new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

TestContext?.WriteLine("Before");
TestContext?.WriteLine(JsonSerializer.Serialize(device));

MapObject<DeviceDto, Device>.GetMapObject()
    .Ignore(dest => dest.Created)
    .CustomMap(dest => dest.Settings, 
        src => JsonSerializer.Serialize(src.Settings, options))
    .Copy(updateDevice, device);

TestContext?.WriteLine("After");
TestContext?.WriteLine(JsonSerializer.Serialize(device));

How to get new object?

var newDevice = new DeviceDto
{
    Name = "Kitchen temp sensor",
    Settings = new Dictionary<string, object>
    {
        ["Address"] = 1,
        ["Registry"] = 6,
        ["Scale"] = 0.1f
    },
    Created = DateTime.Now
};

Device device = MapObject<DeviceDto, Device>.GetMapObject()
    .CustomMap(dest => dest.Settings, 
        src => JsonSerializer.Serialize(src.Settings, new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            }))
    .Get(newDevice);

Using CopyTo extension method.

var device = new Device
{
    Id = 1,
    Name = "TempSensor",
    Settings = "{}",
    Created = new DateTime(2023, 4, 19, 9, 0, 0),
    Updated = null
};

var updateDevice = new DeviceDto
{
    Name = "Kitchen temp sensor",
    Settings = new Dictionary<string, object>
    {
        ["Address"] = 1,
        ["Registry"] = 6,
        ["Scale"] = 0.1f
    },
    Updated = DateTime.Now,
};

var options = new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

var mapper = MapObject<DeviceDto, Device>.GetMapObject()
    .Ignore(dest => dest.Created)
    .CustomMap(dest => dest.Settings,
        src => JsonSerializer.Serialize(src.Settings, options));

updateDevice.CopyTo(device, mapper);

Use in collections

var deviceDtos = new List<DeviceDto>
{
    new DeviceDto { Name = "device01", Created = DateTime.Now },
    new DeviceDto { Name = "device02", Created = DateTime.Now },
    new DeviceDto { Name = "device03", Created = DateTime.Now },
    new DeviceDto { Name = "device04", Created = DateTime.Now },
    new DeviceDto { Name = "device05", Created = DateTime.Now },
    new DeviceDto { Name = "device06", Created = DateTime.Now },
    new DeviceDto { Name = "device07", Created = DateTime.Now },
    new DeviceDto { Name = "device08", Created = DateTime.Now },
};

var mapper = MapObject<DeviceDto, Device>.GetMapObject()
    .CustomMap(dest => dest.Settings, src => "some settings")
    .Ignore(dest => dest.Updated);

var devices = deviceDtos.Select(dto => mapper.Get(dto));
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • 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.3.3 1,354 8/16/2023
1.0.2 245 4/25/2023
1.0.1 186 4/20/2023