AutoMapper -- One-to-many mapping of tree - automapper

I have the following classes:
public class OneToManySource {
public OneToManySource SourceChild { get;set; } // will sometimes be null
public int Value { get; set; }
}
public interface IDestination
{
}
public class ChildlessDestination: IDestination
{
public int DestValue { get; set; }
}
public class ChildedDestination: IDestination
{
public int DestValue { get; set; }
public IDestination DestinationChild { get; set; } // Never null. If it would be null, use a ChildlessDestination instead.
}
I want to map these back and forth in the sensible way. If a source has a child, it goes to a ChildedDestination. Otherwise, it goes to a ChildlessDestination.
I have the following, which works, but it's ugly. I'm wondering if it can be cleaned up. In particular, the "ConstructUsing" gets the job done, but it also seems to be (understandably) opaque to AutoMapper's internal smarts. So ReverseMap() there doesn't work. Instead of that, we are stuck with two other ReverseMap() calls, and also the last CreateMap().
For example, would I be better off starting with the reverse map?
public MapperConfigurationExpression Configure(MapperConfigurationExpression expression) {
expression.CreateMap<OneToManySource, ChildedDestination>()
.ForMember(d => d.DestValue, cfg => cfg.MapFrom(s => s.Value))
.ForMember(d => d.DestinationChild, opt => opt.MapFrom(s => s.SourceChild))
.ReverseMap();
expression.CreateMap<OneToManySource, ChildlessDestination>()
.ForMember(d => d.DestValue, cfg => cfg.MapFrom(s => s.Value))
.ReverseMap();
expression.CreateMap<OneToManySource, IDestination>()
.ConstructUsing((source, context) =>
{
if (source.SourceChild == null) {
return context.Mapper.Map<ChildlessDestination>(source);
}
return context.Mapper.Map<ChildedDestination>(source);
});
expression.CreateMap<IDestination, OneToManySource>()
.ConstructUsing((source, context) =>
{
return context.Mapper.Map<OneToManySource>(source);
});
return expression;
}

Related

What expression is needed for mapping this property?

I am trying to map two objects that are mostly similar with AutoMapper but one member (AudioSummary) raises the following exception :
The following property on EchoNestModel.AudioSummary cannot be mapped: AudioSummary
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type EchoNestModel.AudioSummary.
Context:
- Mapping to property AudioSummary from EchoNest.Api.AudioSummary to EchoNestModel.AudioSummary
- Mapping from type EchoNest.Api.TrackProfile to EchoNestModel.Profile
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.
Mapping definition
var map = Mapper.CreateMap<TrackProfile, Profile>();
map.ForMember(dest => dest.ForeignIds, opt => opt.ResolveUsing<ForeignIdResolver>());
map.ForMember(dest => dest.ForeignReleaseIds, opt => opt.ResolveUsing<ForeignReleaseIdResolver>());
map.ForMember(s => s.Media, t => t.Ignore());
map.ForMember(s => s.ProfileId, t => t.Ignore());
map.ForMember(s => s.AudioSummary, t => t.MapFrom(s => s.AudioSummary));
I've added the following two lines but a totally different error occurs :
map.ForMember(s => s.AudioSummary.Profile, t => t.Ignore());
map.ForMember(s => s.AudioSummary.AudioSummaryId, t => t.Ignore());
Expression 's => s.AudioSummary.Profile' must resolve to top-level member and not any child object's properties.
Use a custom resolver on the child type or the AfterMap option instead.
Parameter name: lambdaExpression
How can I successfully map AudioSummary ?
Source object
Target object
EDIT: In general, try AutoMapper.Mapper.AssertConfigurationIsValid();, this will show you all possible problems in your mapper setup.
From the information you provided, it looks like you need to define map for the AudioSummary classes (dest and source) as well:
[TestFixture]
public class MappingTest
{
public class SourceAudioSummary
{
public int Id { get; set; }
public string OtherData { get; set; }
}
public class TrackProfile
{
public string Whatever { get; set; }
public SourceAudioSummary AudioSummary { get; set; }
}
public class DestAudioSummary
{
public int Id { get; set; }
public string OtherData { get; set; }
}
public class Profile
{
public string Whatever { get; set; }
public DestAudioSummary AudioSummary { get; set; }
}
[Test]
public void Mapping()
{
Mapper.CreateMap<SourceAudioSummary, DestAudioSummary>();
Mapper.CreateMap<TrackProfile, Profile>();
var trackProfile = new TrackProfile
{
Whatever = "something",
AudioSummary = new SourceAudioSummary
{
Id = 1,
OtherData = "other"
}
};
var profile = Mapper.Map<TrackProfile, Profile>(trackProfile);
Assert.That(profile.Whatever == "something");
Assert.That(profile.AudioSummary.Id == 1);
Assert.That(profile.AudioSummary.OtherData == "other");
}
}

