AutoMapper在MVC中的运用05
本篇AutoMapper使用场景:
※ 动态实现接口方法或属性
※ 目标中的属性如果比源多,可以忽略多出的属性
※ 目标有virtual属性,可忽略
※ 目标属性值为null的解决办法
※ int转string
※ bool转string
※ DateTime转String
※ 判断条件
动态实现接口方法或属性
public interface ICreateOrderMessage
{
string CustomerName { get; set; }
}
□ 使用
public ActionResult Dynamic()
{
var order = new {CustomerName = "darren"};
var message = Mapper.DynamicMap<ICreateOrderMessage>(order);
//message中会有CustomerName属性和值
return View(message);
}
□ 要点
传入的实现类或匿名类,动态实现接口方法或属性。
目标中的属性如果比源多,可以忽略多出的属性
□ View model
public class Order3Dto
{
public decimal Amount { get; set; }
}
□ Domain model
public class Order3
{
public int Id { get; set; }
public decimal Amount { get; set; }
}
□ 映射配置
Mapper.CreateMap<Order3Dto, Order3>()
.ForMember("Id", opt => opt.Ignore());
□ 使用
public ActionResult To()
{
var orderDto = new Order3Dto() {Amount = 50m};
//源没有的属性,预先赋值
var order = new Order3() {Id = 4};
Mapper.Map(orderDto, order);
return View(order);
}
□ 要点
如果目标的属性多于源中的属性,并且不赋值,会报错。
目标有virtual属性,可忽略
□ View model
public class UserInfoViewModel
{
public int ID { get; set; }
public string LoginName { get; set; }
}
□ Domain model
public class UserInfo
{
public UserInfo()
{
this.R_UserInfoActionInfo = new HashSet<R_UserInfoActionInfo>();
}
public int ID { get; set; }
public string LoginName { get; set; }
public DateTime SubTime { get; set; } //目标中多出的属性
public virtual ICollection<R_UserInfoActionInfo> R_UserInfoActionInfo { get; set; } //目标中多出的属性,且是virtual
}
public class R_UserInfoActionInfo
{
public string Name { get; set; }
}
□ 映射配置
Mapper.CreateMap<UserInfoViewModel, UserInfo>()
.ForMember("SubTime", opt => opt.Ignore())
.ForMember("R_UserInfoActionInfo", opt => opt.Ignore());
□ 使用
public ActionResult Concern()
{
UserInfoViewModel vmodel = new UserInfoViewModel()
{
LoginName = "Darren"
};
UserInfo dmodel = Mapper.Map<UserInfoViewModel, UserInfo>(vmodel);
dmodel.SubTime = DateTime.Now; //多出的属性虽然在映射配置中忽略了,但还是可以赋值
return View(dmodel);
}
目标属性值为null的解决办法
.ForMember(dto => dto.AuthorBio, opt => opt.NullSubstitute("Not set yet."))
int转string
.ForMember(dto => dto.NumberOfPosts, opt => {
opt.AddFormatter<NumberOfPostsFormatter>();
})
public class NumberOfPostsFormatter : IValueFormatter
{
public string FormatValue(ResolutionContext context)
{
int value = (int)context.SourceValue;
if(value = 1)
return "Number of Posts: One post";
return "Number of posts:" + value;
}
}
bool转string
.ForMember(dto => dto.VIP, m => m.ResolveUsing<VIPResolver>().FromMember(x => x.VIP))
public class VIPResolver : ValueResolver<bool, string>
{
protected override string ResolveCore(bool source)
{
return source ? "Y" : "N";
}
}
DateTime转String
.ForMember(dto => dto.DateOfBirth, m => m.AddFormatter<DateFormatter>());
public class DateFormatter : IValueFormatter
{
public string FormatValue(ResolutionContext context)
{
return ((DateTime)context.SourceValue).ToLongDateString();
}
}
判断条件
Mapper.CreateMap<AutoMapperInMvc01.Portal.Models.Foo, AutoMapperInMvc01.Portal.Models.Bar>()
.ForMember("baz", opt => opt.Condition(context => context.SourceValue.ToString() == "0"));
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。