Skip to content

Commit f1b8e91

Browse files
Separate required and default services
1 parent f1beaec commit f1b8e91

3 files changed

Lines changed: 48 additions & 98 deletions

File tree

samples/ImageSharp.Web.Sample/Startup.cs

Lines changed: 27 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -37,71 +37,32 @@ public class Startup
3737
/// </summary>
3838
/// <param name="services">The collection of service desscriptors.</param>
3939
public void ConfigureServices(IServiceCollection services)
40-
{
41-
services.AddImageSharp()
42-
.SetRequestParser<QueryCollectionRequestParser>()
43-
.Configure<PhysicalFileSystemCacheOptions>(options =>
44-
{
45-
options.CacheRootPath = null;
46-
options.CacheFolder = "is-cache";
47-
options.CacheFolderDepth = 8;
48-
})
49-
.SetCache<PhysicalFileSystemCache>()
50-
.SetCacheKey<UriRelativeLowerInvariantCacheKey>()
51-
.SetCacheHash<SHA256CacheHash>()
52-
.Configure<PhysicalFileSystemProviderOptions>(options =>
53-
{
54-
options.ProviderRootPath = null;
55-
})
56-
.AddProvider<PhysicalFileSystemProvider>()
57-
.AddProcessor<ResizeWebProcessor>()
58-
.AddProcessor<FormatWebProcessor>()
59-
.AddProcessor<BackgroundColorWebProcessor>()
60-
.AddProcessor<QualityWebProcessor>();
61-
62-
// Add the default service and options.
63-
//
64-
// services.AddImageSharp();
65-
66-
// Or add the default service and custom options.
67-
//
68-
// this.ConfigureDefaultServicesAndCustomOptions(services);
69-
70-
// Or we can fine-grain control adding the default options and configure all other services.
71-
//
72-
// this.ConfigureCustomServicesAndDefaultOptions(services);
73-
74-
// Or we can fine-grain control adding custom options and configure all other services
75-
// There are also factory methods for each builder that will allow building from configuration files.
76-
//
77-
// this.ConfigureCustomServicesAndCustomOptions(services);
78-
}
40+
=> services.AddImageSharp(); // Add the default service and options
7941

42+
// Or add the default service and custom options
8043
private void ConfigureDefaultServicesAndCustomOptions(IServiceCollection services)
81-
{
82-
services.AddImageSharp(options =>
44+
=> services.AddImageSharp(options =>
8345
{
8446
options.Configuration = Configuration.Default;
8547
options.BrowserMaxAge = TimeSpan.FromDays(7);
8648
options.CacheMaxAge = TimeSpan.FromDays(365);
87-
options.CacheHashLength = 8;
49+
options.CacheHashLength = 12;
8850
options.OnParseCommandsAsync = _ => Task.CompletedTask;
8951
options.OnBeforeSaveAsync = _ => Task.CompletedTask;
9052
options.OnProcessedAsync = _ => Task.CompletedTask;
9153
options.OnPrepareResponseAsync = _ => Task.CompletedTask;
9254
});
93-
}
9455

56+
// Or we can fine-grain control adding the default options and configure all other services
9557
private void ConfigureCustomServicesAndDefaultOptions(IServiceCollection services)
96-
{
97-
services.AddImageSharp()
98-
.RemoveProcessor<FormatWebProcessor>()
99-
.RemoveProcessor<BackgroundColorWebProcessor>();
100-
}
58+
=> services.AddImageSharp()
59+
.RemoveProcessor<FormatWebProcessor>()
60+
.RemoveProcessor<BackgroundColorWebProcessor>();
10161

62+
// Or we can fine-grain control adding custom options and configure all other services
63+
// There are also factory methods for each builder that will allow building from configuration files
10264
private void ConfigureCustomServicesAndCustomOptions(IServiceCollection services)
103-
{
104-
services.AddImageSharp(options =>
65+
=> services.AddImageSharp(options =>
10566
{
10667
options.Configuration = Configuration.Default;
10768
options.BrowserMaxAge = TimeSpan.FromDays(7);
@@ -112,22 +73,22 @@ private void ConfigureCustomServicesAndCustomOptions(IServiceCollection services
11273
options.OnProcessedAsync = _ => Task.CompletedTask;
11374
options.OnPrepareResponseAsync = _ => Task.CompletedTask;
11475
})
115-
.SetRequestParser<QueryCollectionRequestParser>()
116-
.Configure<PhysicalFileSystemCacheOptions>(options =>
117-
{
118-
options.CacheFolder = "different-cache";
119-
})
120-
.SetCache<PhysicalFileSystemCache>()
121-
.SetCacheKey<UriRelativeLowerInvariantCacheKey>()
122-
.SetCacheHash<SHA256CacheHash>()
123-
.ClearProviders()
124-
.AddProvider<PhysicalFileSystemProvider>()
125-
.ClearProcessors()
126-
.AddProcessor<ResizeWebProcessor>()
127-
.AddProcessor<FormatWebProcessor>()
128-
.AddProcessor<BackgroundColorWebProcessor>()
129-
.AddProcessor<QualityWebProcessor>();
130-
}
76+
.SetRequestParser<QueryCollectionRequestParser>()
77+
.Configure<PhysicalFileSystemCacheOptions>(options =>
78+
{
79+
options.CacheFolder = "different-cache";
80+
options.CacheFolderDepth = 6;
81+
})
82+
.SetCache<PhysicalFileSystemCache>()
83+
.SetCacheKey<UriRelativeLowerInvariantCacheKey>()
84+
.SetCacheHash<SHA256CacheHash>()
85+
.ClearProviders()
86+
.AddProvider<PhysicalFileSystemProvider>()
87+
.ClearProcessors()
88+
.AddProcessor<ResizeWebProcessor>()
89+
.AddProcessor<FormatWebProcessor>()
90+
.AddProcessor<BackgroundColorWebProcessor>()
91+
.AddProcessor<QualityWebProcessor>();
13192

13293
/// <summary>
13394
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

src/ImageSharp.Web/DependencyInjection/ImageSharpBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
45
using Microsoft.Extensions.DependencyInjection;
56

67
namespace SixLabors.ImageSharp.Web.DependencyInjection
@@ -14,7 +15,7 @@ internal class ImageSharpBuilder : IImageSharpBuilder
1415
/// Initializes a new instance of the <see cref="ImageSharpBuilder"/> class.
1516
/// </summary>
1617
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
17-
public ImageSharpBuilder(IServiceCollection services) => this.Services = services;
18+
public ImageSharpBuilder(IServiceCollection services) => this.Services = services ?? throw new ArgumentNullException(nameof(services));
1819

1920
/// <inheritdoc/>
2021
public IServiceCollection Services { get; }

src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,21 @@ public static class ServiceCollectionExtensions
2424
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
2525
/// <returns>An <see cref="IImageSharpBuilder"/> that can be used to further configure the ImageSharp services.</returns>
2626
public static IImageSharpBuilder AddImageSharp(this IServiceCollection services)
27-
{
28-
Guard.NotNull(services, nameof(services));
29-
30-
IImageSharpBuilder builder = new ImageSharpBuilder(services);
31-
32-
AddDefaultServices(builder);
33-
34-
return builder;
35-
}
27+
=> new ImageSharpBuilder(services).AddRequiredServices().AddDefaultServices();
3628

3729
/// <summary>
3830
/// Adds ImageSharp services to the specified <see cref="IServiceCollection" /> with the given options.
3931
/// </summary>
4032
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
41-
/// <param name="setupAction">An <see cref="Action{ImageSharpMiddlewareOptions}"/> to configure the provided <see cref="ImageSharpMiddlewareOptions"/>.</param>
33+
/// <param name="configureOptions">An <see cref="Action{ImageSharpMiddlewareOptions}"/> to configure the provided <see cref="ImageSharpMiddlewareOptions"/>.</param>
4234
/// <returns>An <see cref="IImageSharpBuilder"/> that can be used to further configure the ImageSharp services.</returns>
43-
public static IImageSharpBuilder AddImageSharp(
44-
this IServiceCollection services,
45-
Action<ImageSharpMiddlewareOptions> setupAction)
46-
{
47-
Guard.NotNull(services, nameof(services));
48-
Guard.NotNull(setupAction, nameof(setupAction));
49-
50-
services.Configure(setupAction);
35+
public static IImageSharpBuilder AddImageSharp(this IServiceCollection services, Action<ImageSharpMiddlewareOptions> configureOptions)
36+
=> services.AddImageSharp().Configure(configureOptions);
5137

52-
return AddImageSharp(services);
53-
}
54-
55-
private static void AddDefaultServices(IImageSharpBuilder builder)
38+
private static IImageSharpBuilder AddRequiredServices(this IImageSharpBuilder builder)
5639
{
5740
builder.Services.AddSingleton<FormatUtilities>();
58-
41+
builder.Services.AddSingleton<CommandParser>();
5942
builder.Services.AddSingleton<AsyncKeyReaderWriterLock<string>>();
6043

6144
builder.SetRequestParser<QueryCollectionRequestParser>();
@@ -66,13 +49,6 @@ private static void AddDefaultServices(IImageSharpBuilder builder)
6649

6750
builder.SetCacheHash<SHA256CacheHash>();
6851

69-
builder.AddProvider<PhysicalFileSystemProvider>();
70-
71-
builder.AddProcessor<ResizeWebProcessor>()
72-
.AddProcessor<FormatWebProcessor>()
73-
.AddProcessor<BackgroundColorWebProcessor>()
74-
.AddProcessor<QualityWebProcessor>();
75-
7652
builder.AddConverter<IntegralNumberConverter<sbyte>>();
7753
builder.AddConverter<IntegralNumberConverter<byte>>();
7854
builder.AddConverter<IntegralNumberConverter<short>>();
@@ -119,7 +95,19 @@ private static void AddDefaultServices(IImageSharpBuilder builder)
11995
builder.AddConverter<ColorConverter>();
12096
builder.AddConverter<EnumConverter>();
12197

122-
builder.Services.AddSingleton<CommandParser>();
98+
return builder;
99+
}
100+
101+
private static IImageSharpBuilder AddDefaultServices(this IImageSharpBuilder builder)
102+
{
103+
builder.AddProvider<PhysicalFileSystemProvider>();
104+
105+
builder.AddProcessor<ResizeWebProcessor>()
106+
.AddProcessor<FormatWebProcessor>()
107+
.AddProcessor<BackgroundColorWebProcessor>()
108+
.AddProcessor<QualityWebProcessor>();
109+
110+
return builder;
123111
}
124112
}
125113
}

0 commit comments

Comments
 (0)