All of this is on .NET 6, AutoMapper v12, SimpleInjector v5.4.1 Upon trying to map to IModel (interface of Model), code throws the following stack trace:
AutoMapper.AutoMapperMappingException: Error mapping types.
Mapping types:
EntityCollection`1 -> IEnumerable`1
X.DAL.HelperClasses.EntityCollection`1[[X.DAL.EntityClasses.WFCurrencyEntity, X.DAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.IEnumerable`1[[X.Model.Common.ICurrency, X.Model.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
---> AutoMapper.AutoMapperMappingException: Cannot create interface X.Model.Common.ICurrency
Mapping types:
WFCurrencyEntity -> ICurrency
X.DAL.EntityClasses.WFCurrencyEntity -> X.Model.Common.ICurrency
Type Map configuration:
WFCurrencyEntity -> ICurrency
X.DAL.EntityClasses.WFCurrencyEntity -> X.Model.Common.ICurrency
at lambda_method30(Closure , Object , IEnumerable`1 , ResolutionContext )
--- End of inner exception stack trace ---
at lambda_method30(Closure , Object , IEnumerable`1 , ResolutionContext )
at X.Repository.RepositoryBase`3.FindAsync(IFilter filter)`
I have confirmed my binding for the model is correct:
container.Register<ICurrency, Currency>(); by injecting container into the repository and attempting to call container.GetInstance(typeof(ICurrency))
I am also mapping my model to my LLBLGen entity:
CreateMap<ICurrency, WFCurrencyEntity>(MemberList.Source).ReverseMap();
I have confirmed this hits by debugging CreateMap, and also looking at configurations on IMapper before the mapping error itself that the mapping configuration exists.
By all means this just feels like AutoMapper is not using the SimpleInjector container GetInstance activator...
This is my code for creating the Mapper on Startup.cs
var config = new MapperConfiguration(config =>
{
config.ConstructServicesUsing(t => container.GetInstance(t));
config.AllowNullCollections = true;
config.AllowNullDestinationValues = true;
config.AddProfile(new Repository.RepositoryProfile());
});
container.Register(config.CreateMapper, Lifestyle.Singleton);
Thanks to @LucianBargaoanu, this problem is solved.
I was missing this line of code in my AutoMapper configuration setup. This is something even the SimpleInjector and AutoMapper docs overlooked. And it really doesn't help that the ForAllMaps call is now hidden in internal API.