automapper

AutoMapper: create instance of destination type if source == null

淺唱寂寞╮ 提交于 2019-12-05 16:10:50
问题 Is it possible to configure AutoMapper to return a new instance of the destination type if the source object is null? Source source = null; Dest d1 = AutoMapper.Mapper.Map<Source, Dest>(source); // d1 == null // I'm looking for a way to configure AutoMapper to // eliminate this code: Dest d2 = AutoMapper.Mapper.Map<Source, Dest>(source) ?? new Dest(); 回答1: Answering my own question (partially): AutoMapper has a configuration property named AllowNullDestinationValues which is set to true by

Cannot resolve AutoMapper.IMapper using AutoMapper 4.2 with Autofac

北城以北 提交于 2019-12-05 13:38:39
I have tried various permutations of this but my current configuration (as it relates to AutoMapper) is like this: builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile)).As<Profile>(); builder.Register(c => new MapperConfiguration(cfg => { foreach (var profile in c.Resolve<IEnumerable<Profile>>()) { cfg.AddProfile(profile); } })).AsSelf().SingleInstance(); builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope(); builder.RegisterType<MappingEngine>().As<IMappingEngine>(); I have a constructor using IMapper mapper , however

Automapper enum to Enumeration Class

£可爱£侵袭症+ 提交于 2019-12-05 13:02:41
I'm trying to use Automapper to map from a regular enum to an Enumeration Class (as described by Jimmy Bogard - http://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/ ). The regular enum doesn't have the same values as the enumeration class does. I would therefore like to map using the Name if possible: Enum: public enum ProductType { ProductType1, ProductType2 } Enumeration Class: public class ProductType : Enumeration { public static ProductType ProductType1 = new ProductType(8, "Product Type 1"); public static ProductType ProductType2 = new ProductType(72, "Product Type 2");

Automapper map from one object to nested objects

ⅰ亾dé卋堺 提交于 2019-12-05 12:58:25
What is the best way to map inner objects with Automapper 2.0 Use the solution in this question (Automapper 1.0) Create a Custom Value Resolvers ? public class DTOObject { // MainObject public int Id { get; set; } public string Name { get; set; } // SubObject (TopObject) public string TopText { get; set; } public string TopFont { get; set; } // SubObject (BottomObject) public string BottomText { get; set; } public string BottomFont { get; set; } } public class MainObject { public int Id { get; set; } public string Name { get; set; } public SubObject TopObject { get; set; } public SubObject

Mapping readonly child collection with AutoMapper

夙愿已清 提交于 2019-12-05 11:23:40
问题 It's not so much a question, as I have found a way to do what I want, but it seems like there should be a better way to do it. I've searched everywhere and not found anything. Basically, I have what I consider a very standard object model. public class Parent { private readonly IList<Child> _children = new List<Child>(); public IEnumerable<Child> Children { get { return _children; } } public void AddChild(Child child) { child.Parent = this; _children.Add(child); } } and public class Child {

automapper, mapping to an interface

落花浮王杯 提交于 2019-12-05 10:31:33
I am using automapper (for .net 3.5). Here is an example to illustrate what I am trying to do: I want to map an A object to a B object. Class definitions: class A { public I1 MyI { get; set; } } class B { public I2 MyI { get; set; } } interface I1 { string StringProp1 { get; } } interface I2 { string StringProp1 { get; } } class CA : I1 { public string StringProp1 { get { return "CA String"; } } public string StringProp2 { get; set; } } class CB : I2 { public string StringProp1 { get { return "CB String"; } } public string StringProp2 { get; set; } } The mapping code: A a = new A() { MyI = new

AutoMapper auto create createMap

痴心易碎 提交于 2019-12-05 10:06:59
问题 I have a services that is calling another services. Both of the services are using "the same classes". The classes are named same and have the same properties but has different namespace so I need to use AutoMapper to map from one of the type to the other type. No it's pretty simple since all I have to do is the CreateMap<> , but the problem is that we have around hundreds of classes that I manually needs to write the CreateMap<> from, and it's works wired to me. Isn't there any Auto

Is it a good practice to mock Automapper in unit tests?

别说谁变了你拦得住时间么 提交于 2019-12-05 09:14:41
There is this codebase where we use automapper and have 2 layers, Domain and Service . Each has its object for data representation, DomainItem and ServiceItem . The service gets data from domain, the uses constructor injected automapper instance to map class Service { public ServiceItem Get(int id) { var domainItem = this.domain.Get(id); return this.mapper.Map<DomainItem, ServiceItem>(domainItem); } } Assume best practices, so mapper has no side-effects and no external dependencies. You'd write a static function to convert one object to another within seconds, just mapping fields. With this in

Using AutoMapper to unflatten a DTO

断了今生、忘了曾经 提交于 2019-12-05 08:33:44
I've been trying to use AutoMapper to save some time going from my DTOs to my domain objects, but I'm having trouble configuring the map so that it works, and I'm beginning to wonder if AutoMapper might be the wrong tool for the job. Consider this example of domain objects (one entity and one value): public class Person { public string Name { get; set; } public StreetAddress Address { get; set; } } public class StreetAddress { public string Address { get; set; } public string City { get; set; } public string State { get; set; } } My DTO (from a Linq-to-SQL object) is coming out looking roughly

Automapper 3.0 - This type is not supported on this platform IMapperRegistry

荒凉一梦 提交于 2019-12-05 08:30:18
问题 I updated my project to use Automapper 3.0.0 and now my TFS build is not succeeding. The error is the following: " ...System.PlatformNotSupportedException: System.PlatformNotSupportedException: This type is not supported on this platform IMapperRegistry. " Is there anyone that can help me resolve this issue. In the mean time, I am going to revert to previous version since that one seems to work fine. 回答1: We had the same issue on our build server. MsTest seemed to remove DLLs it deemed