Beispiel #1
0
 public static void AddScopeCached<TInterface, TImplementation>(this IServiceCollection collection)
     where TInterface : class
     where TImplementation : class, TInterface
 {
     collection.AddScoped<TImplementation>();
     collection.AddScoped<TInterface>(provider =>
     {
         var service = provider.GetService<TImplementation>();
         return LifetimeCacheDecorator<TInterface>.Create(service);
     });
 }
Beispiel #2
0
        public static void AddScopeCached<TInterface, TImplementation>(this IServiceCollection collection,
            Func<IServiceProvider, TImplementation> implFactory)
            where TInterface : class
            where TImplementation : class, TInterface
        {
            if (implFactory == null)
            {
                throw new ArgumentNullException(nameof(implFactory));
            }

            collection.AddScoped<TInterface>(provider =>
            {
                var svc = implFactory(provider);
                return LifetimeCacheDecorator<TInterface>.Create(svc);
            });
        }
        public void WhenArgsAreDifferent_IndependentCallIsMade(object input1, object input2)
        {
            using (var sw = new StringWriter())
            {
                Console.SetOut(sw);

                var scopedDecorator = LifetimeCacheDecorator <IInternal> .Create(new Internal());

                var call1Result = scopedDecorator.Write(input1);
                var call2Result = scopedDecorator.Write(input2);

                Console.SetOut(new StreamWriter(Console.OpenStandardError(Int32.MaxValue)));

                var consoleMessages = sw.ToString().Split("\r\n", StringSplitOptions.RemoveEmptyEntries).ToList();

                Assert.AreEqual(2, consoleMessages.Count);
                Assert.IsTrue(consoleMessages.All(x => x == "RealMethodCall"));
            }
        }
        public void WhenRepeatedCallWithSameArgIsMade_SecondCallReturnsDataFromScopedCache()
        {
            using (var sw = new StringWriter())
            {
                Console.SetOut(sw);

                var scopedDecorator = LifetimeCacheDecorator <IInternal> .Create(new Internal());

                var call1Result = scopedDecorator.Write(1);
                var call2Result = scopedDecorator.Write(1);

                var consoleMessages = sw.ToString().Split("\r\n", StringSplitOptions.RemoveEmptyEntries).ToList();

                Assert.AreEqual(1, consoleMessages.Count);
                Assert.AreEqual("RealMethodCall", consoleMessages.Single());
                Assert.AreEqual("1", call1Result);
                Assert.AreEqual("1", call2Result);
            }
        }
        public void WhenRepeatedCallWithClassArgIsMade_SecondCallReturnsDataFromScopedCache(object input1, object input2)
        {
            using (var sw = new StringWriter())
            {
                Console.SetOut(sw);

                var scopedDecorator = LifetimeCacheDecorator <IInternal> .Create(new Internal());

                var call1Result = scopedDecorator.Write(input1);
                var call2Result = scopedDecorator.Write(input2);

                Console.SetOut(new StreamWriter(Console.OpenStandardError(Int32.MaxValue)));

                var consoleMessages = sw.ToString().Split("\r\n", StringSplitOptions.RemoveEmptyEntries).ToList();

                Assert.AreEqual(1, consoleMessages.Count);
                Assert.AreEqual("RealMethodCall", consoleMessages.Single());
                Assert.AreEqual(call1Result, call2Result);
            }
        }