From 78d66ac00d73a785ecdd686190b709044d07f3ad Mon Sep 17 00:00:00 2001 From: Max Vorchakov Date: Tue, 7 Nov 2023 18:55:42 +0100 Subject: [PATCH] Added method to override default services lifetime --- samples/PowerPipe.Sample/Program.cs | 8 +++++--- .../PowerPipeConfiguration.cs | 19 ++++++++++++++++++- .../ServiceRegistrar.cs | 13 ++++++++++++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/samples/PowerPipe.Sample/Program.cs b/samples/PowerPipe.Sample/Program.cs index 1538944..044f2eb 100644 --- a/samples/PowerPipe.Sample/Program.cs +++ b/samples/PowerPipe.Sample/Program.cs @@ -21,10 +21,12 @@ private static IServiceProvider ConfigureServices() //setup dependency injection IServiceCollection services = new ServiceCollection(); - services.AddPowerPipe(c => + services.AddPowerPipe(config => { - c.RegisterServicesFromAssemblies(Assembly.GetExecutingAssembly()) - .AddBehavior(typeof(SampleGenericStep<>), ServiceLifetime.Scoped); + config + .RegisterServicesFromAssemblies(Assembly.GetExecutingAssembly()) + .ChangeDefaultLifetime(ServiceLifetime.Scoped) + .AddBehavior(typeof(SampleGenericStep<>), ServiceLifetime.Singleton); }); services.AddSingleton(); diff --git a/src/PowerPipe.Extensions.MicrosoftDependencyInjection/PowerPipeConfiguration.cs b/src/PowerPipe.Extensions.MicrosoftDependencyInjection/PowerPipeConfiguration.cs index 0b45bba..516ec01 100644 --- a/src/PowerPipe.Extensions.MicrosoftDependencyInjection/PowerPipeConfiguration.cs +++ b/src/PowerPipe.Extensions.MicrosoftDependencyInjection/PowerPipeConfiguration.cs @@ -17,7 +17,24 @@ public class PowerPipeConfiguration internal ICollection AssembliesToRegister { get; } = new List(); internal ICollection BehaviorsToRegister { get; } = new List(); - + + internal ServiceLifetime DefaultLifetime { get; private set; } = ServiceLifetime.Transient; + + /// + /// Changes default (Transient) service registration life time + /// + /// + /// + public PowerPipeConfiguration ChangeDefaultLifetime(ServiceLifetime lifetime) + { + if (DefaultLifetime == lifetime) + return this; + + DefaultLifetime = lifetime; + + return this; + } + /// /// Register assembly to search implementations of steps from /// diff --git a/src/PowerPipe.Extensions.MicrosoftDependencyInjection/ServiceRegistrar.cs b/src/PowerPipe.Extensions.MicrosoftDependencyInjection/ServiceRegistrar.cs index 9dbaa10..263edc3 100644 --- a/src/PowerPipe.Extensions.MicrosoftDependencyInjection/ServiceRegistrar.cs +++ b/src/PowerPipe.Extensions.MicrosoftDependencyInjection/ServiceRegistrar.cs @@ -90,7 +90,18 @@ private static void ConnectImplementationsToTypes( foreach (var type in exactMatches.Where(type => configuration.BehaviorsToRegister.All(c => c.ImplementationType != type))) { - services.TryAddTransient(type); + switch (configuration.DefaultLifetime) + { + case ServiceLifetime.Singleton: + services.TryAddSingleton(type); + break; + case ServiceLifetime.Scoped: + services.TryAddScoped(type); + break; + case ServiceLifetime.Transient: + services.TryAddTransient(type); + break; + } } if (!inf.IsOpenGeneric())