-1

I have this code example done in LinqPad 5 (.Net Framework):

void Main()
{
    var o = new A() { b = new B() { Msj = "Hello, world!" } };
    var r = MapObject<C, A>(o);
    r.Dump();
}

public class A
{
    public B b {get;set;}
}

public class B
{
    public string Msj {get;set;}
}

public class C
{
    public D b { get; set; }
}

public class D
{
    public string Msj { get; set; }
}


private static Tdst MapObject<Tdst, Tori>(Tori obj)
{
    var configMapper = new AutoMapper.MapperConfiguration(cfg => cfg.CreateMap<Tori, Tdst>());
    var mapper = configMapper.CreateMapper();
    return mapper.Map<Tdst>(obj);
}

This code work well in AutoMapper version 7, but I have upgraded AutoMapper from version 7 to version 10. Now this code throws an error:

AutoMapperMappingException: Error mapping types.

Mapping types:
A -> C
UserQuery+A -> UserQuery+C

Type Map configuration:
A -> C
UserQuery+A -> UserQuery+C

Destination Member:
b

I know that classes A and C are exactly the same, but they were created from different web services so they are in different namespaces.

How could I change the method MapObject to make it work in the same way that version 7 of AutoMapper?

Edit 1:

I think I solved it using Mapster instead of AutoMapper, but I want let the question open just for curiosity.

rasputino
  • 691
  • 1
  • 8
  • 24
  • 1
    Obviously, you didn't config B<=>D mapping – Selvin Nov 14 '22 at 09:17
  • @Selvin I know, but I don't wanna do this job for all the members of the A class (that are many in the real project) and this method worked well in version 7 (it mapped all members of A class), I just wanna know if there is any way to make version 10 do the job as version 7 did. – rasputino Nov 14 '22 at 09:23
  • 1
    It is written in "migration to 9.0 version" in official docs: *You will need to explicitly configure maps, manually or using reflection* – Selvin Nov 14 '22 at 09:24
  • Thanks @Selvin, so your answer is that there is no such equivalent method MapObject in version 10 that can do the work, isn't it? – rasputino Nov 14 '22 at 09:28

1 Answers1

0

If you know what classes are "identical", it's easy to write some code that creates the needed maps. AM no longer does that for you because it cannot really know when such a default map is good enough.

Lucian Bargaoanu
  • 3,336
  • 3
  • 14
  • 19
  • I understand, but from my point of view I think is a lack of features for cases like this one. Upgrading a library shouldn't be a loss of functionality. I think it should be solved with an alternative way to do the same job. I finally solved it with Mapster. – rasputino Nov 14 '22 at 11:28
  • 1
    That "feature" brought many problems for as long as it existed, years, that is. While it's trivial to create those maps in user code, it's impossible to do it right in library code. Just because it's possible, it doesn't mean it's a good idea :) – Lucian Bargaoanu Nov 14 '22 at 12:01