0

I'm using ASP.net Core with AutoMapper. To get the DI running, I use the AutoMapper.Extensions.Microsoft.DependencyInjection Nuget-Package and let AutoMapper register the profiles via

    private static void InitializeAutoMapper(IServiceCollection services)
    {
        services.AddAutoMapper();
    }

This works fine, but for some Profiles, I'd like to inject also some dependencies to them, for example:

public class IndividualDtoProfile : Profile
{
    private readonly IIndividualFactory _individualFactory;
    private readonly IMapper _mapper;

    public IndividualDtoProfile(IIndividualFactory individualFactory, IMapper mapper)
    {
        _individualFactory = individualFactory;
        _mapper = mapper;
    }

    public IndividualDtoProfile()
    {
        CreateMap<Individual, IndividualDto>();

        CreateMap<IndividualDto, Individual>()
            .ConstructUsing(
                dto =>
                {
                    var gender = _mapper.Map<IndividualGender>(dto.Gender);
                    return _individualFactory.CreateIndividual(dto.FirstName, dto.LastName, gender, dto.BirthDate);
                });
    }
}

The only relevant discussion I found is here : https://groups.google.com/forum/#!topic/automapper-users/5XK7pqGu_Tg

Which pretty much seems to suggest to not use the goodness of the existing possibilities, but manually map the Profiles. The only other possibility I'd see is to provide a static ServiceProvider-Singleton, which also doesn't seem too appealing.

Is there already a possibility, to use ASP.Net Core with AutoMapper and let dependencies inject into Profiles?

Edit: Due to the comment, probably I'm also something fundamentally wrong: I'm learning Domain Driven Design, where I have an application layer. I'd like to map the DTOs, which are used from a Web-Service, back to the domain entities, and I assumed, it would make sense to use the Factories there too, since otherwise I would bypass the logic in the Factories.

6
  • Does it really make sense to add DI here? Finally, it's just mapping of one object to another. If you have tests, you would need to test/mock mapping too. Double work from my point of view. Commented Aug 11, 2017 at 10:11
  • hm? I don't understand your point: I'd like to generically map with AutoMapper, but for some maps I'd like to use Factories in order to assure some invariants and assuring, that every object is created for its specific Factory. Commented Aug 11, 2017 at 10:43
  • Why the hell would you want to use/inject IMapper inside a profile??! The profiles are used to add registrations, before the mapper is ready to be used (i.e. to perform a validation via Mapper.AssertConfigurationIsValid()). While I generally prefer to inject IMapper anywhere too, there are some limitations though. Are you using Automapper's EF projections? They don't work easily with injections, since the .ProjectTo() method uses the static Mapper class instead Commented Aug 11, 2017 at 10:49
  • Also I assume your Individual is an EF/EF Core Poco, in which case: never map from Dto to Persistence or Domain Models, it will bite you really hard (EF Core works with references, mapping this way may break the way how EF Core handles the entities, especially on collections) Commented Aug 11, 2017 at 11:27
  • @MatthiasMüller I prefer to use Mapping after all magic stuff (validations, getting data from DB etc). It should be as simple as possible. (IMO) Commented Aug 11, 2017 at 11:39

1 Answer 1

1

That's not supported out of the box, by design. If you want it, you have to do it yourself using your DI container. This has been discussed many times before. For example, here. The docs.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, searching for another solutuon then.

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.