automapper

AutoMapper 4.2 and Ninject 3.2

戏子无情 提交于 2019-12-09 09:05:58
问题 I'm updating a project of mine to use AutoMapper 4.2, and I'm running into breaking changes. While I seem to have resolved said changes, I'm not entirely convinced I've done so in the most appropriate way. In the old code, I have a NinjectConfiguration , and an AutoMapperConfiguration class that are each loaded by WebActivator. In the new version the AutoMapperConfiguration drops out and I instead instance a MapperConfiguration directly in the NinjectConfiguration class where the bindings are

AutoMapper: How to parse an Int from a String and possible to creating rules based on data type?

不羁的心 提交于 2019-12-09 05:21:58
问题 I have two models for my form, a ViewModel going to it, and a ControlModel coming from it. The ControlModel has all the same field names and hierarchy, but all of the fields are a string data type. How would you code AutoMapper to convert a string field to integer? I tried Int32.Parse(myString) but Int32 is not available within the expression (gives an error). Mapper.CreateMap<SourceClass, DestinationClass>() .ForMember(dest => dest.myInteger, opt => opt.MapFrom(src => src.myString)); The

Conditional projection using AutoMapper

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 03:36:21
问题 Say I have a 'Comment' property on a 'Message' class. I also have 2 class properties which have a 'Body' property. If the class has either of the class properties set, I want AutoMapper to project the Body property into the comment property of the model, otherwise use the normal comment property on the message class. e.g. public class Message { public string Comment { get; set; } public Inbound? InboundMessage { get; set; } public Outbound? OutboundMessage { get; set; } } public class Inbound

Avoid constructor mapping fields

感情迁移 提交于 2019-12-08 21:05:04
问题 I am using AutoMapper 6.2.2 with .NET Core 2.0 and its default dependency injection mechanism to map between models and DTOs. I need DI in my AutoMapper configs because I have to perform an AfterMap<Action> that needs some injected components. The thing is, for some models that have constructors which parameters match some source member, when I enable DI for AutoMapper (add services.AddAutoMapper() ), these constructors are by default called and fed with data, that then breaks my operations

Automapper Profiles not being loaded in Startup?

Deadly 提交于 2019-12-08 17:13:42
问题 I'm using: AutoMapper 6.1.1 AutoMapper.Extensions.Microsoft.DependencyInjection 3.0.1 It seems my profiles are not being loaded, every time I call the mapper.map I get AutoMapper.AutoMapperMappingException: 'Missing type map configuration or unsupported mapping.' Here my Startup.cs class ConfigureServices method // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(); //register

AutoMapper: What is the difference between ForMember() and ForPath()?

雨燕双飞 提交于 2019-12-08 17:07:39
问题 I am reading AutoMapper's ReverseMap() and I can not understand the difference between ForMember() and ForPath() . Implementations was described here. In my experience I achieved with ForMember() . See the following code where I have configured reverse mapping: public class Customer { public string Surname { get; set; } public string Name { get; set; } public int Age { get; set; } } public class CustomerDto { public string CustomerName { get; set; } public int Age { get; set; } } static void

Automapper null string to empty

和自甴很熟 提交于 2019-12-08 15:20:43
问题 When I try to map an object that has a null string property, the destination is also null. Is there a global settings I can turn on that says all null string should be mapped to empty? 回答1: Something like this should work: public class NullStringConverter : ITypeConverter<string, string> { public string Convert(string source) { return source ?? string.Empty; } } And in your configuration class: public class AutoMapperConfiguration { public static void Configure() { Mapper.CreateMap<string,

Automapper - CreateMap called multiple times

血红的双手。 提交于 2019-12-08 15:12:54
问题 What happens when I call Mapper.CreateMap with the same types multiple times? Does it rewrite the previous map? If so, is it possible to make it throw an exception if I try to create map, that is already created? 回答1: When calling Mapper.CreateMap for the same set of source and destination several times, nothing will happen at all as the Mapper.CreateMap<TSource, TDestination>() does not set up any extensions for a mapping configuration. If you set the overrides for IMappingExpression like

AutoMapper define mapping level

大兔子大兔子 提交于 2019-12-08 14:59:43
问题 public class Foo { public string Baz { get; set; } public List<Bar> Bars { get; set; } } When I map the class above, is there any way to define how deep I want automapper to map objects? Some pseudo code of what I'm after: var mapped = Mapper.Map<FooDTO>(foo, opt => { levels: 0 }); // result = { Baz: "" } var mapped = Mapper.Map<FooDTO>(foo, opt => { levels: 1 }); // result = { Baz: "", Bars: [{ Blah: "" }] } var mapped = Mapper.Map<FooDTO>(foo, opt => { levels: 2 }); // result = { Baz: "",

AutoMapper - What's difference between Condition and PreCondition

与世无争的帅哥 提交于 2019-12-08 14:57:49
问题 Suppose a mapping using AutoMapper like bellow: mapItem.ForMember(to => to.SomeProperty, from => { from.Condition(x => ((FromType)x.SourceValue).OtherProperty == "something"); from.MapFrom(x => x.MyProperty); }); What's difference of substitute Condition by PreCondition: from.PreCondition(x => ((FromType)x.SourceValue).OtherProperty == "something"); What's the practical difference between this two methods? 回答1: The diference is that PreCondition is executed before acessing the source value