Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renamed methods; readme update; minor changes #46

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,22 @@ public static IServiceCollection AddPowerPipe(
By default all found implementations will be registered as Transient.

```csharp
services.AddPowerPipe(c =>
services.AddPowerPipe(cfg =>
{
c.RegisterServicesFromAssemblies(Assembly.GetExecutingAssembly());
cfg.RegisterServicesFromAssemblies(Assembly.GetExecutingAssembly());
});
```

But you can configure service lifetime per step implementation.

```csharp
services.AddPowerPipe(c =>
services.AddPowerPipe(cfg =>
{
c.RegisterServicesFromAssemblies(typeof(Program).Assembly)
.AddBehavior<Step1>(ServiceLifetime.Singleton)
.AddBehavior<Step2>(ServiceLifetime.Scoped)
// default Transient
.AddBehavior<TestStep3>();
cfg.RegisterServicesFromAssemblies(typeof(Program).Assembly)
.ChangeDefaultLifetime(ServiceLifetime.Scoped) // to override default lifetime
.AddSingleton<Step1>()
.AddTransient<Step2>()
.AddTransient(typeof(Step2));
});
```

Expand Down
2 changes: 1 addition & 1 deletion samples/PowerPipe.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private static IServiceProvider ConfigureServices()
config
.RegisterServicesFromAssemblies(Assembly.GetExecutingAssembly())
.ChangeDefaultLifetime(ServiceLifetime.Scoped)
.AddBehavior(typeof(SampleGenericStep<>), ServiceLifetime.Singleton);
.AddScoped(typeof(SampleGenericStep<>));
});

services.AddSingleton<SamplePipeline>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public class PowerPipeConfiguration
public PowerPipeConfiguration ChangeDefaultLifetime(ServiceLifetime lifetime)
{
if (DefaultLifetime == lifetime)
{
return this;
}

DefaultLifetime = lifetime;

Expand Down Expand Up @@ -63,31 +65,57 @@ public PowerPipeConfiguration RegisterServicesFromAssemblies(params Assembly[] a
}

/// <summary>
/// Register implementation with specific lifetime. By default - Transient.
/// Register implementation with specific lifetime
/// </summary>
/// <param name="serviceLifetime">ServiceLifetime.Transient by default</param>
/// <typeparam name="TServiceType">Type of step implementation</typeparam>
/// <returns>instance of PowerPipeConfiguration</returns>
public PowerPipeConfiguration AddBehavior<TServiceType>(ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
=> AddBehavior(typeof(TServiceType), serviceLifetime);
/// <returns>Instance of PowerPipeConfiguration</returns>
public PowerPipeConfiguration AddTransient<TServiceType>() =>
AddTransient(typeof(TServiceType));

/// <summary>
/// Register implementation with specific lifetime. By default - Transient.
/// Register implementation with specific lifetime
/// </summary>
/// <param name="type">Type of step implementation</param>
/// <param name="serviceLifetime">ServiceLifetime.Transient by default</param>
/// <returns>instance of PowerPipeConfiguration</returns>
public PowerPipeConfiguration AddBehavior(Type type, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
=> AddBehavior(type, type, serviceLifetime);
/// <returns>Instance of PowerPipeConfiguration</returns>
public PowerPipeConfiguration AddTransient(Type type) =>
Add(type, ServiceLifetime.Transient);

/// <summary>
/// Register implementation with specific lifetime
/// </summary>
/// <typeparam name="TServiceType">Type of step implementation</typeparam>
/// <returns>Instance of PowerPipeConfiguration</returns>
public PowerPipeConfiguration AddScoped<TServiceType>() =>
AddScoped(typeof(TServiceType));

/// <summary>
/// Register implementation with specific lifetime
/// </summary>
/// <param name="type">Type of step implementation</param>
/// <returns>Instance of PowerPipeConfiguration</returns>
public PowerPipeConfiguration AddScoped(Type type) =>
Add(type, ServiceLifetime.Scoped);

/// <summary>
/// Register implementation with specific lifetime
/// </summary>
/// <typeparam name="TServiceType">Type of step implementation</typeparam>
/// <returns>Instance of PowerPipeConfiguration</returns>
public PowerPipeConfiguration AddSingleton<TServiceType>() =>
AddSingleton(typeof(TServiceType));

#region PrivateMethods
/// <summary>
/// Register implementation with specific lifetime
/// </summary>
/// <param name="type">Type of step implementation</param>
/// <returns>Instance of PowerPipeConfiguration</returns>
public PowerPipeConfiguration AddSingleton(Type type) =>
Add(type, ServiceLifetime.Singleton);

private PowerPipeConfiguration AddBehavior(Type serviceType, Type implementationType, ServiceLifetime serviceLifetime)
private PowerPipeConfiguration Add(Type serviceType, ServiceLifetime serviceLifetime)
{
BehaviorsToRegister.Add(new ServiceDescriptor(serviceType, implementationType, serviceLifetime));
BehaviorsToRegister.Add(new ServiceDescriptor(serviceType, serviceType, serviceLifetime));

return this;
}

#endregion
}
6 changes: 2 additions & 4 deletions tests/PowerPipe.UnitTests/PipelineAutoDITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ public void TestAutoDIRegistration()
services.AddPowerPipe(c =>
{
c.RegisterServicesFromAssemblies(typeof(AutoDIFixture).Assembly)
.AddBehavior<TestStep1>(ServiceLifetime.Singleton)
.AddBehavior<TestStep2>(ServiceLifetime.Scoped)
// default Transient
.AddBehavior<TestStep3>();
.AddSingleton<TestStep1>()
.AddScoped<TestStep2>();
});

var serviceProvider = services.BuildServiceProvider();
Expand Down
Loading