Skip to content

Commit 22352d1

Browse files
Merge pull request #115 from SixLabors/js/simplify-di
Simplify Dependency Injection
2 parents 1695866 + 54a57f3 commit 22352d1

File tree

10 files changed

+183
-96
lines changed

10 files changed

+183
-96
lines changed

README.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ SixLabors.ImageSharp.Web
2121

2222
## License
2323

24-
- ImageSharp is licensed under the [Apache License, Version 2.0](https://opensource.org/licenses/Apache-2.0)
24+
- ImageSharp.Web is licensed under the [Apache License, Version 2.0](https://opensource.org/licenses/Apache-2.0)
2525
- An alternative Commercial License can be purchased for projects and applications requiring support.
2626
Please visit https://sixlabors.com/pricing for details.
2727

@@ -49,7 +49,7 @@ Install stable releases via Nuget; development releases are available via MyGet.
4949

5050
## Manual build
5151

52-
If you prefer, you can compile ImageSharp yourself (please do and help!)
52+
If you prefer, you can compile ImageSharp.Web yourself (please do and help!)
5353

5454
- Using [Visual Studio 2019](https://visualstudio.microsoft.com/vs/)
5555
- Make sure you have the latest version installed
@@ -124,12 +124,32 @@ Become a gold sponsor with a monthly donation of $1000 (providing 16 hours of ma
124124

125125
In addition you get your logo (large) on our README on GitHub and the home page (medium) of sixlabors.com
126126

127-
<a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/0/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/0/avatar.svg?avatarHeight=156"></a><a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/1/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/1/avatar.svg?avatarHeight=156"></a><a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/2/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/2/avatar.svg?avatarHeight=156"></a><a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/3/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/3/avatar.svg?avatarHeight=156"></a><a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/4/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/4/avatar.svg?avatarHeight=156"></a><a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/5/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/5/avatar.svg?avatarHeight=156"></a><a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/6/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/6/avatar.svg?avatarHeight=156"></a><a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/7/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/7/avatar.svg?avatarHeight=156"></a><a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/8/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/8/avatar.svg?avatarHeight=156"></a><a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/9/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/9/avatar.svg?avatarHeight=156"></a><a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/10/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/10/avatar.svg?avatarHeight=156"></a>
127+
<a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/0/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/0/avatar.svg?avatarHeight=156"></a>
128+
<a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/1/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/1/avatar.svg?avatarHeight=156"></a>
129+
<a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/2/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/2/avatar.svg?avatarHeight=156"></a>
130+
<a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/3/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/3/avatar.svg?avatarHeight=156"></a>
131+
<a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/4/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/4/avatar.svg?avatarHeight=156"></a>
132+
<a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/5/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/5/avatar.svg?avatarHeight=156"></a>
133+
<a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/6/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/6/avatar.svg?avatarHeight=156"></a>
134+
<a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/7/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/7/avatar.svg?avatarHeight=156"></a>
135+
<a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/8/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/8/avatar.svg?avatarHeight=156"></a>
136+
<a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/9/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/9/avatar.svg?avatarHeight=156"></a>
137+
<a href="https://opencollective.com/sixlabors/tiers/gold-sponsors/10/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/gold-sponsors/10/avatar.svg?avatarHeight=156"></a>
128138

129139
### Silver Sponsors
130140
Become a silver sponsor with a monthly donation of $500 (providing 8 hours of maintenance and development) and get your logo (medium) on our README on GitHub and the product pages of sixlabors.com
131141

132-
<a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/0/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/0/avatar.svg?avatarHeight=128"></a><a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/1/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/1/avatar.svg?avatarHeight=128"></a><a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/2/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/2/avatar.svg?avatarHeight=128"></a><a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/3/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/3/avatar.svg?avatarHeight=128"></a><a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/4/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/4/avatar.svg?avatarHeight=128"></a><a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/5/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/5/avatar.svg?avatarHeight=128"></a><a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/6/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/6/avatar.svg?avatarHeight=128"></a><a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/7/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/7/avatar.svg?avatarHeight=128"></a><a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/8/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/8/avatar.svg?avatarHeight=128"></a><a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/9/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/9/avatar.svg?avatarHeight=128"></a><a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/10/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/10/avatar.svg?avatarHeight=128"></a>
142+
<a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/0/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/0/avatar.svg?avatarHeight=128"></a>
143+
<a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/1/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/1/avatar.svg?avatarHeight=128"></a>
144+
<a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/2/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/2/avatar.svg?avatarHeight=128"></a>
145+
<a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/3/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/3/avatar.svg?avatarHeight=128"></a>
146+
<a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/4/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/4/avatar.svg?avatarHeight=128"></a>
147+
<a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/5/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/5/avatar.svg?avatarHeight=128"></a>
148+
<a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/6/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/6/avatar.svg?avatarHeight=128"></a>
149+
<a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/7/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/7/avatar.svg?avatarHeight=128"></a>
150+
<a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/8/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/8/avatar.svg?avatarHeight=128"></a>
151+
<a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/9/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/9/avatar.svg?avatarHeight=128"></a>
152+
<a href="https://opencollective.com/sixlabors/tiers/silver-sponsors/10/website" target="_blank"><img src="https://opencollective.com/sixlabors/tiers/silver-sponsors/10/avatar.svg?avatarHeight=128"></a>
133153

134154
### Bronze Sponsors
135155
Become a bronze sponsor with a monthly donation of $100 and get your logo (small) on our README on GitHub.

samples/ImageSharp.Web.Sample/Startup.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class Startup
3939
/// <param name="services">The collection of service desscriptors.</param>
4040
public void ConfigureServices(IServiceCollection services)
4141
{
42-
services.AddImageSharpCore()
42+
services.AddImageSharp()
4343
.SetRequestParser<QueryCollectionRequestParser>()
4444
.Configure<PhysicalFileSystemCacheOptions>(options =>
4545
{
@@ -102,7 +102,7 @@ private void ConfigureCustomServicesAndDefaultOptions(IServiceCollection service
102102

103103
private void ConfigureCustomServicesAndCustomOptions(IServiceCollection services)
104104
{
105-
services.AddImageSharpCore(
105+
services.AddImageSharp(
106106
options =>
107107
{
108108
options.Configuration = Configuration.Default;
@@ -128,7 +128,9 @@ private void ConfigureCustomServicesAndCustomOptions(IServiceCollection services
128128
provider.GetRequiredService<FormatUtilities>());
129129
})
130130
.SetCacheHash<CacheHash>()
131+
.ClearProviders()
131132
.AddProvider<PhysicalFileSystemProvider>()
133+
.ClearProcessors()
132134
.AddProcessor<ResizeWebProcessor>()
133135
.AddProcessor<FormatWebProcessor>()
134136
.AddProcessor<BackgroundColorWebProcessor>();

src/ImageSharp.Web/Caching/CacheHash.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5+
using System.Buffers;
56
using System.Runtime.CompilerServices;
67
using System.Security.Cryptography;
78
using System.Text;
@@ -18,19 +19,14 @@ namespace SixLabors.ImageSharp.Web.Caching
1819
/// </summary>
1920
public sealed class CacheHash : ICacheHash
2021
{
21-
private readonly MemoryAllocator memoryAllocator;
22-
2322
/// <summary>
2423
/// Initializes a new instance of the <see cref="CacheHash"/> class.
2524
/// </summary>
2625
/// <param name="options">The middleware configuration options.</param>
27-
/// <param name="memoryAllocator">The memory allocator.</param>
28-
public CacheHash(IOptions<ImageSharpMiddlewareOptions> options, MemoryAllocator memoryAllocator)
26+
public CacheHash(IOptions<ImageSharpMiddlewareOptions> options)
2927
{
3028
Guard.NotNull(options, nameof(options));
3129
Guard.MustBeBetweenOrEqualTo<uint>(options.Value.CachedNameLength, 2, 64, nameof(options.Value.CachedNameLength));
32-
33-
this.memoryAllocator = memoryAllocator;
3430
}
3531

3632
/// <inheritdoc/>
@@ -46,8 +42,19 @@ public string Create(string value, uint length)
4642
return HashValue(value, length, stackalloc byte[byteCount]);
4743
}
4844

49-
using IManagedByteBuffer buffer = this.memoryAllocator.AllocateManagedByteBuffer(byteCount);
50-
return HashValue(value, length, buffer.Memory.Span);
45+
byte[] buffer = null;
46+
try
47+
{
48+
buffer = ArrayPool<byte>.Shared.Rent(byteCount);
49+
return HashValue(value, length, buffer.AsSpan(0, byteCount));
50+
}
51+
finally
52+
{
53+
if (buffer != null)
54+
{
55+
ArrayPool<byte>.Shared.Return(buffer);
56+
}
57+
}
5158
}
5259

5360
[MethodImpl(MethodImplOptions.AggressiveInlining)]

src/ImageSharp.Web/DependencyInjection/ImageSharpCoreBuilderExtensions.cs renamed to src/ImageSharp.Web/DependencyInjection/ImageSharpBuilderExtensions.cs

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Linq;
76
using System.Reflection;
87
using Microsoft.Extensions.Configuration;
@@ -21,7 +20,7 @@ namespace SixLabors.ImageSharp.Web.DependencyInjection
2120
/// <summary>
2221
/// Extension methods for <see cref="IImageSharpBuilder"/> that allow configuration of services.
2322
/// </summary>
24-
public static class ImageSharpCoreBuilderExtensions
23+
public static class ImageSharpBuilderExtensions
2524
{
2625
/// <summary>
2726
/// Sets the given <see cref="IRequestParser"/> adding it to the service collection.
@@ -243,6 +242,18 @@ public static IImageSharpBuilder RemoveProcessor<TProcessor>(this IImageSharpBui
243242
return builder;
244243
}
245244

245+
/// <summary>
246+
/// Removes all <see cref="IImageWebProcessor"/> instances from the processor collection within the service collection.
247+
/// </summary>
248+
/// <param name="builder">The core builder.</param>
249+
/// <returns>The <see cref="IImageSharpBuilder"/>.</returns>
250+
public static IImageSharpBuilder ClearProcessors(this IImageSharpBuilder builder)
251+
{
252+
builder.Services.RemoveAll(typeof(IImageWebProcessor));
253+
254+
return builder;
255+
}
256+
246257
/// <summary>
247258
/// Registers an action used to configure a particular type of options.
248259
/// </summary>
@@ -270,37 +281,5 @@ public static IImageSharpBuilder Configure<TOptions>(this IImageSharpBuilder bui
270281
builder.Services.Configure(configureOptions);
271282
return builder;
272283
}
273-
274-
/// <summary>
275-
/// Sets the memory allocator configured in <see cref="Configuration.MemoryAllocator"/> of <see cref="ImageSharpMiddlewareOptions.Configuration"/>.
276-
/// </summary>
277-
/// <param name="builder">The core builder.</param>
278-
/// <returns>The <see cref="IImageSharpBuilder"/>.</returns>
279-
internal static IImageSharpBuilder SetMemoryAllocatorFromMiddlewareOptions(this IImageSharpBuilder builder)
280-
{
281-
static MemoryAllocator AllocatorFactory(IServiceProvider s)
282-
{
283-
return s.GetRequiredService<IOptions<ImageSharpMiddlewareOptions>>().Value.Configuration.MemoryAllocator;
284-
}
285-
286-
builder.SetMemoryAllocator(AllocatorFactory);
287-
return builder;
288-
}
289-
290-
/// <summary>
291-
/// Sets the <see cref="FormatUtilities"/> configured by <see cref="ImageSharpMiddlewareOptions.Configuration"/>.
292-
/// </summary>
293-
/// <param name="builder">The core builder.</param>
294-
/// <returns>The <see cref="IImageSharpBuilder"/>.</returns>
295-
internal static IImageSharpBuilder SetFormatUtilitesFromMiddlewareOptions(this IImageSharpBuilder builder)
296-
{
297-
static FormatUtilities FormatUtilitiesFactory(IServiceProvider s)
298-
{
299-
return new FormatUtilities(s.GetRequiredService<IOptions<ImageSharpMiddlewareOptions>>().Value.Configuration);
300-
}
301-
302-
builder.Services.AddSingleton(FormatUtilitiesFactory);
303-
return builder;
304-
}
305284
}
306285
}

0 commit comments

Comments
 (0)