title: Handling Dependency in Automapper Profile
body: <p>I have the following product mapping which will call a dependency (UpdateProduct in ProductMapService) to perform mapping to product child entities after initial mapping. However, when i execute the mapping process, i receive an exception <strong>(No parameterless constructor defined for this object.)</strong></p>
  
  <p>I'm not sure this is the best way to inject dependency in profile class. Can anyone advise me on this? </p>
  
  <p>Automapper Product Profile :</p>
  
  <pre><code>namespace MyAPI.Mappers
  {
  public class ProductProfiles: Profile
  {
      private readonly IProductMapService _productMap;
      public ProductProfiles(IProductMapService productMap)
      {
          _productMap = productMap;
  
          CreateMap&lt;ProductForCreationDto, Product&gt;();
  
          CreateMap&lt;ProductForUpdateDto, Product&gt;()
              .ForMember(p =&gt; p.VariOptions, opt =&gt; opt.Ignore())
              .AfterMap((pDto, p) =&gt; _productMap.UpdateProduct(pDto, p));
      }
  }
  }
  </code></pre>
  
  <p>ProductService.cs : </p>
  
  <pre><code>namespace MyAPI.Services
  {
  public class ProductMapService: IProductMapService
  {
      private readonly IMapper _mapper;
      public ProductMapService(IMapper mapper)
      {
          _mapper = mapper;
  
      }
      public void UpdateProduct(ProductForUpdateDto productForUpdateDto, Product productFromRepo)
      {
  
           foreach (var variOptionDto in productForUpdateDto.VariOptions)
          {
              if(variOptionDto.Id == 0)
              {
                  productFromRepo.VariOptions.Add(Mapper.Map&lt;VariOption&gt;(variOptionDto));
              }
              else
              {
                  _mapper.Map(variOptionDto, productFromRepo.VariOptions.SingleOrDefault(vo =&gt; vo.Id == variOptionDto.Id));
              }
  
              foreach (var variOptionTwoDto in variOptionDto.VariOptionTwos)
              {
                  if(variOptionTwoDto.Id == 0)
                  {
                      productFromRepo.VariOptions.FirstOrDefault(vo =&gt; vo.Id == variOptionDto.Id).VariOptionTwos.Add(Mapper.Map&lt;VariOptionTwo&gt;(variOptionTwoDto));
                  }
                  else
                  {
                      _mapper.Map(variOptionTwoDto, productFromRepo.VariOptions.FirstOrDefault(vo =&gt; vo.Id == variOptionDto.Id).VariOptionTwos.SingleOrDefault(vot =&gt; vot.Id == variOptionTwoDto.Id));
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and in Startup.cs :</p>
  
  <pre><code>services.AddAutoMapper();
  services.AddSingleton&lt;IProductMapService, ProductMapService&gt;();
  </code></pre>