ObjectMapper.psxbox
1.0.3.3
dotnet add package ObjectMapper.psxbox --version 1.0.3.3
NuGet\Install-Package ObjectMapper.psxbox -Version 1.0.3.3
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.3.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ObjectMapper.psxbox --version 1.0.3.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ObjectMapper.psxbox, 1.0.3.3"
#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.3.3 // Install ObjectMapper.psxbox as a Cake Tool #tool nuget:?package=ObjectMapper.psxbox&version=1.0.3.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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));
Changes
1.0.3
- New
CopyTo
andConvertTo
extension methods. If you want to use without custom map/ignore properties, use these methods. Example:objectA.ConvertTo<TypeB>()
.
Product | Versions 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | 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.
-
.NETStandard 2.1
- 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.