您的位置:首页 > 移动开发

【AutoMapper】实体类间自动实现映射关系,及其转换。

2014-05-08 09:52 429 查看
官方项目下载:
http://automapper.codeplex.com/
博文
http://www.iteye.com/blogs/tag/AutoMapper
图解:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Agathas.Storefront.Services.ViewModels
{
public class OrderSummaryView
{
public int Id { get; set; }
public DateTime Created { get; set; }
public bool IsSubmitted { get; set; }
}

}


View Code

第二步:启用配置:

protected void Application_Start()
{

Services.AutoMapperBootStrapper.ConfigureAutoMapper();


第三步:使用:OrderMapper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agathas.Storefront.Model.Orders;
using Agathas.Storefront.Services.ViewModels;
using AutoMapper;

namespace Agathas.Storefront.Services.Mapping
{
public static class OrderMapper
{
public static OrderView ConvertToOrderView(this Order order)
{
return Mapper.Map<Order, OrderView>(order);
}

public static IEnumerable<OrderSummaryView> ConvertToOrderSummaryViews(
this IEnumerable<Order> orders)
{
return Mapper.Map<IEnumerable<Order>, IEnumerable<OrderSummaryView>>(orders);
}
}

}


在需要两个类型转换的地方调用:

Order order = new 。。。;
OrderView orderView = order.ConvertToOrderView();


OrderSummaryViews orderSummaryViews = new .....;
IEnumerable<OrderSummaryView> Orders = orderSummaryViews.ConvertToOrderSummaryViews()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: