AutoMapper / AutoMapper.Extensions.Microsoft.DependencyInjection

MIT License
258 stars 79 forks source link

ProjectTo, ValueConverter and DependencyInjection #170

Closed Misiu closed 1 year ago

Misiu commented 1 year ago

I have a project in .NET7. I'm adding AutoMapper using services.AddAutoMapper(typeof(Bootstrap)); Everything works fine, I'm able to use ProjectTo with Entity Framework.

I have a very complex mapper (at least for me) for two objects that involve XML deserialization and JSON deserialization.

internal sealed class ViewContextProfile : Profile
{
    public ViewContextProfile()
    {
        CreateMap<View, ViewDto>()
            .ForMember(x => x.Definition, x => x.MapFrom(c => ParseXml(c)))
            .ForMember(x => x.Layout, x => x.MapFrom(c => ParseJson(c)));
    }

    private static ViewLayoutDto? ParseJson(View view)
    {
        if (string.IsNullOrWhiteSpace(view.Layout))
        {
            return null;
        }

        try
        {
            var options = new JsonSerializerOptions
            {
                AllowTrailingCommas = true
            };
            var layoutDto = JsonSerializer.Deserialize<ViewLayoutDto>(view.Layout, options);
            return layoutDto;
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);
            Debug.WriteLine($"Error parsing view layout for view {view.Name}");
        }

        return null;
    }

For now, I'm using Debug.WriteLine to write errors about bad JSON and deserialization errors (I'm loading that data from an external system and it might be corrupted, I have no control over it), but I'd like to use a proper logger.

ProjectTo currently can't work with DI (https://github.com/AutoMapper/AutoMapper.Extensions.Microsoft.DependencyInjection/issues/113#issuecomment-524421715) and according to docs (https://docs.automapper.org/en/stable/Queryable-Extensions.html#supported-mapping-options) I can't create a Type or a Value converter.

So I'm asking to add support for DI in Profiles, this way I would be able to inject Logger and pass it the needed functions or allow using Value and/or Type converters when using ProjectTo.

Not sure if this is the correct place for this question, if not please transfer this issue.

lbargaoanu commented 1 year ago

I think this is better suited for StackOverflow.

Misiu commented 1 year ago

@lbargaoanu thank you for a quick reply. I searched StackOverflow before I asked the question (feature request) here. Most answers are similar to this one https://stackoverflow.com/a/73882676/965722 (btw it's your answer).

My use case is very simple: I need to log errors when doing deserialization inside mapExpression. I can't use DI (because it is not allowed in Profiles) and I can't use custom Value and Type Converters with ProjectTo.

jbogard commented 1 year ago

DI in Profiles is explicitly discouraged and not supported. The best you can do is use static service location. At least then you'll know exactly what instance of your logger is getting used.

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.