Using AutoMapper.Extensions.Microsoft.DependencyInjection, instead of own implementation (#537)
This commit is contained in:
parent
e154f5381a
commit
5b84a2e7a2
|
|
@ -13,7 +13,7 @@
|
|||
<PackageTags>elsa, workflows</PackageTags>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LinqKit.Core" Version="1.1.21" />
|
||||
<PackageReference Include="MediatR" Version="9.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="10.1.1" />
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0" />
|
||||
<PackageReference Include="Humanizer.Core" Version="2.8.26" />
|
||||
<PackageReference Include="MediatR" Version="9.0.0" />
|
||||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
|
||||
|
|
|
|||
|
|
@ -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<Assembly>(), serviceLifetime: ServiceLifetime.Singleton);
|
||||
};
|
||||
|
||||
services.AddSingleton<InMemNetwork>();
|
||||
|
|
|
|||
|
|
@ -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<IConfigurationProvider>().CreateMapper(sp.GetService), lifetime));
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddAutoMapperProfile<TProfile>(this IServiceCollection services) where TProfile : Profile
|
||||
{
|
||||
services.TryAddProvider<Profile, TProfile>(ServiceLifetime.Transient);
|
||||
return services;
|
||||
}
|
||||
public static IServiceCollection AddAutoMapperProfile<TProfile>(this IServiceCollection services) where TProfile : Profile, new() =>
|
||||
services.Configure<MapperConfigurationExpression>(options => options.AddProfile<TProfile>());
|
||||
|
||||
public static IServiceCollection AddAutoMapperProfiles(this IServiceCollection services, IEnumerable<Assembly> assemblies) =>
|
||||
services.Scan(scan => scan.FromAssemblies(assemblies).AddClasses(classes => classes.AssignableTo<Profile>()).As<Profile>().WithTransientLifetime());
|
||||
services.Configure<MapperConfigurationExpression>(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<TAssemblyMarkerType>(this IServiceCollection services) => services.AddAutoMapperProfiles(typeof(TAssemblyMarkerType).Assembly);
|
||||
public static IServiceCollection AddAutoMapperProfiles(this IServiceCollection services, params Assembly[] assemblies) =>
|
||||
services.Configure<MapperConfigurationExpression>(options => options.AddMaps(assemblies));
|
||||
|
||||
public static IConfigurationProvider CreateAutoMapperConfiguration(this IServiceProvider serviceProvider)
|
||||
{
|
||||
var profiles = serviceProvider.GetServices<Profile>();
|
||||
|
||||
var configuration = new MapperConfiguration(
|
||||
x =>
|
||||
{
|
||||
foreach (var profile in profiles)
|
||||
x.AddProfile(profile);
|
||||
}
|
||||
);
|
||||
|
||||
return configuration;
|
||||
}
|
||||
public static IServiceCollection AddAutoMapperProfiles<TAssemblyMarkerType>(this IServiceCollection services) =>
|
||||
services.AddAutoMapperProfiles(typeof(TAssemblyMarkerType).Assembly);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue