AnyMapper 1.0.31

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

// Install AnyMapper as a Cake Tool
#tool nuget:?package=AnyMapper&version=1.0.31                

AnyMapper

nuget nuget Build status Codacy Badge

A CSharp library mapping alternative to AutoMapper with built-in support for Entity Framework 6 and Entity Framework Core. The terse mapper!

Description

AnyMapper was built as a standalone object mapper that runs on either .Net Framework or .Net Core. The API is backwards compatible with AutoMapper so switching between the two is nearly seamless.

Differences from AutoMapper

AnyMapper will automatically map between objects of different types as long as they have the same names and the types are the same or are different but convertable automatically.

Examples

Simple usage:

using AnyMapper;
var destObject = Mapper.Map<SourceObject, DestObject>(sourceObject);

The above example will map the type SourceObject to type DestObject. Any fields and properties with the same name will be mapped, recursively. You can also map the same type to create a new copy of the data:

var sourceObjectCloned = Mapper.Map<SourceObject, SourceObject>(sourceObject);

For all of the examples below we will use the following test classes:

// *** classes used in all the examples ***
public class SourceObject
{
  public string Name { get; set; }
  public int Id { get; set; }
  public DateTime DateCreated { get; set; }
  public ICollection<SimpleObject> Items { get; set; }
}

public class DestObject
{
  public string Name { get; set; }
  public int Id { get; set; }
  public DateTime DateCreated { get; set; }
  public string Description { get; set; }
  public bool IsEnabled { get; set; }
  public ICollection<SimpleObject> Items { get; set; }
}

// our custom mapping profile that indicates how one object maps to another
public class MyMappingProfile : Profile
{
  public MyMappingProfile()
  {
    CreateMap<SourceObject, DestObject>()
      .ForMember(x => x.Id, x => x.Id)
      .ForMember(x => x.Name, x => x.Name)
      .ForMember(x => x.DateCreated, x => x.DateCreated)
    ;
  }
}

Map one object to another:


// configure our mapping profile
var profile = new MyMappingProfile();
Mapper.Configure(config =>
{
  config.AddProfile(profile);
});

// map one object to another
var sourceObject = new SourceObject { Id = 1, Name = "Source object", DateCreated = new DateTime(2018, 1, 1) };
var destObject = Mapper.Map<SourceObject, DestObject>(sourceObject);
// output
// destObject.Id = 1
// destObject.Name = "Source object"
// destObject.DateCreated = "2018-01-01 00:00:00"
// destObject.Description = null
// destObject.IsEnabled = false
// destObject.Items = null

Implicitly map (no specified profile) two different objects with similar properties, only matching property names will get mapped:

var sourceObject = new SourceObject { Id = 1, Name = "Source object", DateCreated = new DateTime(2018, 1, 1) };
var destObject = Mapper.Map<SourceObject, DestObject>(sourceObject);

// output
// destObject.Id = 1
// destObject.Name = "Source object"
// destObject.DateCreated = "2018-01-01 00:00:00"
// destObject.Description = null
// destObject.IsEnabled = false
// destObject.Items = null

Profiles

AnyMapper supports scanning for profiles in the current assembly using the following setup:

Mapper.Initialize();

You can also specify profiles manually if you don't want to scan for them:

var profiles = new List<Profile>();
profiles.Add(new MyProfile());
profiles.Add(new CustomerProfile());
profiles.Add(new TypesProfile());
Mapper.Initialize(profiles);

Some additional options for finding profiles:

// scan specified assemblies
var myAssembly1 = Assembly.Load(...assemblyPath);
var myAssembly2 = Assembly.Load(...assemblyPath);
Mapper.Initialize(myAssembly1, myAssembly2);

// scan all assemblies in the application (performance will suffer in a large application)
Mapper.Initialize(MappingOptions.ScanAllAssemblies);
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. 
.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 is compatible.  net462 is compatible.  net463 was computed.  net47 is compatible.  net471 was computed.  net472 was computed.  net48 is compatible.  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.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETFramework 4.6.2

    • No dependencies.
  • .NETFramework 4.7

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on AnyMapper:

Repository Stars
replaysMike/Binner
Open source parts inventory system for makers, electronics hobby, and professional engineers
Version Downloads Last updated
1.0.34 17,026 3/7/2021
1.0.33 405 3/7/2021
1.0.32 380 3/7/2021
1.0.31 398 3/6/2021
1.0.30 409 3/6/2021
1.0.28 676 7/10/2020
1.0.27 634 7/10/2020
1.0.24 566 7/9/2020
1.0.23 5,021 4/22/2020
1.0.20 873 1/15/2020
1.0.19 645 10/25/2019
1.0.18 505 10/20/2019
1.0.9 2,342 5/16/2019
1.0.4 618 5/15/2019
1.0.0 949 12/18/2018