AutoMapper Profile用法
2024-09-08 06:49:09
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper; namespace MvcAutoMapper.AutoMapper
{
public class Configuration
{
public static void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<UserProfile>();
});
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper; namespace MvcAutoMapper.AutoMapper.Profiles
{
public class UserProfile:Profile
{ protected override void Configure()
{
CreateMap<Models.User, Models.UserDto>(); }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper; namespace MyMvc.AutoMapper
{
public static class AutoMapperForMvc
{
public static void Register()
{
Mapper.Initialize(x =>
{
x.AddProfile<UserProfile>();
}); //在程序启动时对所有的配置进行严格验证
Mapper.AssertConfigurationIsValid();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper; namespace MyMvc.AutoMapper
{
public class UserProfile:Profile
{
public UserProfile()
{
base.CreateMap<Models.User, Models.UserView>()
//映射前
.BeforeMap((s, t) =>
{
s.Age += 10;
})
//映射后
.AfterMap((s, t) =>
{
t.Age += 10;
})
//条件判断
//年龄不大于10 不映射年龄这个属性的值,那值就是默认的0,但是映射后+10,所以就是10
.ForMember(t=>t.Age,o=>o.Condition(s=>s.Age>30))
//空值
.ForMember(t=>t.Name, o=>o.NullSubstitute("无名氏"))
.ForMember(x=>x.time,x=>x.MapFrom(s=>s.add_time))
//反向映射
.ReverseMap();
}
}
}
最新文章
- arm,iptables: No chain/target/match by that name.
- 转:在VS2010下编译、调试和生成mex文件
- Weblogic监控指标
- Android 摇一摇 之 震动片
- JS学习笔记10_Ajax
- 转:SDL2源代码分析
- Ubuntu下配置C/C++开发环境
- 设计模式之 -- 单例模式(Singleton)
- maven的中传递依赖,maven的依赖管理(转)
- 【LEETCODE OJ】Binary Tree Preorder Traversal
- C# 必应代码搜索
- [C++]对象的销毁机制
- Struts2配置RESULT中TYPE的参数说明
- 红豆带你从零学C#系列之:开始C#编程(一)
- 实战!基于lamp安装Discuz论坛-技术流ken
- fastJson 之 JSONPath使用
- 集群LVS
- hook mono实现Assembly.Load从指定路径读取文件
- Beta任务项录入
- solr学习四(关于性能的杂知识)