Simple and fast object mapper (using Expression Trees API) to map data between 2 objects which have identical (or nearly identical) shapes.
- Cloning.
- Data archiving (moving data from the active table to the archived table).
Install the package from NuGet:
dotnet add package MapItEasy
Or using the NuGet Package Manager in Visual Studio:
Install-Package MapItEasy
IMapper _mapper = new ExpressionMapper();
[Fact]
public void ReturnNewObject()
{
var source = new A { Id = 1, Name = "abc1", Description = "xyz1" };
var target = _mapper.Map<A, B>(source);
Assert.Equal(1, target.Id);
Assert.Equal("abc1", target.Name);
Assert.Equal("xyz1", target.Description);
}
[Fact]
public void MapExistingObject()
{
var source = new A { Id = 1, Name = "abc1", Description = "xyz1" };
var target = new B();
_mapper.Map(source, target);
Assert.Equal(1, target.Id);
Assert.Equal("abc1", target.Name);
Assert.Equal("xyz1", target.Description);
}
[Fact]
public void MapProperties()
{
var source = new A { Id = 1, Name = "abc1", Description = "xyz1" };
var target = new B();
_mapper.MapProperties(source, target, x => new { x.Name });
Assert.Equal(0, target.Id);
Assert.Equal("abc1", target.Name);
Assert.Null(target.Description);
}
[Fact]
public void MapExclude()
{
var source = new A { Id = 1, Name = "abc1", Description = "xyz1" };
var target = new B();
_mapper.MapExclude(source, target, x => new { x.Name });
Assert.Equal(1, target.Id);
Assert.Null(target.Name);
Assert.Equal("xyz1", target.Description);
}
MapItEasy is licensed under the MIT license.