2

I'm using:

  • .net core v2.2.0
  • Microsoft.EntityFrameworkCore v2.2.4
  • automapper v8.1.1
  • AutoMapper.Extensions.Microsoft.DependencyInjection v6.1.1
  • VS2017 Community Edition v15.9.12

In my solution i have:

  • TVC_DATA: handles all data access and contains the mapping profile classes
  • TVC_PORTAL: react web app

I have setup automapper using dependency injection following the guide as described here

So: in startup.cs of TVC_PORTAL in ConfigureServices method i have:

services.AddAutoMapper(typeof(AutomapProfileGen));

where i use AutoMapProfileGen is one of the marker types for AddAutoMapper to locate the TVC_DATA assembly where the other profiles are located as well.

In my ObjectController i inject IMapper:

public ObjectController(IHostingEnvironment environment, IMapper mapper)
{
        _hostingEnvironment = environment;            
        _mapper = mapper;
}

And i use the mapper later on:

IEnumerable<ObjectViewType> vList = _mapper.Map<IEnumerable<ObjectType>, IEnumerable<ObjectViewType>>(mList);

My profiles are pretty straightforward, for example:

public class AutomapProfileSrc : Profile
{
    public AutomapProfileSrc()
    {
        //source data
        CreateMap<AirlineView, Airline>().ReverseMap();
        CreateMap<AirlineListView, Airline>().ReverseMap();
        CreateMap<AirportView, Airport>().ReverseMap();
        CreateMap<AirportListView, Airport>().ReverseMap();
        CreateMap<CountryView, Country>().ReverseMap();
        CreateMap<CountryListView, Country>().ReverseMap();
    }
}

My question: i want to set some global config options for automapper and cannot figure out where/how to set them. For example: i want to set ValidateInlineMaps to false (because that was mentioned as a solution when AssertConfigurationIsValid throws 'member not mapped' exceptions). Also i want to set MaxDepth to 1 for all maps to avoid circular references. What i tried:

1) Set ValidateInlineMaps to false in all profile constructors: doesn't work.

public class AutomapProfileCfg : Profile
{
    public AutomapProfileCfg()
    {
        ValidateInlineMaps = false;
...

2) Create a MapperConfiguration object in ConfigureServices like:

'var config = new MapperConfiguration(cfg =>
    {
        cfg.ForAllMaps((typeMap, mappingExpression) => {  mappingExpression.MaxDepth(1); });
        cfg.AllowNullCollections = true;
        cfg.ValidateInlineMaps = false;
        //cfg.Advanced.MaxExecutionPlanDepth = 1;
    });'

But i do not know how to link it to the mapper instance: just creating is not changing the mapper's behaviour.

Have been going through documentation and searching this site for almost a day now: is getting frustrating because this looks like it must be simple....but somehow i can't get it to work. Any help would be much appreciated

1 Answer 1

4

Any global config actions can be specified directly when configuring the service:

services.AddAutoMapper(cfg =>
    {
        cfg.ValidateInlineMaps = true;
        ...other config stuff

    }, typeof(AutomapProfileGen)); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.