using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using System.Collections; namespace DanaZhangCms.Domain.AutoMapper
{
public static class AutoMapperExtension
{ /// <summary>
/// 集合对集合
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="self"></param>
/// <returns></returns>
public static List<TResult> MapTo<TResult>(this IEnumerable self)
{
if (self == null) throw new ArgumentNullException();
return (List<TResult>)Mapper.Map(self, self.GetType(), typeof(List<TResult>));
} /// <summary>
/// 对象对对象
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="self"></param>
/// <returns></returns>
public static TResult MapTo<TResult>(this object self)
{
if (self == null) throw new ArgumentNullException();
return (TResult)Mapper.Map(self, self.GetType(), typeof(TResult));
} ///// <summary>
///// 自动Map
///// <para>此方式极易覆盖预期的【已Map】的设置,调用前请确定映射从未被创建</para>
///// </summary>
///// <typeparam name="TResult"></typeparam>
///// <param name="self"></param>
///// <returns></returns>
//[Obsolete("此方式极易覆盖预期的【已Map】的设置,调用前请确定映射从未被创建", false)]
//public static TResult AutoMapTo<TResult>(this object self)
//{
// if (self == null) throw new ArgumentNullException();
// Mapper.CreateMap(self.GetType(), typeof(TResult));
// return (TResult)Mapper.Map(self, self.GetType(), typeof(TResult));
//} /// <summary>
///
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="result"></param>
/// <param name="source"></param>
/// <returns></returns>
public static void MapFrom<TSource, TResult>(this TResult result, TSource source)
{
result = Mapper.Map<TSource, TResult>(source, result);
} }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using DanaZhangCms.Domain.ViewModel;
using DanaZhangCms.Domain.DbModels;
using DanaZhangCms.DbModels; namespace DanaZhangCms.Domain.AutoMapper
{
public class Configuration
{
public static void RegisterConfigure()
{
#region
Mapper.Initialize(
cfg =>
{
cfg.CreateMap<ChannelViewModel, Channel>();
cfg.CreateMap<Channel, ChannelViewModel>(); cfg.CreateMap<ArticleCategoryViewModel, ArticleCategory>();
cfg.CreateMap<ArticleCategory, ArticleCategoryViewModel>(); cfg.CreateMap<ArticleViewModel, Article>();
cfg.CreateMap<Article, ArticleViewModel>(); cfg.CreateMap<Banner, BannerViewModel>();
cfg.CreateMap<BannerViewModel, Banner>();
}
);
#endregion
}
}
}
DanaZhangCms.Domain.AutoMapper.Configuration.RegisterConfigure();

AutoMapperExtension的更多相关文章

  1. EF架构~对AutoMapper实体映射的扩展

    回到目录 AutoMapper在之前我曾经介绍过,今天主要是把它作一下扩展,因为它的调用太麻烦了,呵呵,扩展之后,用着还可以,感觉.net3.5之后,有了扩展方法这个东西,在程序开发速度及表现力上都有 ...

  2. .NET之AutoMapper对象映射工具运用

    AutoMapper对象映射工具:主要是将某一个实体转成另一个实体. 1.引用NuGet包;搜索:AutoMapper 2.创建实体类 using System; using System.Colle ...

  3. Automapper实现自动映射

    出于安全考虑,在后台与前台进行数据传输时,往往不会直接传输实体模型,而是使用Dto(Data transfer object 数据传输对象),这样在后台往前台传递数据时可以省略不必要的信息,只保留必要 ...

  4. C# AutoMapper的简单扩展

    AutoMapper可以很方便的将一个实体的属性值转化给另一个对象.这个功能在我们日常的编码中经常会遇到.我将AutoMapper的一些基本映射功能做成扩展方法,在编码中更方便使用. using Sy ...

  5. Automapper 实现自动映射

    出于安全考虑,在后台与前台进行数据传输时,往往不会直接传输实体模型,而是使用Dto(Data transfer object 数据传输对象),这样在后台往前台传递数据时可以省略不必要的信息,只保留必要 ...

  6. OOM框架AutoMapper基本使用(2)

    出于安全考虑,在后台与前台进行数据传输时,往往不会直接传输实体模型,而是使用Dto(Data transfer object 数据传输对象),这样在后台往前台传递数据时可以省略不必要的信息,只保留必要 ...

  7. 在 ASP.NET Core 项目中使用 AutoMapper 进行实体映射

    一.前言 在实际项目开发过程中,我们使用到的各种 ORM 组件都可以很便捷的将我们获取到的数据绑定到对应的 List<T> 集合中,因为我们最终想要在页面上展示的数据与数据库实体类之间可能 ...

  8. netcore 2.2 封装 AutoMapper

    在上篇中我们通过创建一个类并继承autoMapper的Profile类 public class Mappings : Profile { public Mappings() { CreateMap& ...

  9. AutoMapper扩展帮助类

    /// <summary> /// AutoMapper扩展帮助类 /// </summary> public static class AutoMapperExtension ...

随机推荐

  1. spring-boot集成spring-data-jpa

    参考这个就行, http://blog.csdn.net/wazz753/article/details/72472411 ps:集成过程中pom文件,我加入的内容如下,两个都需要,实体类记得加注解和 ...

  2. SSL证书的类型区别和配置教程

    证书类型 参考:  https://cloud.tencent.com/product/ssl 我们能申请到的免费证书就是DV SSL,个人站长不二之选.免费证书从哪申请,我就介绍几个,具体申请步骤百 ...

  3. SpringBank 开发日志 一种简单的拦截器设计实现

    当交易由Action进入Service之前,需要根据不同的Service实际负责业务的不同,真正执行Service的业务逻辑之前,做一些检查工作.这样的拦截器应该是基于配置的,与Service关联起来 ...

  4. Linux与Windows中的UTC时间

    Linux与Windows中的UTC时间 先介绍几个术语 UTC 协调世界时,又称世界标准时间或世界协调时间,简称UTC(从英文“Coordinated Universal Time”/法文“Temp ...

  5. POJ1151Atlantis 矩形面积并 扫描线 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ1151 题意概括 给出n个矩形,求他们的面积并. n<=100 题解 数据范围极小. 我们分3种 ...

  6. BZOJ1053 [HAOI2007]反素数ant 数论

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 传送门 - BZOJ1053 题目描述 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4.如果某个正 ...

  7. Jetbrains 2018 等系列软件激活破解除去黄色警告框方法(含多个平台)

    现在以IDEA 2018软件在windows为例进行讲解破解方法,其它软件及其在不同平台的破解方法原理 大同小异,有需要的有兴趣的可以尝试一下. 最近更新了Intellij IDEA到2018.1.5 ...

  8. 004.HAProxy的管理与维护

    一 安装 [root@haproxy_master ~]# yum -y install gcc gcc-c++ make openssl-devel wget openssh-clients #安装 ...

  9. 工作->离职->考研

    1.工作篇 去年我大三,理论上来说我应该考研,也必须考研,我当时的想法也是这样.但是不知道什么情况,我竟然选择了工作,连我也没想到的反转,可能当时我对自己的技术很自信?我想可能是,有点对自己技术觉得还 ...

  10. hystrix dashboard Unable to connect to Command Metric Stream解决办法

    spring cloud 在初次使用 hystrix dashboard仪表盘的时候很容易出现hystrix dashboard Unable to connect to Command Metric ...