1

I need to use the DB context to map a nested object based on the Name property which is a string. I'm trying to inject DB context into the Mapping Config but I get the following error:

System.MissingMethodException: Cannot dynamically create an instance of type 'ChatBotCore.Application.Bots.Common.MappingConfig'. Reason: No parameterless constructor defined.less constructor defined

    public class MappingConfig : IRegister
    {
        private readonly IApplicationDbContext _context;

        public MappingConfig(IApplicationDbContext context)
        {
            _context = context;
        }

        public void Register(TypeAdapterConfig config)
        {
            config.NewConfig<Bot, GetBotsResult>()
                .Map(dest => dest.Channel, src => src.Channel.Name)
                .Map(dest => dest.NluServiceProvider, src => src.NluServiceProvider.Name);

             config.NewConfig<CreateBotCommand, Bot>()
                 .Map(dest => dest.Channel, src => GetChannelByName(src.Channel))
                 .Map(dest => dest.NluServiceProvider, src => GetNluServiceProviderByName(src.NluServiceProvider));

              config.NewConfig<UpdateBotCommand, Bot>()
                 .Map(dest => dest.Channel, src => GetChannelByName(src.Channel))
                 .Map(dest => dest.NluServiceProvider, src => GetNluServiceProviderByName(src.NluServiceProvider));
        }

         private async Task<NluServiceProvider> GetNluServiceProviderByName(string nluServiceProvider)
         {
             return await _context.NluServiceProviders.FirstAsync(x => x.Name.ToLower() == nluServiceProvider.ToLower());
         }

         private async Task<Channel> GetChannelByName(string channel)
         {
             return await _context.Channels.FirstAsync(x => x.Name.ToLower() == channel.ToLower());
         }
    }
Guillermo Verón
  • 218
  • 1
  • 11

1 Answers1

0

`

public interface IMappingConfig: IRegister { /* interface for finding */ }
public class MappingConfig : IMappingConfig // use known interface here
{
    /* your code */
}

private static void RegisterMappers(IServiceCollection services)
{
    services.AddSingleton(TypeAdapterConfig.GlobalSettings);
    services.AddScoped<IMapper, ServiceMapper>();

    // inserting needed dependencies
    services.AddSingleton<IApplicationDbContext, ApplicationDbContext>(); 
    services.AddScoped<IMappingConfig, MappingConfig>();

    // now the work around, create a (temporary) service provider 
    // just for retrieving your mapping having all di done automatically
    var provider = services.BuildServiceProvider();

    TypeAdapterConfig.GlobalSettings.Apply(
        provider.GetService<IMappingConfig>() // this now works with di
    );
}

`

  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Apr 22 '23 at 00:20