Skip to content

You have multiple concrete types that implement the same interface

Isaac Abraham edited this page Jan 26, 2016 · 12 revisions

###Scenario Unity will simply overwrite an existing inteface-concrete registration with the latest one it gets (kind of a "last past the post" behaviour). To prevent accidentally overwriting mappings, if the Automapper finds two concrete mappings for the same interface without a named registration supplied, it throws an exception.

###Example

public interface IMyService { }
public class MyService : IMyService { }
public class MyOtherService : IMyService { } // also maps to 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.
  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.