automapper

Unit test the Automapper profiles

◇◆丶佛笑我妖孽 提交于 2019-12-10 01:10:10
问题 I do want to test the custom logic in the CreateMap method. I do NOT want to test whether the mapping exist at all for some types. How can I do that or what are the classes that I need to know. I am grateful for every hint The document about. Automapper unit testing seems very rare... public class UnitProfile : Profile { protected override void Configure() { // Here I create my maps with custom logic that needs to be tested CreateMap<Unit, UnitTreeViewModel>() .ForMember(dest => dest.IsFolder

Is AutoMapper case sensitive or insensitive?

浪子不回头ぞ 提交于 2019-12-10 00:45:48
问题 If object a has a property named 'Id' and object b has a property named 'ID', will AutoMapper correctly map the two properties (without doing a .ForMember(...) call)? 回答1: The trunk version is now default case-insensitive, and supports multiple naming conventions (camelCase, lowercase_underscore, etc). Look for this in the next version of AutoMapper, which should drop in a couple of days. 回答2: No, last time I tried it (month or so ago) it was case sensitive. 来源: https://stackoverflow.com

How can I use a dataset in Automapper?

做~自己de王妃 提交于 2019-12-09 18:51:09
问题 I am currently using a datareader as the source but I want to instead use a dataset. //datareader AutoMapper.Mapper.CreateMap<IDataReader, AccountDTO>() .ForMember(m => m.AccountId, opt => opt.MapFrom (r => r.GetInt32(r.GetOrdinal("AccountId")))) .ForMember(m => m.ParentAccountId, opt => opt.MapFrom(r => r.GetInt32(r.GetOrdinal("ParentAccountId")))) .ForMember(m => m.IsInactive, opt => opt.MapFrom(r => r.GetString(r.GetOrdinal("IsInactive")))) .ForMember(m => m.AccountName, opt => opt.MapFrom

How to Transfer DataAnnotation metadata to ViewModel with AutoMapper with Betty's Approach

梦想与她 提交于 2019-12-09 18:31:44
问题 I need clarification on how to implement Betty's code solution to transferring data annotation metadata to ViewModels with AutoMapper (see here). Or if you have a better way, please share that. Maybe the implementation of Betty's answer is obvious to someone who knows AutoMapper well, but I'm new to it. Here is a simple example, what do I add to this code to make Betty's solution work: // Data model Entity public class User1 { [Required] public int Id { get; set; } [Required] [StringLength(60

Automapper map custom collections

烂漫一生 提交于 2019-12-09 18:29:21
问题 Hello. I have a list that looks like this one: public class PagedList<T> : List<T> { public PagedList(IEnumerable<T> collection) : base(collection) { } public int TotalItems { get; set; } public int CurrentPage { get; set; } public int PageSize { get; set; } //some other properties } and used in repository for paging public PagedList<TEntity> GetPaged(int page) { var pagedEntities = some_query; return pagedEntities.AsPagedList(totalResults, page, pageSize); } The same PagedList is also used

Automapper v5 Ignore unmapped properties

时间秒杀一切 提交于 2019-12-09 16:30:46
问题 Previously when I used Automapper v3.x ignoring unmapped properties could be done by simply adding a .IgnoreUnmappedProperties() extension which looked like this public static class AutoMapperExtensions { public static IMappingExpression<TSource, TDestination> IgnoreUnmappedProperties<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression) { var typeMap = Mapper.FindTypeMapFor<TSource, TDestination>(); if (typeMap != null) { foreach (var unmappedPropertyName in

automapper map dynamic object

笑着哭i 提交于 2019-12-09 13:31:18
问题 I am working with Automapper and need to achieve the following mapping but not sure how it can be done. I want to map a Dictionary object to a dynamic object, so that the key is the property on the object and the value of the dictionary is the value of property in dynamic object. Can this be achieve with automapper and if so, how? 回答1: You can simply get Dictionary from ExpandoObject and fill it with original dictionary values void Main() { AutoMapper.Mapper.CreateMap<Dictionary<string,

Automapper Convention

泄露秘密 提交于 2019-12-09 13:11:38
问题 Is is possible with Automapper to setup a convention so that maps do not have to be created by hand for situations where the entity you are mapping to just has say "ViewModel" appended. As an example I would rather not have to setup the following map: Mapper.CreateMap<Error, ErrorViewModel>(); I understand if projection is required that I would need to create a custom map, but having a convention to create maps would be nice. 回答1: You would need to use Mapper.DynamicMap<TDest>(source) to map.

Automapper - want case sensitive

泄露秘密 提交于 2019-12-09 10:43:00
问题 From similar questions on here I have read here that AutoMapper used to be case sensitive, but is now case insensitive. I want it case sensitive - can't see any way to change this, and none of the other questions Re this showed how to do it (I did look). Any ideas anyone? Thanks 回答1: You can Refer : DataReaderMapper should create case-insensitive mappings by default http://automapper.codeplex.com/workitem/6127 you can control this in Mapper.Initialize as the answer AutoMapper: Mapping between

Force throwing of exception when a source property is unmapped

╄→гoц情女王★ 提交于 2019-12-09 10:11:51
问题 In AutoMapper 2.2.1, is there any way I can configure my mappings so that when a property is not explicitly ignored, an exception is thrown? For example, I have the following classes and configuration: public class Source { public int X { get; set; } public int Y { get; set; } public int Z { get; set; } } public class Destination { public int X { get; set; } public int Y { get; set; } } // Config Mapper.CreateMap<Source, Destination>(); The behavior I receive with this configuration is that