From 262dd9c9a83e3806d29bc46f06ae2ca334f1ee46 Mon Sep 17 00:00:00 2001 From: Dylan Reisenberger Date: Sun, 27 May 2018 09:15:29 +0100 Subject: [PATCH] Improve DI example in readme --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e76dc0c..be09999 100644 --- a/README.md +++ b/README.md @@ -63,24 +63,24 @@ public class Startup public void ConfigureServices(IServiceCollection services) { services.AddMemoryCache(); - services.AddSingleton, Polly.Registry.PolicyRegistry>(); - services.AddSingleton(); // Or: IAsyncCacheProvider - // ... - } + services.AddSingleton(); + + services.AddSingleton, Polly.Registry.PolicyRegistry>((serviceProvider) => + { + PolicyRegistry registry = new PolicyRegistry(); + registry.Add("myCachePolicy", Policy.CacheAsync(serviceProvider.GetRequiredService(), TimeSpan.FromMinutes(5))); + return registry; + }); - public void Configure(..., IPolicyRegistry 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 policyRegistry) { - var _cachePolicy = policyRegistry.Get("myCachePolicy"); // Or: IAsyncPolicy + var _cachePolicy = policyRegistry.Get>("myCachePolicy"); // ... }