[英]AutoMapper - Map using the same source and destination object types
I'm using Automapper to take two objects of the same type and map any new values that have changed. 我正在使用Automapper来获取两个相同类型的对象,并映射已更改的任何新值。 I tried using the code below, but it keeps throwing an error and I'm not even sure if this can even be achieved with Automapper.
我尝试使用下面的代码,但它不断抛出错误,我甚至不确定这是否可以通过Automapper实现。
For example: 例如:
Mapper.CreateMap<UserDetails, UserDetails>();
UserDetails userDetails = Mapper.Map<UserDetails, UserDetails>(userDetailsCurrent, userDetailsNew);
Basically, I need to copy across any new values that come in from the new object "userDetailsNew" to the existing object "userDetailsCurrent" - even though they are of the same type. 基本上,我需要将从新对象“userDetailsNew”进入的任何新值复制到现有对象“userDetailsCurrent” - 即使它们属于同一类型。 This way I can "update" the existing object with the new values.
这样我就可以用新值“更新”现有对象。 The reason I am doing this is because I am not sure what user details will be passed in - I need to map them as and when they arrive.
我这样做的原因是因为我不确定将传递哪些用户详细信息 - 我需要在它们到达时映射它们。
I have normally used Automapper to map different objects with similar properties - but I thought that I could use the power of Automapper to achieve the same thing this way. 我通常使用Automapper来映射具有相似属性的不同对象 - 但我认为我可以使用Automapper的强大功能以这种方式实现相同的功能。 There might even be a better solution - any help would be appreciated!
甚至可能有更好的解决方案 - 任何帮助将不胜感激!
This seems to work for me. 这似乎对我有用。 My custom type:
我的自定义类型:
class MyType
{
public int MyInt { get; set; }
public string MyString { get; set; }
}
My mapping code: 我的映射代码:
Mapper.CreateMap<MyType, MyType>();
var source = new MyType() {MyInt = 1, MyString = "Hello world"};
var dest = Mapper.Map<MyType, MyType>(source);
What is interesting about your custom type beyond simple properties? 除了简单属性之外,您的自定义类型有什么意义?
This can be done using tuples and by creating a custom type converter deriving from Automapper's Abstract TypeConverter class. 这可以使用元组和创建从Automapper的Abstract TypeConverter类派生的自定义类型转换器来完成。
Say you had a source and destination class of: 假设您有源和目标类:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return string.Format("Firstname: {0}, Lastname: {1}", FirstName, LastName);
}
}
Then you could build the custom converter type as 然后你可以构建自定义转换器类型
public class CustomerPersonConverter : TypeConverter<Tuple<Person, Person>, Person>
{
protected override Person ConvertCore(Tuple<Person, Person> source)
{
var orginalValues = source.Item1;
var updatedValues = source.Item2;
var result = new Person
{
FirstName = string.IsNullOrEmpty(updatedValues.FirstName) ? orginalValues.FirstName : updatedValues.FirstName,
LastName = string.IsNullOrEmpty(updatedValues.LastName) ? orginalValues.LastName : updatedValues.LastName
};
return result;
}
}
which could be used like 可以像
var orginal = new Person() {FirstName = "Clifford", LastName = "Mayson"};
var updated = new Person() {FirstName = "Cliff"};
Mapper.CreateMap<Tuple<Person, Person>, Person>().ConvertUsing<CustomerPersonConverter>();
var result = Mapper.Map<Person>(new Tuple<Person, Person>(orginal, updated));
Console.WriteLine(result);
Which would produce the result of keeping the original last name value as that was missing in the update but updating the firstname value eg. 这将产生保持原始姓氏值的结果,因为更新中缺少原始姓氏值,但更新名字值,例如。
Firstname: Cliff, Lastname: Mayson
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.