Skip to content

Commit

Permalink
FEAT: add 2nd overload for AddEasyCaching() to support IServiceProvid…
Browse files Browse the repository at this point in the history
…er in setupActions (#487)
  • Loading branch information
MoienTajik committed Sep 8, 2023
1 parent 4ff2f40 commit 790b802
Showing 1 changed file with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
namespace Microsoft.Extensions.DependencyInjection
{
using EasyCaching.Core;
using EasyCaching.Core.Configurations;
using System;
using EasyCaching.Core.Configurations;
using System;

/// <summary>
/// EasyCaching service collection extensions.
/// </summary>
public static class EasyCachingServiceCollectionExtensions
{
/// <summary>
/// Adds the easycaching.
/// Adds the EasyCaching.
/// </summary>
/// <returns>The easy caching.</returns>
/// <param name="services">Services.</param>
Expand All @@ -30,5 +30,38 @@ public static IServiceCollection AddEasyCaching(this IServiceCollection services

return services;
}

/// <summary>
/// Adds the EasyCaching.
/// </summary>
/// <returns>The easy caching.</returns>
/// <param name="services">Services.</param>
/// <param name="setupAction">Setup action.</param>
public static IServiceCollection AddEasyCaching(this IServiceCollection services, Action<IServiceProvider, EasyCachingOptions> setupAction)
{
ArgumentCheck.NotNull(setupAction, nameof(setupAction));

// Options
services.AddSingleton(sp =>
{
var options = new EasyCachingOptions();
setupAction(sp, options);
return options;
});

// Extension services
services.AddSingleton(sp =>
{
var options = sp.GetRequiredService<EasyCachingOptions>();
foreach (var serviceExtension in options.Extensions)
{
serviceExtension.AddServices(services);
}
return options;
});

return services;
}
}
}

0 comments on commit 790b802

Please sign in to comment.