Skip to content

You have multiple concrete types that implement the same interface

isaacabraham edited this page Dec 21, 2012 · 12 revisions

###Scenario With the default behaviour of the Automapper, it expects to find one and only one implementation of a given interface. If you have several concrete types that map to an interface, it will throw a DuplicateMappingException. ###Example

         public interface IMyService { }
         public class MyService : IMyService { }
         public class MyOtherService : IMyService { }

###How do I fix this? You have several options: -

  1. If you intend to only use one of these implementations at runtime, mark the other one with the [DoNotMap] attribute (or fluent equivalent).
  2. If you want to access all implementations at runtime, either: -
    • Mark the interface as a [Multimap] (or fluent equivalent)
    • Use the MultimapByDefault mapping behavior when calling the Automapper. See here for more details on Multimaps.
  3. If you are passing an explicit set of types through to the Automapper, you could simply miss out the one that you do not want to map.