private ILease ResolveLease()
 {
     var defaultLease = new SlidingLease(TimeSpan.FromMinutes(1));
     if (this.Kernel == null)
     {
         return defaultLease;
     }
     if (this.Kernel.HasComponent(typeof(ILease)))
     {
         return this.Kernel.Resolve<ILease>();
     }
     return defaultLease;
 }
Exemplo n.º 2
0
        public void ReuseTheSameCacheForSeveralComponents()
        {
            var container = new Container();

            var lease = new SlidingLease(TimeSpan.FromMinutes(1));
            var cache = new CacheLifecycle(lease);
            container.Configure(r =>
            {
                r.For<IIngredient>().LifecycleIs(cache).Use<Steak>();
                r.For<ICourse>().LifecycleIs(cache).Use<Course>();
            });

            var course1 = container.GetInstance<ICourse>();
            var course2 = container.GetInstance<ICourse>();

            var ingredient1 = Assert.IsAssignableFrom<Course>(course1).Ingredients.Single();
            var ingredient2 = Assert.IsAssignableFrom<Course>(course2).Ingredients.Single();

            Assert.Same(ingredient1, ingredient2);
        }
Exemplo n.º 3
0
        public void UsingCachingIngredientWithShortTimeout()
        {
            var lease = new SlidingLease(TimeSpan.FromTicks(1));
            var lifecycle = new CacheLifecycle(lease);

            var container = new Container();
            container.Configure(r => r
                .For<IIngredient>()
                .LifecycleIs(lifecycle)
                .Use<SauceBéarnaise>());

            var ingredient1 = container.GetInstance<IIngredient>();
            Thread.Sleep(TimeSpan.FromMilliseconds(1));
            var ingredient2 = container.GetInstance<IIngredient>();

            Assert.NotSame(ingredient1, ingredient2);
        }
Exemplo n.º 4
0
        public void UsingCachingIngredientWithLongTimeout()
        {
            var container = new Container();

            var lease = new SlidingLease(TimeSpan.FromMinutes(1));
            var cache = new CacheLifecycle(lease);
            container.Configure(r => r
                .For<IIngredient>()
                .LifecycleIs(cache)
                .Use<SauceBéarnaise>());

            var ingredient1 = container.GetInstance<IIngredient>();
            var ingredient2 = container.GetInstance<IIngredient>();

            Assert.Same(ingredient1, ingredient2);
        }
Exemplo n.º 5
0
        public void DisposeContainerDisposesCachedParsley()
        {
            var container = new UnityContainer();

            var lease = new SlidingLease(TimeSpan.FromMinutes(1));
            var cache = new CacheLifetimeManager(lease);
            container.RegisterType<IIngredient, Parsley>(cache);

            var ingredient = container.Resolve<IIngredient>();

            container.Dispose();

            var parsley = Assert.IsAssignableFrom<Parsley>(ingredient);
            Assert.True(parsley.IsDisposed);
        }
Exemplo n.º 6
0
        public void TeardownParsleyAsCachedDisposesParsleyWhenCacheLifetimeStrategyExtensionIsAdded()
        {
            var container = new UnityContainer();
            container.AddNewExtension<CacheLifetimeStrategyExtension>();

            var lease = new SlidingLease(TimeSpan.FromTicks(1));
            var cache = new CacheLifetimeManager(lease);
            container.RegisterType<IIngredient, Parsley>(cache);

            var ingredient = container.Resolve<IIngredient>();

            Thread.Sleep(TimeSpan.FromMilliseconds(1));

            container.Teardown(ingredient);

            var parsley = Assert.IsAssignableFrom<Parsley>(ingredient);
            Assert.True(parsley.IsDisposed);
        }
Exemplo n.º 7
0
        public void UsingCachingIngredientWithShortTimeout()
        {
            var container = new UnityContainer();

            var lease = new SlidingLease(TimeSpan.FromTicks(1));
            var cache = new CacheLifetimeManager(lease);
            container.RegisterType<IIngredient, SauceBéarnaise>(cache);

            var ingredient1 = container.Resolve<IIngredient>();
            Thread.Sleep(TimeSpan.FromMilliseconds(1));
            var ingredient2 = container.Resolve<IIngredient>();

            Assert.NotSame(ingredient1, ingredient2);
        }
Exemplo n.º 8
0
        public void UsingCachingIngredientWithLongTimeout()
        {
            var container = new UnityContainer();

            var lease = new SlidingLease(TimeSpan.FromMinutes(1));
            var cache = new CacheLifetimeManager(lease);
            container.RegisterType<IIngredient, SauceBéarnaise>(cache);

            var ingredient1 = container.Resolve<IIngredient>();
            var ingredient2 = container.Resolve<IIngredient>();

            Assert.Same(ingredient1, ingredient2);
        }