| | | 1 | | namespace Elsa.Extensions; |
| | | 2 | | |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | using System; |
| | | 5 | | using System.Linq; |
| | | 6 | | |
| | | 7 | | public static class ServiceCollectionExtensions |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Adds the service with a specific implementation type only if the combination |
| | | 11 | | /// of service and implementation does not already exist in the service collection. |
| | | 12 | | /// </summary> |
| | | 13 | | public static IServiceCollection TryAddScopedImplementation<TService, TImplementation>( |
| | | 14 | | this IServiceCollection services) |
| | | 15 | | where TService : class |
| | | 16 | | where TImplementation : class, TService |
| | | 17 | | { |
| | 776 | 18 | | if (!services.Any(sd => sd.ServiceType == typeof(TService) && sd.ImplementationType == typeof(TImplementation))) |
| | 2 | 19 | | services.AddScoped<TService, TImplementation>(); |
| | | 20 | | |
| | 6 | 21 | | return services; |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Adds the service with a specific implementation factory only if the combination |
| | | 26 | | /// of service and implementation already doesn't exist. |
| | | 27 | | /// </summary> |
| | | 28 | | public static IServiceCollection TryAddScopedImplementation<TService>( |
| | | 29 | | this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory) |
| | | 30 | | where TService : class |
| | | 31 | | { |
| | 0 | 32 | | if (services.All(sd => sd.ServiceType != typeof(TService))) |
| | 0 | 33 | | services.AddScoped(implementationFactory); |
| | | 34 | | |
| | 0 | 35 | | return services; |
| | | 36 | | } |
| | | 37 | | } |