Suppose that you have the following generic interface:
And you use Automapper in implementation:
I.e. in your code you will have bunch of mapping interfaces like IMapper<Foo1, Bar1>, IMapper<Foo2, Bar2>, …, which will be implemented by Mapper<Foo1, Bar1>, Mapper<Foo2, Bar2>, …, i.e. by the same class with the same generic parameters. How to configure mapping for these interfaces in StructureMap? First way is to write all possible mappings in the custom registry:
But with this approach you will need to add new mapping for each new pair of classes which you will add in the future. Is there an easier way to define mapping for open generic interface IMapper<T,U> to open generic class Mapper<T,U>? If you would have own class for each mapping (like Foo1Bar1Mapper, Foo2Bar2Mapper, …), then you would be able to use ConnectImplementationsToTypesClosing method, but not in this example, because we have the same class for all mappings. However, it is also possible for our case:
Mapping is defined in line 9. With this configuration Mapper<,> class will be used for all new mappings which you will use in the project.