Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider supporting Automapper dependency injection #5288

Closed
realLiangshiwei opened this issue Sep 4, 2020 · 4 comments · Fixed by #6868
Closed

Consider supporting Automapper dependency injection #5288

realLiangshiwei opened this issue Sep 4, 2020 · 4 comments · Fixed by #6868

Comments

@realLiangshiwei
Copy link
Member

realLiangshiwei commented Sep 4, 2020

Currently, we create Automapper when the application is initialized, But this way we cannot use Automapper's dependency injection function, like: https://docs.automapper.org/en/stable/Before-and-after-map-actions.html#asp-net-core-and-automapper-extensions-microsoft-dependencyinjection

.

We can consider creating Automapper in advance to get dependency injection. example:

public override void PostConfigureServices(ServiceConfigurationContext context)
{
    var mapperAccessor = new MapperAccessor();

    context.Services.AddSingleton<MapperAccessor>(_ => mapperAccessor);
    context.Services.Add(new ServiceDescriptor(typeof(IMapperAccessor), CreateMappings, ServiceLifetime.Singleton));
}

private IMapperAccessor CreateMappings(IServiceProvider serviceProvider)
{
    var options = serviceProvider.GetRequiredService<IOptions<AbpAutoMapperOptions>>().Value;

    void ConfigureAll(IAbpAutoMapperConfigurationContext ctx)
    {
        foreach (var configurator in options.Configurators)
        {
            configurator(ctx);
        }
    }

    void ValidateAll(IConfigurationProvider config)
    {
        foreach (var profileType in options.ValidatingProfiles)
        {
            config.AssertConfigurationIsValid(((Profile) Activator.CreateInstance(profileType)).ProfileName);
        }
    }

    using (var scope = serviceProvider.CreateScope())
    {
        var mapperConfiguration = new MapperConfiguration(mapperConfigurationExpression =>
        {
            ConfigureAll(new AbpAutoMapperConfigurationContext(mapperConfigurationExpression,
                scope.ServiceProvider));
        });

        ValidateAll(mapperConfiguration);

        var mapperAccessor = serviceProvider.GetService<MapperAccessor>();
        mapperAccessor.Mapper = new Mapper(mapperConfiguration, serviceProvider.GetService);

        return mapperAccessor;
    }
}

image

@realLiangshiwei realLiangshiwei changed the title Consider initializing AutoMapper in advance Consider supporting Automapper dependency injection Sep 4, 2020
@realLiangshiwei
Copy link
Member Author

@hikalkan
Copy link
Member

hikalkan commented Sep 6, 2020

I didn't know this feature of the AutoMapper. While ABP has a way to use DI on the mapping, such a support would be good.

You can implement and send a PR.

@maliming
Copy link
Member

maliming commented Oct 8, 2020

@mtozlu
Copy link
Contributor

mtozlu commented Oct 8, 2020

Currently i use DI in Profile, however this issue should be implemented to allow usage of actions and native DI support of AutoMapper.
Here is how i use it for now:
In AppModule:

Configure<AbpAutoMapperOptions>(options =>
{
	options.AddMaps<PortalApplicationModule>();

	options.Configurators.Add(abpAutoMapperConfigurationContext =>
	{
		var profile = abpAutoMapperConfigurationContext.ServiceProvider.GetRequiredService<TestAutoMapperProfile>();
		abpAutoMapperConfigurationContext.MapperConfiguration.AddProfile(profile);
	});
});

Profile:

public class TestAutoMapperProfile : Profile, ITransientDependency
{
	public TestAutoMapperProfile() { }
	public TestAutoMapperProfile(ITranslatableObjectManager translatableObjectManager)
	{
		CreateMap<TestGroup, TestGroupListOutputDto>()
			.BeforeMap((s, d) =>
			{
				translatableObjectManager.Translate<TestGroup, TestGroupTranslation>(s);
			});
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants