Class ServiceCollectionExtensions
- Namespace
- Codebelt.Extensions.BenchmarkDotNet
- Assembly
- Codebelt.Extensions.BenchmarkDotNet.dll
Extension methods for the IServiceCollection interface.
public static class ServiceCollectionExtensions
- Inheritance
-
ServiceCollectionExtensions
Examples
The following example registers a benchmark workspace through the non-generic AddBenchmarkWorkspace overload — which defaults to the built-in BenchmarkWorkspace implementation — and the generic AddBenchmarkWorkspace<TWorkspace> overload — which lets a consumer plug in a custom IBenchmarkWorkspace (here, a FakeWorkspace that simply returns an empty assembly set). Both overloads register the workspace and the resolved options as singletons, so any consumer can resolve IBenchmarkWorkspace and BenchmarkWorkspaceOptions straight from the built IServiceProvider.
using System;
using System.Reflection;
using Codebelt.Extensions.BenchmarkDotNet;
using Microsoft.Extensions.DependencyInjection;
namespace MyBenchmarks;
// a custom workspace registered through the generic overload
public sealed class FakeWorkspace : IBenchmarkWorkspace
{
public Assembly[] LoadBenchmarkAssemblies() => Array.Empty<Assembly>();
public void PostProcessArtifacts() { }
}
public static class Program
{
public static void Main()
{
// default registration: registers BenchmarkWorkspace and the resolved options as singletons
var services = new ServiceCollection();
services.AddBenchmarkWorkspace(setup: o => o.BenchmarkProjectSuffix = "Benchmarks");
using (var provider = services.BuildServiceProvider())
{
var defaultWorkspace = provider.GetRequiredService<IBenchmarkWorkspace>();
var defaultOptions = provider.GetRequiredService<BenchmarkWorkspaceOptions>();
Console.WriteLine($"default: {defaultWorkspace.GetType().Name} / {defaultOptions.BenchmarkProjectSuffix}");
}
// generic registration: any IBenchmarkWorkspace implementation
var typed = new ServiceCollection();
typed.AddBenchmarkWorkspace<FakeWorkspace>(setup: o => o.RepositoryPath = @"C:\Repos\MyRepo");
using (var typedProvider = typed.BuildServiceProvider())
{
var customWorkspace = typedProvider.GetRequiredService<IBenchmarkWorkspace>();
var customOptions = typedProvider.GetRequiredService<BenchmarkWorkspaceOptions>();
Console.WriteLine($"custom: {customWorkspace.GetType().Name} / {customOptions.RepositoryPath}");
}
}
}
Methods
AddBenchmarkWorkspace(IServiceCollection, Action<BenchmarkWorkspaceOptions>)
Adds the default benchmark workspace implementation (BenchmarkWorkspace) to the specified IServiceCollection
public static IServiceCollection AddBenchmarkWorkspace(this IServiceCollection services, Action<BenchmarkWorkspaceOptions> setup = null)
Parameters
servicesIServiceCollectionThe IServiceCollection to add the services to.
setupAction<BenchmarkWorkspaceOptions>The BenchmarkWorkspaceOptions which may be configured.
Returns
- IServiceCollection
The original IServiceCollection instance for chaining.
AddBenchmarkWorkspace<TWorkspace>(IServiceCollection, Action<BenchmarkWorkspaceOptions>)
Adds a benchmark workspace implementation of type TWorkspace to the specified IServiceCollection
public static IServiceCollection AddBenchmarkWorkspace<TWorkspace>(this IServiceCollection services, Action<BenchmarkWorkspaceOptions> setup = null) where TWorkspace : class, IBenchmarkWorkspace
Parameters
servicesIServiceCollectionThe IServiceCollection to add the services to.
setupAction<BenchmarkWorkspaceOptions>The BenchmarkWorkspaceOptions which may be configured.
Returns
- IServiceCollection
The original IServiceCollection instance for chaining.
Type Parameters
TWorkspaceThe type that implements the IBenchmarkWorkspace interface.
Remarks
Validates the services parameter and the provided setup configurator.
Registers IBenchmarkWorkspace as a singleton using TWorkspace,
applies the provided configuration, and registers the resolved BenchmarkWorkspaceOptions as a singleton.