AutoMapper 初次使用心得
本例以asp.net webform为例:
结构:

主要代码:AutoMapperConfig 类
public class AutoMapperConfig
{
public static void RegisterAutoMapper()
{
AutoMapper.Configuration.Configure();
}
}
Configuration类:
public static void Configure()
{
Mapper.Initialize(cfg =>
{
var typeToAddProfile = Assembly.GetExecutingAssembly().GetTypes();
foreach (var item in typeToAddProfile)
{
if (item.BaseType != null && item.BaseType.Name == "Profile" && !item.BaseType.Name.Equals("Configuration"))
{
object o = Activator.CreateInstance(item);
cfg.AddProfile(o as Profile);
}
}
});
Mapper.AssertConfigurationIsValid();
}
Profiles 下 TestProfiles类:
public class TestProfiles : Profile
{
protected override void Configure()
{
CreateMap<TestModel, DTO_TestModel>();
//.ForMember(a => a.Address, b => b.MapFrom(c => c.Name));
}
}
Golbal 中:
protected void Application_Start(object sender, EventArgs e)
{
AutoMapperWeb.AutoMapperConfig.RegisterAutoMapper();
}
protected void Page_Load(object sender, EventArgs e)
{
var t1 = new List<TestModel>()
{
new TestModel(){Id=1,Name="张三",Age=25,Sex="男",Address="湖南省长沙市雨花区长房天翼未来城"},
new TestModel(){Id=1,Name="李四",Age=25,Sex="男",Address="湖南省长沙市雨花区长房天翼未来城",QQ="9375671",Email="93759671@qq.com",Phone="15725032"}
};
var list = Mapper.Map<List<TestModel>, List<DTO_TestModel>>(t1);
string str = string.Format("<div><hr/>");
foreach (var item in list)
{
str += string.Format("姓名:{0},<br/>",item.Name);
str += string.Format("家庭住址:{0},<br/>", item.Address);
str += string.Format("QQ:{0},<br/>", item.QQ);
str += string.Format("邮箱:{0}<hr/>", item.Email);
}
str += string.Format("</div>");
Response.Write(str);
Response.End();
}
AutoMapper 初次使用心得的更多相关文章
- 201671010142 <java程序设计>初次学习心得与感悟
从开始对JDK的配置就遇到了问题,从这点就可以知道自己知识的薄弱.又知道了在控制台下一些常用命令的掌握.对知识的理解挺艰难,比如遇到一个新的问题就不知道该从哪里入手,有时候还不知道到底问题是啥.接受能 ...
- LINQPad 4 初次使用心得
最近学习EntityFramework,于是接触了LinqPad这款享誉已久的软件,深感相见恨晚.软件具体不多做介绍了,只简单介绍下使用方法. 数据库操作 添加数据库连接 1,首先通过点击Add co ...
- 自我介绍和Github初次使用心得
姓名:许洪科 班级:网络工程142 学号:1413042047 爱好:足球 编写过的程序:0 编写过的代码:几乎为0(大多数为网上直接复制的) Github注册过程:. 1:进入Github网站后点击 ...
- (LaTex)CTex的初次使用心得及入门教程
摘要 最近要发论文了,被知乎里人推荐使用论文编译软件(CTex.LaTex和Overleaf之类),瞬间感觉自己用Word简直Out了(书读少). 学校里也听说过LaTex,不过因为当时没怎么写过论文 ...
- 手机web——自适应网页设计(html/css控制)
一. 允许网页宽度自动调整: "自适应网页设计"到底是怎么做到的? 其实并不难. 首先,在网页代码的头部,加入一行viewport元标签. <meta name=" ...
- 【转】手机web——自适应网页设计(html/css控制)
手机web——自适应网页设计(html/css控制) 就目前形势来看,Web App 正是眼下的一个趋势和潮流,但是,对于Web App的设计可能大家有的不是很了解,下面就将整理好的网页设计的技巧奉献 ...
- 手机web——自适应网页设计(html/css控制) - 51CTO.COM
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- 手机web——自适应网页设计(html/css控制)【转】
一. 允许网页宽度自动调整: "自适应网页设计"到底是怎么做到的?其实并不难. 首先,在网页代码的头部,加入一行viewport元标签. <meta name="v ...
- 手机web——自适应网页设计(html/css控制)http://mobile.51cto.com/ahot-409516.htm
http://mobile.51cto.com/ahot-409516.htm 一. 允许网页宽度自动调整: "自适应网页设计"到底是怎么做到的? 其实并不难. 首先,在网页代码的 ...
随机推荐
- Soft Actor-Critic: Off-Policy Maximum Entropy Deep Reinforcement Learning with a Stochastic Actor
Soft Actor-Critic: Off-Policy Maximum Entropy Deep Reinforcement Learning with a Stochastic Actor 20 ...
- Grid数字或金额千分位或保留两位小数
formatter: 'number', formatoptions: { thousandsSeparator: "", decimalPlaces: 2 }
- postgres安装pg_buffercache扩展
1.查看是否安装了pg_buffercache postgres=# \dx List of installed extensions Name | Version | Schema | Descri ...
- semi-join子查询优化 -- FirstMatch策略
FirstMatch执行semi-join子查询的一种策略. 类似于MySQL 5.x中如何执行in.exists子查询. 让我们以搜索拥有大城市的国家为例: select * from Countr ...
- EasyUi datagrid列表增加复选框
本文为博主原创,未经允许不得转载 1.增加复选框列 { field: 'oid', title: '<input type=\"checkbox\" name ...
- Spring boot使用Aspose.Slides操作ppt转PDF、转图片
最近要将ppt转为PDF和图片,Apache poi ,jacob都试了下 Apache poi 转图片乱码,处理了,还会存在部分乱码 jacob对系统依赖比较大,必须是windows还得安装MS O ...
- tween算法
tween算法 https://www.cnblogs.com/cloudgamer/archive/2009/01/06/Tween.html 参数说明: t: current time:当前时间: ...
- idea2018.1.1版激活码到2020.7
N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjoid3UgYW5qdW4iLCJhc3NpZ25lZU5hbWUiO ...
- LeetCode 151. 翻转字符串里的单词(Reverse Words in a String)
151. 翻转字符串里的单词 151. Reverse Words in a String
- [转帖]在 k8s 中自动为域名配置 https
在 k8s 中自动为域名配置 https https://juejin.im/post/5db8d94be51d4529f73e2833 随着 web 的发展,https 对于现代网站来说是必不可少的 ...