I have a scenario where data is mapped using Automapper into some target object. The logic, that runs this mapping should be tested with unit tests.
Since the target object are quite complex and require also external systems to be operational, we decided to use mocks instead of the production targets. The goal of the unit tests was to test the logic, not the target objects (tested separately).
The maps were set up like this:
CreateMap<ISource, ITargetInterface>)
.ForMember(....);
The mapping was done like this:
var targetObject = mapper.Map<ITargetInterface>(sourceObject);
while in production code
class ProductionTarget : ITargetInterface
and in unit tests
class MockedTarget : ITargetInterface
For some magic reasons that worked with AutoMapper 7.0.1.
Now, that we are updating the software from net462 to net8 we also updated Automapper to the version 13.0.1 and get the following Exception:
AutoMapperMappingException: Cannot create interface
which is kind of obvious, since Automapper does not "know" which interface-implementation should be created.
But how could that ever work with v7? I assume somwhere in between Automapper v7 and v13 this "magic" feature was removed, fixed, whatever...
Is there any way to achieve this behaviour (e.g. by some test-depending configuration/mapping)? Unfortunatey i was not able to find a clue on how to do this.
I have a scenario where data is mapped using Automapper into some target object. The logic, that runs this mapping should be tested with unit tests.
Since the target object are quite complex and require also external systems to be operational, we decided to use mocks instead of the production targets. The goal of the unit tests was to test the logic, not the target objects (tested separately).
The maps were set up like this:
CreateMap<ISource, ITargetInterface>)
.ForMember(....);
The mapping was done like this:
var targetObject = mapper.Map<ITargetInterface>(sourceObject);
while in production code
class ProductionTarget : ITargetInterface
and in unit tests
class MockedTarget : ITargetInterface
For some magic reasons that worked with AutoMapper 7.0.1.
Now, that we are updating the software from net462 to net8 we also updated Automapper to the version 13.0.1 and get the following Exception:
AutoMapperMappingException: Cannot create interface
which is kind of obvious, since Automapper does not "know" which interface-implementation should be created.
But how could that ever work with v7? I assume somwhere in between Automapper v7 and v13 this "magic" feature was removed, fixed, whatever...
Is there any way to achieve this behaviour (e.g. by some test-depending configuration/mapping)? Unfortunatey i was not able to find a clue on how to do this.
Share Improve this question asked Mar 14 at 7:45 jholzerjholzer 91 silver badge1 bronze badge 1- How is your existing code doing this? A repro with v7 would help. Make a gist that we can execute. – Lucian Bargaoanu Commented Mar 14 at 8:51
1 Answer
Reset to default 0Prior to v11 of AutoMapper, creating a map to an interface would use proxy classes at runtime by default.
As you can see from the documentation, this behaviour changed in v11, and you now need to opt in to this behaviour by adding AsProxy()
to the map:
https://docs.automapper./en/latest/11.0-Upgrade-Guide.html#generating-interface-proxies-is-disabled-by-default
So for your example:
CreateMap<ISource, ITargetInterface>()
.AsProxy()
.ForMember(....);
But this will cause AutoMapper to create a dynamic proxy class & instance at runtime, and won't use ProductionTarget
, or MockedTarget
for that matter.
There are a number of ways to use those specific types instead, but you will probably want to make use of Mapping Inheritance. For example:
CreateMap<ISource, ITargetInterface>()
.ForMember(....);
CreateMap<ISource, ProductionTarget>()
.IncludeBase<ISource, ITargetInterface>();
CreateMap<ISource, MockedTarget>()
.IncludeBase<ISource, ITargetInterface>();