You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When attempting to verify the supported mappings via ShouldSupportMappingFromSourceToDestination an error will occur if any of your mappers use a ValueResolver which doesn't include an empty constructor.
An example of when this occurred for me was when attempting to map a request object containing an IEnumerable to a SQL parameters object containing a DataTable to facilitate passing the values via Dapper as TVP. I have service which handles the conversion which is injected into a ValueResolver, since services cannot be injected into the Profile class.
public class DataTableResolver<TSource, TDestination> : IValueResolver<TSource, TDestination, DataTable>
{
private readonly IDataTableUtility _dataTableUtility;
public DataTableResolver(IDataTableUtility dataTableUtility)
{
_dataTableUtility = dataTableUtility;
}
public DataTable Resolve(TSource source, TDestination destination, DataTable destMember, ResolutionContext context)
{
// ... do stuff with IDataTableUtility to return a DataTable
}
}
Because the mapper is created directly from the configuration the dependency injection cannot be resolved.
public MappingTests()
{
_configuration = new MapperConfiguration(config => config.AddProfile<MappingProfile>());
_mapper = _configuration.CreateMapper();
}
In order to resolve this in my own project I modified the test constructor to use an IServiceProvider to instantiate the mapper, which allowed me to include the relevant dependencies:
public MappingTests()
{
_configuration = new MapperConfiguration(cfg => cfg.AddProfile<MappingProfile>());
var services = new ServiceCollection()
.AddAutoMapper(typeof(MappingProfile).Assembly)
.RegisterDataTableUtilities()
.BuildServiceProvider();
_mapper = services.GetService<IMapper>();
}
Hopefully this will help someone who comes across a similar issue, or perhaps this might become a part of the template.
This discussion was converted from issue #611 on May 20, 2022 07:18.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
When attempting to verify the supported mappings via ShouldSupportMappingFromSourceToDestination an error will occur if any of your mappers use a ValueResolver which doesn't include an empty constructor.
An example of when this occurred for me was when attempting to map a request object containing an IEnumerable to a SQL parameters object containing a DataTable to facilitate passing the values via Dapper as TVP. I have service which handles the conversion which is injected into a ValueResolver, since services cannot be injected into the Profile class.
Because the mapper is created directly from the configuration the dependency injection cannot be resolved.
In order to resolve this in my own project I modified the test constructor to use an IServiceProvider to instantiate the mapper, which allowed me to include the relevant dependencies:
Hopefully this will help someone who comes across a similar issue, or perhaps this might become a part of the template.
Beta Was this translation helpful? Give feedback.
All reactions