Skip to content

Instantly share code, notes, and snippets.

@ferventcoder
Created September 22, 2011 21:20
Show Gist options
  • Save ferventcoder/1236092 to your computer and use it in GitHub Desktop.
Save ferventcoder/1236092 to your computer and use it in GitHub Desktop.
How do you create a map without generics that can ignore destination properties? Automapper v1.1
public class Object1
{
public int Id { get; set; }
public string FirstField { get; set; }
public string SecondField { get; set; }
}
public class WhenMappingTwoItemsOfTheSameType : Spec
{
protected Object1 original;
protected Object1 update;
public override void Context()
{
original = new Object1
{
Id = 123,
FirstField = "a",
SecondField = "1"
};
update = new Object1
{
Id = 0,
FirstField = "b",
SecondField = "2"
};
//Mapper.CreateMap(original.GetType(), original.GetType()).ForMember("Id",opt => opt.Ignore()); //doesn't compile
}
public override void Because()
{
//original = Mapper.Map(update, original);
Mapper.DynamicMap(update, original);
}
[Fact]
public void ShouldNotReplaceId()
{
original.Id.ShouldEqual(123); //fails
}
[Fact]
public void ShouldReplaceFirstField()
{
original.FirstField.ShouldEqual(update.FirstField);
}
}
@ferventcoder
Copy link
Author

The solution? Write an extension or abstraction on top of the Dynamic Map that will catch the fields you want to keep and set them back. Not pretty...but it works. :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment