I have the following classes.
public class Person {
public string Name { get; set; }
public Address Address { get; set; }
}
public class PersonDTO {
public string Name { get; set; }
public Address Address { get; set; }
}
I create a standard mapping using
Mapper.CreateMap<Person, PersonDTO>();
Then I'd like to map a Person into an existing PersonDTO hierarchy in that way that the existing Address will be updated instead of reference copied if you know what I mean.
var person = new Person() {
Name = "Test",
Address = new Address() {
Country = "USA",
City = "New York"
}
};
var personDTO = new PersonDTO() {
Name = "Test2",
Address = new Address() {
Country = "Canada",
City = "Ottawa"
}
};
Mapper.Map(person, personDTO);
I'd like to fulfill the following tests.
Assert.AreNotEqual(person.Address, personDTO.Address);
Assert.AreEqual(person.Address.Country, personDTO.Address.Country);
Assert.AreEqual(person.Address.City, personDTO.Address.City);
Just create a map between Address and itself like this:
Mapper.CreateMap<Address, Address>();
Please note that the following test:
Assert.AreNotEqual(person.Address, personDTO.Address);
Might not give you want you want if Address defines an Equals method. From what I understand from the question, you want to check for reference equality.
If you are using NUnit, you should use Assert.AreNotSame.
In general you can use object.ReferenceEquals to check for reference equality like this:
bool same_object = object.ReferenceEquals(person.Address, personDTO.Address);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With