Skip to content

Instantly share code, notes, and snippets.

@ahmad2x4
Last active August 29, 2015 14:06
Show Gist options
  • Save ahmad2x4/acb5669edd84beb8aab2 to your computer and use it in GitHub Desktop.
Save ahmad2x4/acb5669edd84beb8aab2 to your computer and use it in GitHub Desktop.
Blog autpmapper expression
public class Source
{
public string FullName { get; set; }
public int NumberOfYears { get; set; }
}
public class Destination
{
[Key]
public int UserId { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class AutomapperQueriableTest
{
public AutomapperQueriableTest()
{
Mapper.CreateMap<Source, Destination>()
.ForMember(d => d.Name, opt => opt.MapFrom(s => s.FullName))
.ForMember(d => d.Age, opt => opt.MapFrom(s => s.NumberOfYears));
Mapper.CreateMap<Destination, Source>()
.ForMember(d => d.FullName, opt => opt.MapFrom(s => s.Name))
.ForMember(d => d.NumberOfYears, opt => opt.MapFrom(s => s.Age));
}
[Fact]
public void SimpleMappingTest()
{
var fixture = new Fixture();
var source = fixture.Create<Source>();
var destination = Mapper.Map<Destination>(source);
Assert.Equal(source.FullName, destination.Name);
Assert.Equal(source.NumberOfYears, destination.Age);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment