Skip to content

Simple and fast object mapper (using Expression Trees API) to map data between 2 objects which have identical (or nearly identical) shapes, useful for scenarios like cloning or data archiving, ...

License

Notifications You must be signed in to change notification settings

phongnguyend/MapItEasy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MapItEasy

Simple and fast object mapper (using Expression Trees API) to map data between 2 objects which have identical (or nearly identical) shapes.

Use Cases

  • Cloning.
  • Data archiving (moving data from the active table to the archived table).

Installation

Install the package from NuGet:

dotnet add package MapItEasy

Or using the NuGet Package Manager in Visual Studio:

Install-Package MapItEasy

Examples

IMapper _mapper = new ExpressionMapper();

Map all possible properties

[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);
}

Map all possible selected properties

[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);
}

Map all possible properties except selected properties

[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);
}

License

MapItEasy is licensed under the MIT license.

About

Simple and fast object mapper (using Expression Trees API) to map data between 2 objects which have identical (or nearly identical) shapes, useful for scenarios like cloning or data archiving, ...

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages