automapper

netcore 2.2 封装 AutoMapper

孤街醉人 提交于 2019-12-06 11:03:15
在上篇中我们通过创建一个类并继承autoMapper的Profile类 public class Mappings : Profile { public Mappings() { CreateMap<UserDto, TbUser>(); } } 这样做有一种弊端,就是当传输对象很多的时候,还需要手动一个个的配置映射关系,维护起来相对来说也比较麻烦。 封装AutoMapper 创建AutoMapperExtension扩展类 /// <summary> /// AutoMapper扩展类 /// </summary> public static class AutoMapperExtension { /// <summary> /// 将源对象映射到目标对象 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="obj">源对象</param> /// <returns>转化之后的实体</returns> public static T MapTo<T>(this object obj) { if (obj == null) throw new Exception("异常"); var config = new MapperConfiguration(ctx => ctx.CreateMap

netcore 2.2 使用 AutoMapper 实现实体之间映射

…衆ロ難τιáo~ 提交于 2019-12-06 11:02:09
一、什么是AutoMapper? AutoMapper是一个简单的对象映射框架(OOM),将一个对象映射到另一个对象。 二、AutoMapper的好处 以前的时候我们将DTO对象转换为Model对象时,我们必须将每一个属性都手动映射 实体类 /// <summary> /// 用户表 /// </summary> [Table("tb_User")] public class TbUser { /// <summary> /// 用户Id /// </summary> [Key] [Column("userId")] [StringLength(32)] public string UserId { get; set; } /// <summary> /// 用户名 /// </summary> [Column("userName")] [StringLength(20)] public string UserName { get; set; } /// <summary> /// 邮箱 /// </summary> [Column("email")] [StringLength(30)] public string Email { get; set; } /// <summary> /// 添加时间 /// </summary> [Column("addTime")]

AutoMapper map IdPost to Post

点点圈 提交于 2019-12-06 08:29:31
I'm trying to map int IdPost on DTO to Post object on Blog object, based on a rule. I would like to achieve this: BlogDTO.IdPost => Blog.Post Post would be loaded by NHibernate: Session.Load(IdPost) How can I achieve this with AutoMapper? You could define AfterMap action to load entities using NHibernate in your mapping definition. I'm using something like this for simmilar purpose: mapperConfiguration.CreateMap<DealerDTO, Model.Entities.Dealer.Dealer>() .AfterMap((src, dst) => { if (src.DepartmentId > 0) dst.Department = nhContext.CurrentSession.Load<CompanyDepartment>(src.DepartmentId); if

Automapper Mapping for Localization Resolver in a Multi-Language Website

和自甴很熟 提交于 2019-12-06 07:18:24
I found a way to use Automapper for Language mapping based on the active Culture. The question is if it's possible to build a generic Resolver to map all the models that use the Resolver. In this Case, the models to map have always the same properties, Id and Name (including the language properties Name_PT, Name_FR and Name_EN): // MODELS public class MakeDto { // Primary properties public int Id { get; set; } public string Name { get; set; } public string Name_PT { get; set; } public string Name_FR { get; set; } public string Name_EN { get; set; } } public class MakeViewModel { // Primary

AutoMapper unflattening complex objects of same type

老子叫甜甜 提交于 2019-12-06 07:17:09
I have a problem mapping the following complex types: RequestDTO { int OldUserId; string OldUsername; int NewUserId; string NewUsername; } Request { User OldUser; User NewUser; } User { int UserId; string Username; } Mapping/flattening Request to RequestDTO is easy. But how to unflatten this object? Assuming that you make your classes and fields public, the example below shows how to handle this in AutoMapper. You need to tell it how to reconstruct your complex types from the dto. Mapper.CreateMap<RequestDTO, Request>() .ForMember(request => request.OldUser, mappingOption => mappingOption

Automapper - concrete object to array

霸气de小男生 提交于 2019-12-06 07:04:35
问题 I need to map some values from a class to an array. For example: public class Employee { public string name; public int age; public int cars; } must be converted to [age, cars] I tried with this var employee = new Employee() { name = "test", age = 20, cars = 1 }; int[] array = new int[] {}; Mapper.CreateMap<Employee, int[]>() .ForMember(x => x, options => { options.MapFrom(source => new[] { source.age, source.cars }); } ); Mapper.Map(employee, array); but i get this error: Using mapping

Automapper, generics, dto funtimes

↘锁芯ラ 提交于 2019-12-06 06:54:54
问题 Here's the deal: I have a report designer where users can create reports based on some predefined datasets. They can select a set of columns to include in the report and then, when the report is ran, an IList is created by mapping the NHibernate collection over to the dto class collection using automapper. The problem with this is that the DTO collection has a load of redundant columns in as it will be populated with all the data regardless of whether or not it's needed. My solution to this?

Is a mapping library like AutoMapper available on the .NET Compact Framework 3.5?

橙三吉。 提交于 2019-12-06 06:18:35
Is anyone working on a .NET Compact Framework port of AutoMapper or are there any similar mapping libraries for the .NET Compact Framework? Ruben Bartelink You could build on OmuMapper ? (UPDATE: @Omu himself has since built ValueInjecter ). (Obviously there's a strong chance this wont meet many challenges you might place in front of it. Are you really interested in all the AM features? Is size an issue? Any more context you can give?) EDIT: As they've RTW'd over at AutoMapper Maybe now is the perfect time to ask the question in their forum while the eyes of the world are watching. EDIT 2: 5

AutoMapping custom Generic Types - How?

倖福魔咒の 提交于 2019-12-06 05:57:04
hey guys, I'm using automapper version 1.1.0.188 In my AutoMapper.Configure I'm mapping Entities to DTOs and vice versa, like so: // entity >> DTO Mapper.CreateMap<MetaTemplate, MetaTemplateDTO>(); Mapper.CreateMap<Person, PersonDTO>(); // DTO >> Entity Mapper.CreateMap<MetaTemplateDTO, MetaTemplate>(); Mapper.CreateMap<PersonDTO, Person>(); When I do the below mappings (and vice versa) everything works fine Mapper.Map<entity, entityDTO>(entity); Mapper.Map<List<entity>, List<entityDTO>>(entities); Note above that automapper just works with List<> without me having to configure anything. I

Technique for carrying metadata to View Models with AutoMapper

◇◆丶佛笑我妖孽 提交于 2019-12-06 05:50:07
问题 I use AutoMapper to map my domain objects to my view models. I have metadata in my domain layer, that I would like to carry over to the view layer and into ModelMetadata. (This metadata is not UI logic, but provides necessary information to my views). Right now, my solution is to use a separate MetadataProvider (independently of ASP.NET MVC), and use conventions to apply the relevant metadata to the ModelMetadata object via an AssociatedMetadataProvider. The problem with this approach is that