automapper

AutoMapper之投影

谁都会走 提交于 2020-02-06 06:43:34
7.投影 AutoMapper有一种自定义映射,叫投影。接下来我们通过一个示例来了解它 7.1示例 //源对象 public class CalendarEvent { public DateTime Date { get; set; } public string Title { get; set; } } //目标对象 public class CalendarEventForm { public DateTime EventDate { get; set; } public int EventHour { get; set; } public int EventMinute { get; set; } public string Title { get; set; } } 映射说明:把日期拆开分别映射到不同字段 CalendarEvent中属性 说明 CalendarEventForm的属性 Date Date中的年月日-> EventDate Date Date中小时-> EventHour Date Date中分钟-> EventMinute [TestClass] public class Projection { [TestMethod] public void ProjectionTest() { var calendarEvent = new

AutoMapper(五)

旧时模样 提交于 2020-02-06 06:43:03
返回总目录 Dynamic和ExpandoObject映射 AutoMapper不用任何配置就可以从dynamic(动态)对象映射或映射到dynamic对象。 namespace FifthAutoMapper { //定义一个Person类 public class Person { public int Age { get; set; } public string Name { get; set; } } //主程序 class Program { static void Main(string[] args) { //不需要CreateMap同样可以映射,这就是所谓的“零配置” //Mapper.CreateMap<MyDynamicClass, Person>().ReverseMap(); //将一个动态对象映射到一个普通实例 dynamic dynamicObj = new ExpandoObject();//ExpandoObject对象包含可在运行时动态添加或移除的成员 dynamicObj.Age = 103; dynamicObj.Name = "tkb至简"; Person person = Mapper.Map<Person>(dynamicObj); Console.WriteLine("person.Age={0},Name={1}", person

AutoMapper(三)

十年热恋 提交于 2020-02-06 06:41:33
返回总目录 自定义类型转换 有时,需要完全控制一个类型到另一个类型的转换。一个类型一点都不像另一个类型,而且转换函数已经存在了,在这种情况下,你想要从一个“宽松”的类型转换成一个更强壮的类型,例如一个string的源类型到一个int32的目标类型。 这里有两个类Source和Destination,要把前者映射到后者,代码如下: public class Source { public string Value1 { get; set; } public string Value2 { get; set; } public string Value3 { get; set; } } public class Destination { public int Value1 { get; set; } public DateTime Value2 { get; set; } public Type Value3 { get; set; } } 截至发稿前,官方文档这样描述的 “因为AutoMapper不清楚从string到int,Datetime或Type的映射,如果要尝试映射的话,AutoMapper就会抛出异常(在映射时和配置检查时)”。 为了创建 这些类型的映射,我们必须提供自定义的类型转换器,我们有三种方法这样做: void ConvertUsing(Func<TSource,

AutoMapper : Map both ways in Net Core 2 syntax

末鹿安然 提交于 2020-02-02 15:51:07
问题 What is syntax to map both ways in AutoMapper Net Core2? I need to map ProductViewModel and ProductDto both ways. This is not working, Startup.cs var config = new MapperConfiguration ( cfg => cfg.CreateMap<Models.ProductDto, Models.ProductViewModel>(), cfg => cfg.CreateMap<Models.ProductViewModel, Models.ProductDto>() ); var mapper = config.CreateMapper(); 回答1: I'd rather create a separate initializer and mapper. e.g here is my AutoMapperStartupTask class. public static class

AutoMapper 8.0 missing GetPropertyMaps

南楼画角 提交于 2020-02-02 05:48:08
问题 Prior to AutoMapper 8.0, I used this code to find a property mapping by string, example: entity model has property named "currency_id" and DTO has property named "currency". I have defined bi-directional mapping in AutoMapper, and I used this code to find source/target property relat public static string GetDestinationPropertyFor<TSrc, TDst>(IMapper IMapper, string sourceProperty) { var mapper = AutoMapper.IMapper.ConfigurationProvider; // TSrc = source generic type // TDst = destination

How to map EditModel to Command Messages?

笑着哭i 提交于 2020-01-31 18:20:46
问题 Jimmy Bogard at Los Techies says that he maps his EditModel to Command Messages instead of mapping EditModel to DomainModel. Can anyone explain this further? 回答1: I'd guess it'd be a version of the command pattern that performs the necessary manipulations on the relevant domain objects based on the supplied message. e.g. Something like public PromoteEmployeeCommand : ICommand { private readonly PromotionMessage _message; private readonly IEmployeeRepository _repository; public

UseDestinationValue only when destination property is not null

依然范特西╮ 提交于 2020-01-30 04:50:13
问题 How to configure AutoMapper mapping when I want to use behaviour from UseDestinationValue method, but only when destination property is NOT null . Something like that: Mapper.CreateMap<Item, ItemViewModel>() .ForMember(x => x.Details, _ => _.UseDestinationValue(dontUseWhenNullDestination: true)) EDIT class ItemDetails { public string Info { get; set; } public string ImportantData { get; set; } // only in Domain, not in ViewModel } class Item { public ItemDetails Details { get; set; } } class

ASP.NET Core搭建多层网站架构【8-使用AOP动态拦截器进行服务层日志记录】

给你一囗甜甜゛ 提交于 2020-01-30 02:06:16
2020/01/29, ASP.NET Core 3.1, VS2019 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站架构【7-编写角色业务的增删改查】 编写最简单的增删改业务,涉及到DI依赖注入的使用、AutoMapper的使用、工作单元与仓储的使用、雪花Id的生成 文章目录 此分支项目代码 本章节介绍了编写最简单的增删改查业务,涉及到DI依赖注入的使用、AutoMapper的使用、工作单元与仓储的使用 来源: https://www.cnblogs.com/kasnti/p/12241973.html

AutoMapper Getting started

左心房为你撑大大i 提交于 2020-01-28 04:55:25
AutoMapper 是什么? 为什么要用AutoMapper? 如何使用AutoMapper? 在什么地方配置AutoMapper? 如何测试my mappings? AutoMapper 是什么? AutoMapper是一个对象到对象的映射关系,对象到对象的映射是通过转化一个类型对象输入到一个不同的类型对象输出来工作的,使得AutoMapper让人感兴趣的一点就是他提供了一些比较有趣的规则把繁重的工作从类型对象转换算法中剔除掉, 只要类型对象遵循了AutoMapper建立的规则,基本上不需要额外的配置去做类型对象之间的转化。 为什么要用AutoMapper? 类型转换代码是令人讨厌的,测试类型转换代码更加的令人讨厌,AutoMapper提供了非常简单的配置机制,类型转换测试也变得同样简单,现在真正剩下的需要关心的问题是why use object-object mapping?, 类型转化会发生在项目中很多地方,但是大部分发生在层与层之间的边界,例如 UI/Domain 之间,或者Service/Domain 之间,通常来说一层关心的东西和另外一层关心的东西是不一致的, 所以对象到对象的映射产生了一个隔离的模型,这样每一层的对象的变化更加独立,仅仅会影响对象所在的层,对其他的层不会产生影响 如何使用AutoMapper? 首先,你需要Source类型和目标类型

1.AutoMapper核心:扁平化

自闭症网瘾萝莉.ら 提交于 2020-01-26 23:48:54
对象 - 对象映射的一个常见用法是获取一个复杂的对象模型,并将其展开成一个更简单的模型。 您可以采取复杂的模型,如: 1 public class Order 2 { 3 private readonly IList<OrderLineItem> _orderLineItems = new List<OrderLineItem>(); 4 5 public Customer Customer { get; set; } 6 7 public OrderLineItem[] GetOrderLineItems() 8 { 9 return _orderLineItems.ToArray(); 10 } 11 12 public void AddOrderLineItem(Product product, int quantity) 13 { 14 _orderLineItems.Add(new OrderLineItem(product, quantity)); 15 } 16 17 public decimal GetTotal() 18 { 19 return _orderLineItems.Sum(li => li.GetTotal()); 20 } 21 } 22 23 public class Product 24 { 25 public decimal Price {