Incorrect mapping in case of multiple interfaces
See original GitHub issueAutomapper version 4.2.1 In case when source implements more than one interface and we do not have mapping configuration for the source but have it for interfaces it will be impossible to use mapping configured for second (third, fourth…) interface
public interface IChildA
{
string PropertyA { get; set; }
}
public interface IChildB
{
string PropertyB { get; set; }
}
public class Source : IChildA, IChildB
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
}
public class Destination
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
}
...
public void MappingTest()
{
var conf = new MapperConfiguration(
config =>
{
config.CreateMap<IChildA, Destination>();
config.CreateMap<IChildB, Destination>();
});
var mapper = conf.CreateMapper();
var src = new Source { PropertyA = "A", PropertyB = "B" };
var dest = new Destination();
mapper.Map<IChildA, Destination>(src, dest); // will map PropertyA
mapper.Map<IChildB, Destination>(src, dest); // will NOT map PropertyB
Trace.WriteLine(dest.PropertyA == src.PropertyA); // true
Trace.WriteLine(dest.PropertyB == src.PropertyB); // false !!!
}
The code above will not fill PropertyB (It will in case we will change order of interfaces in the Source class declaration)
The reason is MapperConfiguration.ResolveTypeMap method:
public TypeMap ResolveTypeMap(object source, object destination, Type sourceType, Type destinationType)
{
return ResolveTypeMap(source?.GetType() ?? sourceType, destination?.GetType() ?? destinationType);
}
It will call source.GetType() and then will look for the first known mapping (it will be IChildA in our case), so “sourceType” (IChildB in our case) will be ignored and we will got invalid mapping.
Issue Analytics
- State:
- Created 7 years ago
- Comments:26 (13 by maintainers)
Top Results From Across the Web
Implementing Incompatible Interfaces - java
I am trying to build a class that implements Queue and Map . Both interfaces define the remove(Object) method, but with different return...
Read more >Avoiding ARP Flux in Multi-Interface Linux Hosts
However, in specific cases, ARP Flux generates unexpected behavior of applications due to incorrect mapping between IPv4 addresses and MAC ...
Read more >Hints and tips for the interface map editor
The following example shows an interface map with a validation problem. In this case, the map representation in the Business Integration view will...
Read more >Best Practices for Using Multiple Network Interfaces (NICs) ...
If multiple default gateways exist, then packets may be routed to the wrong outside network, causing them to be undeliverable. In most cases, ......
Read more >RFC 6418 - Multiple Interfaces and Provisioning Domains ...
Multiple Interfaces and Provisioning Domains Problem Statement (RFC 6418, ... In most cases, the virtual interface will map several physical network ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just a heads up that I’m still on this thing, just havn’t found any time to actually write the code
A lot of stuff went on here while I was sleeping 😃
@oddbear you’re right about the order of interfaces, as I said, it was just a quick thought. I like your approach as it eliminates the dependency on ValueTuple.
I have some thoughts on this issue, or at least some ways I’d like to see it implemented. I think I might be able to conceive most of it through some extension methods, but I’d really have to bunker down and hammer out the code to be sure. Without changing the core it might have some uglyish parts.
I was thinking of having something like a MapAs method taking one or more type parameters to use explicitly. Having a separate method should enhance readability. @jbogard, what do you mean with a global config? That doesn’t sound very useful, as in some case one might still want to use the default behaviour.
Anyways, more on this when I got down with some code. I don’t think I’ll have much time for it today, but maybe I could put together a little POC by tomorrow or thursday.