admin管理员组文章数量:1391929
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>();
本文标签: cAutomapper with interface as targetStack Overflow
版权声明:本文标题:c# - Automapper with interface as target - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744668811a2618697.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论