Automapper automatic mapping

I have a Domain model that countains the following properties :
public class Profile
{
public DateTime birthDate { get; set; }
public List<Language> languages { get; set; }
}
Where Language represent an enumeration defined here :
public enum Language
{
English,
French,
Spannish
}
i would like to automatically populate the two following properties based on the languages values stored inside my domain model :
public Dictionary <int, String> languages_list { get; set; }
public List<string> languages_known { get; set; }
the question is : can it be done using automapper , if so how should i proceed ?
OK, using the detail you've provided this is what I've come up with.
Classes
public class SOProfile
{
public DateTime birthDate { get; set; }
public List<Language> languages { get; set; }
}
public class WhatAmI
{
public Dictionary<int, String> languages_list { get; set; }
public List<string> languages_known { get; set; }
}
Note that Profile was renamed to SOProfile to avoid a clash with AutoMapper
AutoMapper Configuration
public class MyProfile : Profile
{
public override string ProfileName
{
get
{
return "MyProfile";
}
}
protected override void Configure()
{
Mapper.CreateMap<SOProfile, WhatAmI>()
.ForMember(dest => dest.languages_list,
opt => opt.MapFrom(
src => src.languages
.Select((x,i) => new { Item = x, Index = i})
.ToDictionary(x => x.Index,
x => x.Item.ToString())))
.ForMember(dest => dest.languages_known,
opt => opt.MapFrom(
src => src.languages
.Select(x => x.ToString()).ToList()));
}
}
Unit Tests
[TestFixture]
public class MappingTests
{
[Test]
public void AutoMapper_Configuration_IsValid()
{
Mapper.Initialize(m => m.AddProfile<MyProfile>());
Mapper.AssertConfigurationIsValid();
}
[Test]
public void AutoMapper_Mapping_IsValid()
{
Mapper.Initialize(m => m.AddProfile<MyProfile>());
Mapper.AssertConfigurationIsValid();
var profile = new SOProfile
{
birthDate = new DateTime(2012, 01, 01),
languages = new List<Language>
{
Language.English,
Language.French,
Language.English,
Language.French
}
};
var rc = Mapper.Map<SOProfile, WhatAmI>(profile);
Assert.That(rc, Is.Not.Null);
Assert.That(rc.languages_known, Is.Not.Null);
Assert.That(rc.languages_known.Count, Is.EqualTo(4));
Assert.That(rc.languages_known.Count(x => x == "English"),
Is.EqualTo(2));
Assert.That(rc.languages_known.Count(x => x == "French"),
Is.EqualTo(2));
Assert.That(rc.languages_known.Count(x => x == "Spanish"),
Is.EqualTo(0));
Assert.That(rc.languages_list, Is.Not.Null);
Assert.That(rc.languages_list.Count, Is.EqualTo(4));
Assert.That(rc.languages_list.First(x => x.Key == 0).Value,
Is.EqualTo("English"));
Assert.That(rc.languages_list.First(x => x.Key == 1).Value,
Is.EqualTo("French"));
Assert.That(rc.languages_list.First(x => x.Key == 2).Value,
Is.EqualTo("English"));
Assert.That(rc.languages_list.First(x => x.Key == 3).Value,
Is.EqualTo("French"));
}
}

AutoMapper resolver not passing expected type

