Skip to content

Instantly share code, notes, and snippets.

@philippdolder
Created November 25, 2014 06:52
Show Gist options
  • Save philippdolder/9c73807fe6fbfaffacbb to your computer and use it in GitHub Desktop.
Save philippdolder/9c73807fe6fbfaffacbb to your computer and use it in GitHub Desktop.
AutoMapper constructor
public class Test
{
[Fact]
public void MapChildViewModel()
{
Mapper.CreateMap<ViewModel, Dto>();
var childViewModel = new ChildViewModel
{
ChildId = 5,
ChildName = "Child"
};
var viewModel = new ViewModel
{
Name = "Parent",
Id = 1,
Child = childViewModel,
Additional = "Foo"
};
Dto map = Mapper.Map<ViewModel, Dto>(viewModel);
map.Id.Should().Be(1);
map.Name.Should().Be(viewModel.Name);
map.ChildName.Should().Be(childViewModel.ChildName);
}
}
public class ViewModel
{
public string Name { get; set; }
public int Id { get; set; }
public string Additional { get; set; }
public ChildViewModel Child { get; set; }
}
public class ChildViewModel
{
public int ChildId { get; set; }
public string ChildName { get; set; }
}
public class Dto
{
public Dto(int id, string name, string childName)
{
this.Id = id;
this.Name = name;
this.ChildName = childName;
}
public int Id { get; private set; }
public string Name { get; private set; }
public string ChildName { get; private set; }
}
public class Test
{
[Fact]
public void MapChildViewModel()
{
Mapper.CreateMap<ViewModel, Dto>();
var childViewModel = new ChildViewModel
{
Id = 5,
Name = "Child"
};
var viewModel = new ViewModel
{
Name = "Parent",
Id = 1,
Child = childViewModel,
Additional = "Foo"
};
Dto map = Mapper.Map<ViewModel, Dto>(viewModel);
map.Id.Should().Be(1);
map.Name.Should().Be(viewModel.Name);
map.ChildName.Should().Be(childViewModel.Name);
}
}
public class ViewModel
{
public string Name { get; set; }
public int Id { get; set; }
public string Additional { get; set; }
public ChildViewModel Child { get; set; }
}
public class ChildViewModel
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Dto
{
public Dto(int id, string name, string childName)
{
this.Id = id;
this.Name = name;
this.ChildName = childName;
}
public int Id { get; private set; }
public string Name { get; private set; }
public string ChildName { get; private set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment