Created
September 22, 2011 21:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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