How to scan and auto-configure profiles in AutoMapper?

给你一囗甜甜゛ 提交于 2020-01-20 14:21:40
问题 Is there any way to auto-configue Automapper to scan for all profiles in namespace/assembly? What I would like to do is to add mapping profiles to AutoMapper from given assembly filtered by given interface, something like Scan Conventions in StructureMap: public static void Configure() { ObjectFactory.Initialize(x => { // Scan Assembly x.Scan( scanner => { scanner.TheCallingAssembly(); scanner.Convention<MyCustomConvention>(); scanner.WithDefaultConventions(); }); // Add Registries x

Map xml string property to C# properties

两盒软妹~` 提交于 2020-01-17 01:19:05
问题 I need to map a xml property to c# properties. var src = new Source(); src.Id = 1; src.Name = "Test"; src.Address = "<Country>MyCountry</Country><Prefecture>MyPrefecture</Prefecture><City>MyCity</City>"; class Source { public string ID{ get; set; } public string Name{ get; set; } public string Address{ get; set; } } Class Destination { public string ID{ get; set; } public string Name{ get; set; } public string Country { get; set;} public string Prefecture { get; set;} public string City { get

AutoMapper : Site wide usage of IValueFormatter for given types

岁酱吖の 提交于 2020-01-16 03:43:09
问题 It is my understanding I can configure AutoMapper in the following way and during mapping it should format all source model dates to the rules defined in the IValueFormatter and set the result to the mapped model. ForSourceType<DateTime>().AddFormatter<StandardDateFormatter>(); ForSourceType<DateTime?>().AddFormatter<StandardDateFormatter>(); I get no effect for my mapped class with this. It only works when I do the following: Mapper.CreateMap<Member, MemberForm>().ForMember(x => x

How to reverse map to an existing parent object using AutoMapper?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-15 09:13:32
问题 Consider the following classes: public class Customer { public Guid Id { get; set; } public List<Address> Addresses { get; set; } public List<Order> Orders { get; set; } } public class Address { public Guid Id { get; set; } public Customer Customer { get; set; } // parent // ... } public class Order { public Guid Id { get; set; } public Customer Customer { get; set; } // parent public Address Address { get; set; } // ... } And the data transfer objects (DTOs): public class CustomerDto {

AutoMapper null source value and custom type converter, fails to map?

时光总嘲笑我的痴心妄想 提交于 2020-01-14 13:07:50
问题 When using a custom type converter (ITypeConverter) with AutoMapper it does not seem to enter the type converters code if the source value is null , e.g: Mapper.CreateMap<string, Enum>().ConvertUsing<EnumConverter>(); Assert.AreEqual(Enum.Value1, Mapper.Map<Enum>("StringValue1")); Assert.AreEqual(Enum.Value1, Mapper.Map<Enum>(null); Assert.AreEqual(Enum.Value1, Mapper.Map<Enum?>(null); The type converter looks something like: public class EnumConvertor: ITypeConverter<string, Enum> { public

How to Map Object / Class Property to Dictionary When Object Property And Dictionary Key Names Are Different?

…衆ロ難τιáo~ 提交于 2020-01-14 06:29:09
问题 I am calling Bloomberg Server API (for Stock Market Data) and getting Data back in a Dictionary<string, object> Where the Key to the dictionary is the Field Name on Bloomberg's side, and the object contains the data value from Bloomberg and can be string , decimal , DateTime , boolean etc. After I get the Bloomberg Data, I need to populate my strong type entities / classes with the values returned. Depending on what Field Names I send in my request to bloomberg, the returned Dictionary could

AutoMapper unflattening complex objects of same type

十年热恋 提交于 2020-01-13 19:42:12
问题 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? 回答1: 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

Inject service into an AutoMapper destination class

萝らか妹 提交于 2020-01-13 08:40:07
问题 Say I have a source and destination class that is mapped using AutoMapper. The destination has a logger service injected into the constructor. However, I don't know how to get the service injected into the constructor through StructureMap? I've tried the following: Mapper.Initialize(m => { m.ConstructServicesUsing(ObjectFactory.GetInstance); }); which didn't prevent me having the exception on the mapping call, I guess because the service isn't being injected in properly. I also tried the

AutoMapper registration in Unity DI

[亡魂溺海] 提交于 2020-01-13 04:55:21
问题 I'm not that familiar with Unity or StructureMap. How do you convert the following StructureMap registration sample into Unity registration syntax? public class ConfigurationRegistry : Registry { public ConfigurationRegistry() { ForRequestedType<ConfigurationStore>() .CacheBy(InstanceScope.Singleton) .TheDefault.Is.OfConcreteType<ConfigurationStore>() .CtorDependency<IEnumerable<IObjectMapper>>().Is(expr => expr.ConstructedBy(MapperRegistry.AllMappers)); ForRequestedType

Using AutoMapper with Data Reader

两盒软妹~` 提交于 2020-01-12 19:08:54
问题 I went through How can I easily convert DataReader to List<T>? I wanted to implement something like what is accepted as an answer in the above link. Scenrio: I am using OdbcDataReader to retrieve from the database. And I have a Model Class . FYI , the properties of this class are exact replica of the column names from the database. I need to map these columns to the properties and return List Can this be accomplished using Automapper. 回答1: Something like this public List<T> ReadData<T>(string