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; }
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); }
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); }
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); }
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); }
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); }
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); }
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); }