AutoMapper在MVC中的运用05-映射中的忽略、处理null、多种映射转换

本篇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"));

上一篇:Ceph分布式存储实2.1 Ceph功能模块与RADOS


下一篇:iOS开发UI篇—UIScrollView控件实现图片缩放功能