Sprintor.AutoMapper.MultiTargetMapping
1.0.0
dotnet add package Sprintor.AutoMapper.MultiTargetMapping --version 1.0.0
NuGet\Install-Package Sprintor.AutoMapper.MultiTargetMapping -Version 1.0.0
<PackageReference Include="Sprintor.AutoMapper.MultiTargetMapping" Version="1.0.0" />
paket add Sprintor.AutoMapper.MultiTargetMapping --version 1.0.0
#r "nuget: Sprintor.AutoMapper.MultiTargetMapping, 1.0.0"
// Install Sprintor.AutoMapper.MultiTargetMapping as a Cake Addin #addin nuget:?package=Sprintor.AutoMapper.MultiTargetMapping&version=1.0.0 // Install Sprintor.AutoMapper.MultiTargetMapping as a Cake Tool #tool nuget:?package=Sprintor.AutoMapper.MultiTargetMapping&version=1.0.0
AutoMapper.MultiTargetMapping
A simple multi-target mapping enhancement for AutoMapper.
The Problem
Assuming that we have three types:
public class AModel
{
public string Field1 { get; set; }
public string Field2 { get; set; }
}
public class BModel
{
public string Field3 { get; set; }
public string Field4 { get; set; }
}
public class CModel
{
public string Field5 { get; set; }
public string Field6 { get; set; }
}
And we have defined the mapping [AModel => BModel
] and [AModel => CModel
].
A source object like this:
var source = new AModel()
{
Field1 = "a",
Field2 = "b"
};
If we want to map the source object into serveral other types, we can call the Mapper.Map function servaral times to achieve this, like:
var b = Mapper.Map<AModel, BModel>(source);
var c = Mapper.Map<AModel, CModel>(source);
...
Repeating these codes are so boring.
With the help of System.Tuple
and Destruction assignment in C# 7
features, using this library, we can now wrap things up to:
var (b1, b2) = MultiTargetMapper.Map<Tuple<BModel, BModel>>(this.Source);
// Now b1 and b2 are newly constructed objects which are converted from the source object using AutoMapper, and they are both of type BMdoel.
// No more than 8 items are OK using this way.
var (b3, b4, c1) = MultiTargetMapper.Map<Tuple<BModel, BModel, CModel>>(this.Source);
...
Since the predefined System.Tuple
can hold no more than 8 items, we provide a way to make a dynamic object which contains what you want.
Sadly, since the dynamically created object's type is only known at runtime,and the destruction assignment syntax just works in compilation time, we cannot use a strongly-typed way to do so.
// If you want to map as many as possible destinations, do like this.
// This way you can get more than 8 items while Tuple just holds no more than 8 items.
var destination = MultiTargetMapper.MapDynamic(this.Source,
typeof(BModel), typeof(BModel), typeof(CModel), typeof(CModel));
// The destination object is of dynamic type, so we cannot do a destuction over it to make new variables.
Note: The dynamic
feature is only available on .NET Framework 4.0, .NET Standard 1.1, .NET Standard 2.0 and Portable Class Library, portable-net45+win8+wpa81, Profile 111.
For more general scenarios, we also provide a batch mapping method:
var destinations = MultiTargetMapper.Map(this.Source, typeof(BModel), typeof(BModel), typeof(CModel), typeof(CModel));
// The destinations object is of IList<object>. You can iterate the collection to fetch each destination object. Ordering is the same as the given types array.
Platform Support
This library helps supporting frameworks like:
- .NET Framework 3.5
- .NET Framework 4.0
- .NET Framework 4.7.1
- .NETStandard 1.1
- .NETStandard 2.0
- Windows Phone 8.0 (Silverlight)
- Windows Phone 8.1 (UWP)
- Windows 8 (UWP)
- Silverlight 5
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 | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard1.1 is compatible. netstandard1.2 was computed. netstandard1.3 was computed. netstandard1.4 was computed. netstandard1.5 was computed. netstandard1.6 was computed. netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net35 is compatible. net40 is compatible. net403 was computed. net45 was computed. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 is compatible. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Universal Windows Platform | uap was computed. uap10.0 was computed. |
Windows Phone | wpa81 was computed. |
Windows Store | netcore was computed. netcore45 was computed. netcore451 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 3.5
- AutoMapper (>= 1.1.2)
- NetLegacySupport.Tuple (>= 1.1.1)
-
.NETFramework 4.0
- AutoMapper (>= 3.2.1)
-
.NETFramework 4.7.1
- AutoMapper (>= 3.2.1)
-
.NETStandard 1.1
- AutoMapper (>= 5.0.2)
- NETStandard.Library (>= 1.6.1)
- System.Dynamic.Runtime (>= 4.3.0)
- System.Reflection (>= 4.3.0)
- System.Runtime (>= 4.3.0)
-
.NETStandard 2.0
- AutoMapper (>= 5.0.2)
- System.Dynamic.Runtime (>= 4.3.0)
- System.Reflection (>= 4.3.0)
- System.Runtime (>= 4.3.0)
-
Portable Class Library (.NETFramework 4.0, Silverlight 5.0, Windows 8.0, WindowsPhone 8.0, WindowsPhoneApp 8.1)
- AutoMapper (>= 3.2.1)
-
Portable Class Library (.NETFramework 4.5, Windows 8.0, WindowsPhoneApp 8.1)
- AutoMapper (>= 3.2.1)
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.0 | 569 | 3/21/2019 |