1

I'm trying to upgrade some code that I didn't write from AutoMapper 4.0.4 to 7.0.1 and I'm running into an issue. There is a TypeConverter that looks like this:

public class BaseListTypeConverter<TCol1, TCol2, T1, T2> : ITypeConverter<TCol1, TCol2>
    where TCol1 : ICollection<T1>
    where TCol2 : ICollection<T2>
    where T1 : class
    where T2 : class
{
    public TCol2 Convert(ResolutionContext context)
    {
        var sourceList = (TCol1)context.SourceValue;
        TCol2 destinationList = default(TCol2);

        if (context.PropertyMap == null
            || context.Parent == null
            || context.Parent.DestinationValue == null)
            destinationList = (TCol2)context.DestinationValue;
        else
            destinationList = (TCol2)context.PropertyMap.DestinationProperty.GetValue(context.Parent.DestinationValue);
...

But the ITypeConverter and ResolutionContext interfaces have now changed. The ResolutionContext no longer has the SourceValue, DestinationValue, PropertyMap, or Parent properties. I thought that since the new signature of the Covert method has parameters for the source and destination objects that I could just omit the first if statement as in:

public class BaseListTypeConverter<TCol1, TCol2, T1, T2> : ITypeConverter<TCol1, TCol2>
    where TCol1 : ICollection<T1>
    where TCol2 : ICollection<T2>
    where T1 : class
    where T2 : class
{
    public TCol2 Convert(TCol1 sourceList, TCol2 destinationList, ResolutionContext context)
    {
...

But the parameter destinationList is coming in as null so apparently I still need whatever logic that if statement is doing, but how do I rewrite it for AutoMapper 7?

adam0101
  • 29,096
  • 21
  • 96
  • 174
  • If you don't pass a destination, you get null in the destination. – Lucian Bargaoanu Oct 24 '18 at 15:39
  • @LucianBargaoanu, I'm not sure I follow you. I'm not the one passing in the destination, AutoMapper is. This code was ran in a unit test that passed with the previous version of AutoMapper and now fails because `destinationList` is null. My guess is because `destinationList` was being set by this line: `destinationList = (TCol2)context.PropertyMap.DestinationProperty.GetValue(context.Parent.DestinationValue);` and now it's not. I need to know how to write this line using the new version of AutoMapper. – adam0101 Oct 24 '18 at 16:22
  • If when you call Map, you pass a destination, you'll have one in your type converter, otherwise it's null. – Lucian Bargaoanu Oct 24 '18 at 16:49
  • @LucianBargaoanu, the call itself is being passed instances of a source object and destination object. They have properties with lists of objects and those objects also have properties with lists of objects. So the null destination being passed into the convert method is buried somewhere in a huge object graph. Despite it being null, the author of this code was able to set `destinationList` using the previous version of AutoMapper and the line of code I mention in my previous comment. In order to update this code, I need to know what that line does and how to do the same in AutoMapper 7. – adam0101 Oct 24 '18 at 18:26
  • You can install 4.0.4 and find out. But I find that futile. I would start over fresh and try to solve the problem. It's not about AM. If you need smth in a child mapping, you have to pass that yourself using Context.Items. See [this](http://docs.automapper.org/en/latest/5.0-Upgrade-Guide.html#resolution-context-things). – Lucian Bargaoanu Oct 24 '18 at 18:31
  • @LucianBargaoanu, we have tons of code built on top this. It's not an understatement to say that it would cost ~$1million to rewrite it the "right way". I'm left with either downgrading AutoMapper and losing the month of work I did upgrading our code to not use the static methods, or figuring out how to make it work as it did before. You mention Context.Items, what would that implementation look like? Can I use it to pass the parent context and PropertyMap down into the TypeConverter? If so, how? – adam0101 Oct 24 '18 at 20:29
  • A lot of people did it before you, so maybe it's not that difficult. AM doesn't keep that state for you anymore. – Lucian Bargaoanu Oct 25 '18 at 05:11

0 Answers0