From 5b84a2e7a2e0cce33501fcd02f36f2411be68619 Mon Sep 17 00:00:00 2001 From: Rafael Carnucci Date: Thu, 24 Dec 2020 14:25:43 +0100 Subject: [PATCH] Using AutoMapper.Extensions.Microsoft.DependencyInjection, instead of own implementation (#537) --- .../Elsa.Abstractions.csproj | 2 +- src/core/Elsa.Core/Elsa.Core.csproj | 1 + src/core/Elsa.Core/ElsaOptions.cs | 7 +++- .../AutoMapperServiceCollectionExtensions.cs | 40 ++++--------------- 4 files changed, 15 insertions(+), 35 deletions(-) diff --git a/src/core/Elsa.Abstractions/Elsa.Abstractions.csproj b/src/core/Elsa.Abstractions/Elsa.Abstractions.csproj index 0157eab40..d6419779a 100644 --- a/src/core/Elsa.Abstractions/Elsa.Abstractions.csproj +++ b/src/core/Elsa.Abstractions/Elsa.Abstractions.csproj @@ -13,7 +13,7 @@ elsa, workflows - + diff --git a/src/core/Elsa.Core/Elsa.Core.csproj b/src/core/Elsa.Core/Elsa.Core.csproj index fa52c2940..da3351923 100644 --- a/src/core/Elsa.Core/Elsa.Core.csproj +++ b/src/core/Elsa.Core/Elsa.Core.csproj @@ -16,6 +16,7 @@ + diff --git a/src/core/Elsa.Core/ElsaOptions.cs b/src/core/Elsa.Core/ElsaOptions.cs index ecdd6243e..4bd2c91f7 100644 --- a/src/core/Elsa.Core/ElsaOptions.cs +++ b/src/core/Elsa.Core/ElsaOptions.cs @@ -1,4 +1,7 @@ using System; +using System.Linq; +using System.Reflection; +using AutoMapper; using Elsa.Caching; using Elsa.DistributedLock; using Elsa.Persistence; @@ -33,8 +36,8 @@ namespace Elsa AddAutoMapper = () => { - services.AddAutoMapper(ServiceLifetime.Singleton); - services.AddSingleton(sp => sp.CreateAutoMapperConfiguration()); + // The profiles are added to AddWorkflowsCore so that they are not forgotten in case the AddAutoMapper function(option) is overridden. + services.AddAutoMapper(Enumerable.Empty(), serviceLifetime: ServiceLifetime.Singleton); }; services.AddSingleton(); diff --git a/src/core/Elsa.Core/Extensions/AutoMapperServiceCollectionExtensions.cs b/src/core/Elsa.Core/Extensions/AutoMapperServiceCollectionExtensions.cs index 185bc37d0..51767de0b 100644 --- a/src/core/Elsa.Core/Extensions/AutoMapperServiceCollectionExtensions.cs +++ b/src/core/Elsa.Core/Extensions/AutoMapperServiceCollectionExtensions.cs @@ -1,47 +1,23 @@ -using System; using System.Collections.Generic; -using System.Linq; using System.Reflection; using AutoMapper; +using AutoMapper.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; namespace Elsa { public static class AutoMapperServiceCollectionExtensions { - public static IServiceCollection AddAutoMapper(this IServiceCollection services, ServiceLifetime lifetime) - { - services.TryAdd(new ServiceDescriptor(typeof(IMapper), sp => sp.GetRequiredService().CreateMapper(sp.GetService), lifetime)); - return services; - } - - public static IServiceCollection AddAutoMapperProfile(this IServiceCollection services) where TProfile : Profile - { - services.TryAddProvider(ServiceLifetime.Transient); - return services; - } + public static IServiceCollection AddAutoMapperProfile(this IServiceCollection services) where TProfile : Profile, new() => + services.Configure(options => options.AddProfile()); public static IServiceCollection AddAutoMapperProfiles(this IServiceCollection services, IEnumerable assemblies) => - services.Scan(scan => scan.FromAssemblies(assemblies).AddClasses(classes => classes.AssignableTo()).As().WithTransientLifetime()); + services.Configure(options => options.AddMaps(assemblies)); - public static IServiceCollection AddAutoMapperProfiles(this IServiceCollection services, params Assembly[] assemblies) => services.AddAutoMapperProfiles(assemblies.AsEnumerable()); - public static IServiceCollection AddAutoMapperProfiles(this IServiceCollection services, Assembly assembly) => AddAutoMapperProfiles(services, new[] { assembly }); - public static IServiceCollection AddAutoMapperProfiles(this IServiceCollection services) => services.AddAutoMapperProfiles(typeof(TAssemblyMarkerType).Assembly); + public static IServiceCollection AddAutoMapperProfiles(this IServiceCollection services, params Assembly[] assemblies) => + services.Configure(options => options.AddMaps(assemblies)); - public static IConfigurationProvider CreateAutoMapperConfiguration(this IServiceProvider serviceProvider) - { - var profiles = serviceProvider.GetServices(); - - var configuration = new MapperConfiguration( - x => - { - foreach (var profile in profiles) - x.AddProfile(profile); - } - ); - - return configuration; - } + public static IServiceCollection AddAutoMapperProfiles(this IServiceCollection services) => + services.AddAutoMapperProfiles(typeof(TAssemblyMarkerType).Assembly); } } \ No newline at end of file