A CSharp library mapping alternative to AutoMapper with built-in support for Entity Framework 6 and Entity Framework Core. The terse mapper!
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.
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.
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
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);