Given the following classes and resolver, why am I getting this error? I don't see why ProductAddModel is being passed in at all.
AutoMapper.AutoMapperMappingException was unhandled by user code
Message=Value supplied is of type System.Decimal but expected
AuctionCMS.Framework.Models.Admin.ProductAddModel. Change the value
resolver source type, or redirect the source value supplied to the
value resolver using FromMember.
Types:
public class Currency
{
public Int64 Value { get; set; }
// Spot saved for currency type and any other extra properties
}
public class Product
{
public Currency Price { get; set; }
public Currency ReservePrice { get; set; }
}
public class ProductAddModel
{
public Decimal Price { get; set; }
public Decimal ReservePrice { get; set; }
}
Resolver code:
public class DecimalToCurrencyValueResolver : ValueResolver<decimal, Currency>
{
#region Overrides of ValueResolver<decimal,Currency>
protected override Currency ResolveCore(decimal source)
{
return new Currency() { Value = (Int64)((decimal)source) * 1000 };
}
#endregion
}
public class CurrencyToDecimalValueResolver : ValueResolver<Currency, decimal>
{
#region Overrides of ValueResolver<decimal,Currency>
protected override decimal ResolveCore(Currency source)
{
return (decimal)source.Value * 1000;
}
Mapping code:
Mapper.CreateMap<ProductAddModel, Product>()
.ForMember(x => x.Price, opt => opt.ResolveUsing<DecimalToCurrencyValueResolver>())
.ForMember(x => x.ReservePrice, opt => opt.ResolveUsing<DecimalToCurrencyValueResolver>());
Mapper.CreateMap<Product, ProductAddModel>()
.ForMember(x => x.Price, opt => opt.ResolveUsing<CurrencyToDecimalValueResolver>())
.ForMember(x => x.ReservePrice, opt => opt.ResolveUsing<CurrencyToDecimalValueResolver>());
var model = new ProductAddModel();
var product = new Product();
Mapper.Map<ProductAddModel, Product>(model, product);
What am I doing wrong and is this approach the best way to handle simple transforms during the mapping process?
Thanks!
Use TypeConverters instead:
public class CurrencyToDecimalTypeConverter : ITypeConverter<Currency, Decimal>
{
public decimal Convert(ResolutionContext context)
{
return ((Currency)context.SourceValue).Value * 1000;
}
}
public class DecimalToCurrencyTypeConverter : ITypeConverter<Decimal, Currency>
{
public Currency Convert(ResolutionContext context)
{
return new Currency() { Value = (Int64)((decimal)context.SourceValue) * 1000 };
}
}
Here is configuration:
Mapper.CreateMap<ProductAddModel, Product>();
Mapper.CreateMap<Product, ProductAddModel>();
Mapper.CreateMap<Decimal, Currency>().ConvertUsing<DecimalToCurrencyTypeConverter>();
Mapper.CreateMap<Currency, Decimal>().ConvertUsing<CurrencyToDecimalTypeConverter>();
var model = new ProductAddModel();
var product = new Product();
Mapper.Map<ProductAddModel, Product>(model, product);
In resolver decimal is used and in mapping productAddModel is passed. So mapping should be like this
Mapper.CreateMap<ProductAddModel, Product>()
.ForMember(x => x.Price, opt => opt.ResolveUsing<DecimalToCurrencyValueResolver>().FromMember(e => e.Value ));

Automapper: How to leverage a custom INamingConvention?

I am working with a database where the designers really seemed to enjoy capital letters and the underscore key. Since I have a simple ORM, my data models use these names as well. I need to build DTOs and I would prefer to give them standard names since we are exposing them through services.
The code below is now corrected! The test passes so use this as a reference if you need to use multiple naming conventions
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using AutoMapper;
using NUnit.Framework;
namespace AutomapperTest
{
public class DATAMODEL
{
public Guid ID { get; set; }
public string FIRST_NAME { get; set; }
public List<CHILD_DATAMODEL> CHILDREN { get; set; }
}
public class CHILD_DATAMODEL
{
public Guid ID { get; set; }
public int ORDER_ID { get; set; }
}
public class DataModelDto
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public List<ChildDataModelDto> Children { get; set; }
}
public class ChildDataModelDto
{
public Guid Id { get; set; }
public int OrderId { get; set; }
}
public class UpperUnderscoreNamingConvention : INamingConvention
{
private readonly Regex _splittingExpression = new Regex(#"[\p{Lu}0-9]+(?=_?)");
public Regex SplittingExpression { get { return _splittingExpression; } }
public string SeparatorCharacter { get { return "_"; } }
}
public class Profile1 : Profile
{
protected override void Configure()
{
SourceMemberNamingConvention = new UpperUnderscoreNamingConvention();
DestinationMemberNamingConvention = new PascalCaseNamingConvention();
CreateMap<DATAMODEL, DataModelDto>();
CreateMap<CHILD_DATAMODEL, ChildDataModelDto>();
}
}
[TestFixture]
public class Tests
{
[Test]
public void CanMap()
{
//tell automapper to use my convention
Mapper.Initialize(x => x.AddProfile<Profile1>());
//make a dummy source object
var src = new DATAMODEL();
src.ID = Guid.NewGuid();
src.FIRST_NAME = "foobar";
src.CHILDREN = new List<CHILD_DATAMODEL>
{
new CHILD_DATAMODEL()
{
ID = Guid.NewGuid(),
ORDER_ID = 999
}
};
//map to destination
var dest = Mapper.Map<DATAMODEL, DataModelDto>(src);
Assert.AreEqual(src.ID, dest.Id);
Assert.AreEqual(src.FIRST_NAME, dest.FirstName);
Assert.AreEqual(src.CHILDREN.Count, dest.Children.Count);
Assert.AreEqual(src.CHILDREN[0].ID, dest.Children[0].Id);
Assert.AreEqual(src.CHILDREN[0].ORDER_ID, dest.Children[0].OrderId);
}
}
}
Create your mappings in profiles, and define the INamingConvention parameters as appropriate.
I don't like the global/static, so I prefer using Initialize and define all of my mappings together. This also has the added benefit of allowing a call to AssertConfiguration... which means if I've borked my mapping I'll get the exception at launch instead of whenever my code gets around to using the problematic mapping.
Mapper.Initialize(configuration =>
{
configuration.CreateProfile("Profile1", CreateProfile1);
configuration.CreateProfile("Profile2", CreateProfile2);
});
Mapper.AssertConfigurationIsValid();
in the same class with that initialization method:
public void CreateProfile1(IProfileExpression profile)
{
// this.CreateMap (not Mapper.CreateMap) statements that do the "normal" thing here
// equivalent to Mapper.CreateMap( ... ).WithProfile("Profile1");
}
public void CreateProfile2(IProfileExpression profile)
{
profile.SourceMemberNamingConvention = new PascalCaseNamingConvention();
profile.DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention();
// this.CreateMap (not Mapper.CreateMap) statements that need your special conventions here
// equivalent to Mapper.CreateMap( ... ).WithProfile("Profile2");
}
if you do it this way, and don't define the same mapping in both profiles, I don't think you need anything to "fill in the blank" from the original question, it should already be setup to do the right thing.
What about
public class DATAMODELProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<DATAMODEL, DATAMODEL>();
Mapper.CreateMap<DATAMODEL, SOMETHINGELSE>();
Mapper.CreateMap<DATAMODEL, DataModelDto>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.ID))
.ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.FIRST_NAME))
.ForMember(dest => dest.ChildDataModels, opt => opt.MapFrom(src => src.CHILD_DATAMODELS));
}
}

