使用 Automapper 基于内置数据类型创建对象
Make an object base on built-in data types with Automapper
我有两个 类 命名为 ApprenticeshipDto
一个 Apprenticeship
如下所示
public class ApprenticeshipDto
{
public int Id { get; set; }
public string PersonFirstName { get; set; }
public string PersonLastName { get; set; }
}
public class Apprenticeship
{
public virtual int Id { get; set; }
public Person Person { get; set; }
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
这就是我尝试将 ApprenticeshipDto 映射到 Apprenticeship 的方式:
Mapper.CreateMap<ApprenticeshipDto, Apprenticeship>();
Apprenticeship a = Mapper.Map<Apprenticeship>(Apprdto);
问题是所有属性都已正确映射,但 Person
未正确映射。
是否可以告诉 Automapper 创建 Person 属性 并自动将 PersonFirstName
和 PersonLastName
分配给 Person 对象的 FirstName
和 LastName
?
是的,可以通过使用 custom value resolver class 告诉 AutoMapper 将 DTO 属性映射到目标对象 属性。解析器将 PersonFirstName
和 PersonLastName
属性映射到新的 Person
对象:
// create the resolver class
// -> extract person info from DTO and return a new Person object
public class CustomResolver : ValueResolver<ApprenticeshipDto, Person>
{
protected override Person ResolveCore(ApprenticeshipDto source)
{
return new Person
{
FirstName = source.PersonFirstName,
LastName = source.PersonLastName
};
}
}
然后在映射之前在 AutoMapper 配置中使用新的自定义解析器 class:
Mapper.CreateMap<ApprenticeshipDto, Apprenticeship>();
var adto = new ApprenticeshipDto
{
Id = 10,
PersonFirstName = "John",
PersonLastName = "Doe"
};
// configure custom mapping
Mapper.CreateMap<ApprenticeshipDto, Apprenticeship>()
.ForMember(destination => destination.Person, opt => opt.ResolveUsing<CustomResolver>());
Apprenticeship a = Mapper.Map<Apprenticeship>(adto);
Console.WriteLine("{0} - {1} - {2}", a.Id, a.Person.FirstName, a.Person.LastName);
输出符合预期:
10 - John - Doe
我有两个 类 命名为 ApprenticeshipDto
一个 Apprenticeship
如下所示
public class ApprenticeshipDto
{
public int Id { get; set; }
public string PersonFirstName { get; set; }
public string PersonLastName { get; set; }
}
public class Apprenticeship
{
public virtual int Id { get; set; }
public Person Person { get; set; }
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
这就是我尝试将 ApprenticeshipDto 映射到 Apprenticeship 的方式:
Mapper.CreateMap<ApprenticeshipDto, Apprenticeship>();
Apprenticeship a = Mapper.Map<Apprenticeship>(Apprdto);
问题是所有属性都已正确映射,但 Person
未正确映射。
是否可以告诉 Automapper 创建 Person 属性 并自动将 PersonFirstName
和 PersonLastName
分配给 Person 对象的 FirstName
和 LastName
?
是的,可以通过使用 custom value resolver class 告诉 AutoMapper 将 DTO 属性映射到目标对象 属性。解析器将 PersonFirstName
和 PersonLastName
属性映射到新的 Person
对象:
// create the resolver class
// -> extract person info from DTO and return a new Person object
public class CustomResolver : ValueResolver<ApprenticeshipDto, Person>
{
protected override Person ResolveCore(ApprenticeshipDto source)
{
return new Person
{
FirstName = source.PersonFirstName,
LastName = source.PersonLastName
};
}
}
然后在映射之前在 AutoMapper 配置中使用新的自定义解析器 class:
Mapper.CreateMap<ApprenticeshipDto, Apprenticeship>();
var adto = new ApprenticeshipDto
{
Id = 10,
PersonFirstName = "John",
PersonLastName = "Doe"
};
// configure custom mapping
Mapper.CreateMap<ApprenticeshipDto, Apprenticeship>()
.ForMember(destination => destination.Person, opt => opt.ResolveUsing<CustomResolver>());
Apprenticeship a = Mapper.Map<Apprenticeship>(adto);
Console.WriteLine("{0} - {1} - {2}", a.Id, a.Person.FirstName, a.Person.LastName);
输出符合预期:
10 - John - Doe