Skip to content

Commit

Permalink
Merge pull request #44 from mvSapphire/AddedMethodToOverrideDefaultLi…
Browse files Browse the repository at this point in the history
…fetime

Added method to override default services lifetime
  • Loading branch information
homolibere authored Nov 7, 2023
2 parents b8ba381 + 78d66ac commit 7b7a6e4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
8 changes: 5 additions & 3 deletions samples/PowerPipe.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SamplePipeline>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,24 @@ public class PowerPipeConfiguration
internal ICollection<Assembly> AssembliesToRegister { get; } = new List<Assembly>();

internal ICollection<ServiceDescriptor> BehaviorsToRegister { get; } = new List<ServiceDescriptor>();


internal ServiceLifetime DefaultLifetime { get; private set; } = ServiceLifetime.Transient;

/// <summary>
/// Changes default (Transient) service registration life time
/// </summary>
/// <param name="lifetime"></param>
/// <returns></returns>
public PowerPipeConfiguration ChangeDefaultLifetime(ServiceLifetime lifetime)
{
if (DefaultLifetime == lifetime)
return this;

DefaultLifetime = lifetime;

return this;
}

/// <summary>
/// Register assembly to search implementations of steps from
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 7b7a6e4

Please sign in to comment.