Automapping a wrapped class

We want to generically map from one database's context.tableType to another
e.g. UatDb.Branch--LiveDb.Branch
The table's are identical so no MapFrom is necessary.
The following generic mapping definition is sufficient
Mapper.CreateMap<TFromContextTableType,TToContextTableType>();
However!!!
We need to wrap the source context.tableType in the following wrapper class:
public class SWrapper<TFrom> where TFrom : class
{
public SWrapper(TFrom model)
{
Model = model;
}
public TFrom Model { get; private set; }
}
Now to perform the mapping we have to map as follows:
Mapper.CreateMap<SWrapper<FromBranchType>, ToBranchType>().ConstructUsing(x => new Live.Branch()))
.ForMember(d => d.BranchID, o => o.MapFrom(x => x.Model.BranchID))
.ForMember(d => d.BranchName, o => o.MapFrom(x => x.Model.BranchName))
.ForMember(d => d.BranchCountry, o => o.MapFrom(x => x.Model.BranchCountry))
This means that we cannot generically map and have to explicitly declare a ForMember for each mapping. I can't figure out any solution using Resolvers or Type Converters.
I thought about perhaps wrapping the target in a SWrapper then resolving the returned Wrapped object to return the internal Product but not sure how to perform this.
All ideas welcome....
Taking from your mapping you could do the following which does not require you to enter all of the property mappings by hand. The helper method in the second test allows for specialized mapping that you may not need.
[TestMethod]
public void Given_a_wrapper_class_I_should_be_able_to_unwrap_it_and_continue_with_mappings()
{
Mapper.CreateMap<Wrapper<Source>, Destination>()
.UnwrapUsing<Wrapper<Source>, Source, Destination>(w => w.Model)
.ForMember(d => d.Bar, o => o.MapFrom(d => d.Baz));
var source = new Source { Baz = 1, Foo = "One" };
var wrapped = new Wrapper<Source> { Model = source };
var destination = Mapper.Map<Destination>(wrapped);
Assert.IsNotNull(destination);
Assert.AreEqual(1, destination.Bar);
Assert.AreEqual("One", destination.Foo);
}
public static class AutoMapperExtensions
{
public static IMappingExpression<TSource, TDest> UnwrapUsing<TWrapper, TSource, TDest>(this IMappingExpression<TWrapper, TDest> expr, Func<Wrapper<TSource>, TSource> unwrapper)
{
Mapper.CreateMap<Wrapper<TSource>, TDest>()
.ConstructUsing(x => Mapper.Map<TSource, TDest>(unwrapper(x)));
return Mapper.CreateMap<TSource, TDest>();
}
}
public class Source
{
public string Foo { get; set; }
public int Baz { get; set; }
}
public class Destination
{
public string Foo { get; set; }
public int Bar { get; set; }
}
public class Wrapper<T>
{
public T Model { get; set; }
}
How about adding a little utility method to your wrapper class:
public TDest MapTo<TDest>()
{
return Mapper.Map<TFrom, TDest>(this.Model);
}

Resources