automapper

Inferring destination type from interface with AutoMapper

空扰寡人 提交于 2019-12-06 05:40:28
I'm trying to implement some cross-cutting concerns in my application, which uses AutoMapper to map between different DTOs/message objects. Say I have this: configuration.Map<MyMessage, MyEvent>() . MyEvent implements IEvent (which is a marker interface with no properties). Is there any way to ask AutoMapper to map a MyMessage to IEvent , and have it infer that "oh, I have a mapping MyMessage to MyEvent , and MyEvent implements IEvent "? This (invalid) example shows what I want to achieve: // IEvent is just a marker interface with no properties, // and is implemented by all of the *Event

Autofac, ASP.NET MVC 3 httpRequest scope and AutoMapper: No scope with a Tag matching 'httpRequest' is visible

让人想犯罪 __ 提交于 2019-12-06 05:37:08
When I use a web type registered with autofac from an automapper mapping, I get this error: No scope with a Tag matching 'httpRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself. When another type is resolved in the mapping it works. When a web type is resolved from

How to Initialize AutoMapper Profiles in referenced project DLLs in ASP.Net webapp

爷,独闯天下 提交于 2019-12-06 05:16:44
问题 Struggling a little on how to use automapper in my project class libraries (dlls). See my structure of my overall solution below. The WebApp fires up, and in Global.asax App Start, the AutoMapper.Configure() method is called to add the mapping profiles. For now I am just adding the Services.AutoMapperViewModelProfile. But I need to somehow account for the profiles in each of the WebStoreAdapters (BigCommerce and Shopify in the example below). I was hoping not to add references to each

Calling IMappingEngine.Map inside custom mapping

℡╲_俬逩灬. 提交于 2019-12-06 04:32:17
问题 With AutoMapper, when using ConvertUsing to define a custom mapping for a type that is a container, I often need to call IMappingEngine.Map inside the mapping function. This is necessary because it allows-me to reuse the definition of the child mapping. CreateMap<Order, OrderModel>() .ConvertUsing(o => new OrderModel( o.Id, o.ShippingAddress, mapper.Map<IList<OrderItemModel>>(o.Items) )); In order to do this, I need a reference to IMappingEngine. When the mapping engine is being configured, I

Updated AutoMapper and now receiving unmapped property exception

自古美人都是妖i 提交于 2019-12-06 03:53:09
I have the following mapping: Mapper.CreateMap<Playlist, PlaylistDto>() .ReverseMap() .ForMember(playlist => playlist.Folder, opt => opt.MapFrom(playlistDto => folderDao.Get(playlistDto.FolderId))); Which converts a Playlist object to a PlaylistDto object and back. It seemed to work great before I updated AutoMapper. Now, when I call: Mapper.AssertConfigurationIsValid(); I see: Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type ====================================================

Can automapper ignore destination if it is not null / only change null fields

守給你的承諾、 提交于 2019-12-06 03:29:41
问题 Background: I'm working on a webservice that I want to allow input that has a null field to mean, "don't do an update". The input object is very similar but not identical to the database model, so we're using automapper to do the transforms. So in the case of an update, I'd like to be able to take the existing values, use them to override any of the null fields in the input, and then save that to do the whole update. So is there a way to make automapper only put values into the destination if

AutoMapper Dictionary Flattening

纵然是瞬间 提交于 2019-12-06 02:46:18
问题 I've got a Dictionary<User, bool> User is as follows: public class User { public string Username { get; set; } public string Avatar { get; set; } The second type, bool, indicates whether this user is a friend of the logged in User. I want to flatten this Dictionary into a List<UserDto > UserDto is defined as: public class UserDto { public string Username { get; set; } public string Avatar { get; set; } public bool IsFriend { get; set; } } IsFriend represents the value of the dictionary. How

Configuring AutoMapper to fulfil ITypeConverter<,> constructor dependecies with Autofac

倾然丶 夕夏残阳落幕 提交于 2019-12-06 02:18:19
My first time working with Autofac to inject AutoMapper's IMapper interface into classes that have an object mapping requirement. I have made some progress, with a little help , getting the various dependencies added to AutoMapper's register using Assembly Scanning: builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies()) .AsClosedTypesOf(typeof(ITypeConverter<,>)) .AsImplementedInterfaces(); builder.RegisterAssemblyTypes(typeof(AutoMapperExtensions).Assembly) .AssignableTo<Profile>().As<Profile>(); builder.Register(context => { var profiles = context.Resolve<IEnumerable<Profile>

AutoMapper.AutoMapperMappingException

爷,独闯天下 提交于 2019-12-06 00:34:58
I know a lot of questions have been asked regarding this topic, but none of them provided an answer for my problem. That's why I'm creating a new question. I've looked on google and here for answers and have found some which have improved my setup of AutoMapper. (i.e. only creating your mappings only once) My problem is that I sometimes get the exception and sometimes I don't. And I have absolutely no idea how to solve it. I've been searching a whole day now and I can't find anything. When I run Mapper.AssertConfigurationIsValid();it succeeds. I've tried to add the following: Mapper

AutoMapper - setting destination string to null actually makes it string.Empty

牧云@^-^@ 提交于 2019-12-06 00:04:16
With the following mapping: Mapper.CreateMap<ObjectA, ObjectB>() .ForMember(dest => dest.SomeStringProperty, opt => opt.MapFrom(src => null)) SomeStringProperty is now empty string not null (as I would expect) Is this a bug? How can I get it to actually be null? I see that opt.Ignore() will make it null but I actually want to do a conditional null like the following and the above simplified bug(?) is preventing this Mapper.CreateMap<ObjectA, ObjectB>() .ForMember(dest => dest.SomeStringProperty, opt => opt.MapFrom(src => src.SomeOtherProp != null ? src.SomeOtherProp.Prop1 : null)) I found the