https://github.com/replaysmike/anymapper
A CSharp library mapping alternative to AutoMapper with built-in support for Entity Framework. The terse mapper!
https://github.com/replaysmike/anymapper
automapper automatic-mapping entity-framework mapper no-registration terse
Last synced: about 1 month ago
JSON representation
A CSharp library mapping alternative to AutoMapper with built-in support for Entity Framework. The terse mapper!
- Host: GitHub
- URL: https://github.com/replaysmike/anymapper
- Owner: replaysMike
- License: gpl-3.0
- Created: 2018-12-09T02:22:52.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-03-07T02:06:29.000Z (about 5 years ago)
- Last Synced: 2025-06-14T17:04:37.339Z (10 months ago)
- Topics: automapper, automatic-mapping, entity-framework, mapper, no-registration, terse
- Language: C#
- Size: 131 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AnyMapper
[](https://www.nuget.org/packages/AnyMapper/)
[](https://www.nuget.org/packages/AnyMapper/)
[](https://ci.appveyor.com/project/MichaelBrown/anymapper)
[](https://app.codacy.com/app/replaysMike/AnyMapper?utm_source=github.com&utm_medium=referral&utm_content=replaysMike/AnyMapper&utm_campaign=Badge_Grade_Dashboard)
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:
```csharp
using AnyMapper;
var destObject = Mapper.Map(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:
```csharp
var sourceObjectCloned = Mapper.Map(sourceObject);
```
For all of the examples below we will use the following test classes:
```csharp
// *** 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 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 Items { get; set; }
}
// our custom mapping profile that indicates how one object maps to another
public class MyMappingProfile : Profile
{
public MyMappingProfile()
{
CreateMap()
.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:
```csharp
// 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);
// 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:
```csharp
var sourceObject = new SourceObject { Id = 1, Name = "Source object", DateCreated = new DateTime(2018, 1, 1) };
var destObject = Mapper.Map(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:
```csharp
Mapper.Initialize();
```
You can also specify profiles manually if you don't want to scan for them:
```csharp
var profiles = new List();
profiles.Add(new MyProfile());
profiles.Add(new CustomerProfile());
profiles.Add(new TypesProfile());
Mapper.Initialize(profiles);
```
Some additional options for finding profiles:
```csharp
// 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);
```