Skip to content

Commit

Permalink
Improve DI example in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
reisenberger committed May 27, 2018
1 parent 9cafa60 commit 262dd9c
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@ public class Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddSingleton<Polly.Registry.IPolicyRegistry<string>, Polly.Registry.PolicyRegistry>();
services.AddSingleton<Polly.Caching.ISyncCacheProvider, Polly.Caching.MemoryCache.MemoryCacheProvider>(); // Or: IAsyncCacheProvider
// ...
}
services.AddSingleton<Polly.Caching.IAsyncCacheProvider, Polly.Caching.Memory.MemoryCacheProvider>();

services.AddSingleton<Polly.Registry.IPolicyRegistry<string>, Polly.Registry.PolicyRegistry>((serviceProvider) =>
{
PolicyRegistry registry = new PolicyRegistry();
registry.Add("myCachePolicy", Policy.CacheAsync<HttpResponseMessage>(serviceProvider.GetRequiredService<IAsyncCacheProvider>(), TimeSpan.FromMinutes(5)));
return registry;
});

public void Configure(..., IPolicyRegistry<string> policyRegistry, ISyncCacheProvider cacheProvider)
{
var cachePolicy = Policy.Cache(cacheProvider, TimeSpan.FromMinutes(5)); // Or: Policy.CacheAsync(IAsyncCacheProvider, ...)
policyRegistry.Add("myCachePolicy", cachePolicy);
// ...
}
}

// In a controller, inject the policyRegistry and retrieve the policy:
// (magic string "myCachePolicy" only hard-coded here to keep the example simple!)
// (magic string "myCachePolicy" only hard-coded here to keep the example simple)
public MyController(IPolicyRegistry<string> policyRegistry)
{
var _cachePolicy = policyRegistry.Get<ISyncPolicy>("myCachePolicy"); // Or: IAsyncPolicy
var _cachePolicy = policyRegistry.Get<IAsyncPolicy<HttpResponseMessage>>("myCachePolicy");
// ...
}

Expand Down

0 comments on commit 262dd9c

Please sign in to comment.