Skip to content

Commit

Permalink
added di method; minor changes;
Browse files Browse the repository at this point in the history
  • Loading branch information
mvSapphire committed Sep 1, 2023
1 parent cde4ceb commit d75bdec
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,18 @@ public static IServiceCollection AddPowerPipeStep<TStep, TContext>(
_ => throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, null)
};
}

public static IServiceCollection AddPowerPipeCompensationStep<TStep, TContext>(
this IServiceCollection serviceCollection, ServiceLifetime lifetime = ServiceLifetime.Transient)
where TStep : class, IPipelineCompensationStep<TContext>
where TContext : class
{
return lifetime switch
{
ServiceLifetime.Transient => serviceCollection.AddTransient<TStep>(),
ServiceLifetime.Scoped => serviceCollection.AddScoped<TStep>(),
ServiceLifetime.Singleton => serviceCollection.AddSingleton<TStep>(),
_ => throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, null)
};
}
}
2 changes: 1 addition & 1 deletion tests/PowerPipe.UnitTests/PipelineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public async Task CompensateWith_Succeed()
var stepFactory =
new PipelineStepFactory(new ServiceCollection()
.AddPowerPipeStep<TestStep1, TestPipelineContext>()
.AddPowerPipeStep<TestStep2, TestPipelineContext>()
.AddTransient(_ => step2)
.AddTransient(_ => compensationStep)
.BuildServiceProvider());

Expand Down
5 changes: 2 additions & 3 deletions tests/PowerPipe.UnitTests/Steps/TestStep2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PowerPipe.Interfaces;
Expand All @@ -9,6 +8,6 @@ public class TestStep2 : IPipelineStep<TestPipelineContext>
{
public IPipelineStep<TestPipelineContext> NextStep { get; set; }

public virtual Task ExecuteAsync(TestPipelineContext context, CancellationToken cancellationToken) =>
throw new Exception();
public virtual async Task ExecuteAsync(TestPipelineContext context, CancellationToken cancellationToken) =>
await NextStep.ExecuteAsync(context, cancellationToken);
}

0 comments on commit d75bdec

Please sign in to comment.