Esempio n. 1
0
 /// <summary>
 /// Constructs a <see cref="ResolutionScopeBase"/>.
 /// </summary>
 public ResolutionScopeBase()
 {
     this.disposed            = new AtomicBool();
     this.rootItem            = DisposableItem.Empty;
     this.rootFinalizableItem = FinalizableItem.Empty;
     this.scopedItems         = new ConcurrentTree <object, object>();
     this.scopedInstances     = new ConcurrentTree <Type, object>();
 }
Esempio n. 2
0
 private ResolutionScope(IResolverSelector resolverSelector,
                         IExpressionBuilder expressionBuilder, IContainerContext containerContext,
                         DelegateCache delegateCache, object name)
 {
     this.rootItem            = DisposableItem.Empty;
     this.rootFinalizableItem = FinalizableItem.Empty;
     this.scopedItems         = AvlTree <object> .Empty;
     this.scopedInstances     = AvlTreeKeyValue <Type, object> .Empty;
     this.resolverSelector    = resolverSelector;
     this.expressionBuilder   = expressionBuilder;
     this.containerContext    = containerContext;
     this.Name          = name;
     this.delegateCache = delegateCache;
 }
Esempio n. 3
0
        /// <inheritdoc />
        public TDisposable AddDisposableTracking <TDisposable>(TDisposable disposable)
            where TDisposable : IDisposable
        {
            var item = new DisposableItem {
                Item = disposable, Next = this.rootItem
            };
            var current = this.rootItem;

            Swap.SwapValue(ref this.rootItem, current, item, root =>
                           new DisposableItem {
                Item = disposable, Next = root
            });

            return(disposable);
        }
Esempio n. 4
0
 private ResolutionScope(IResolverSelector resolverSelector, IServiceRegistrator serviceRegistrator,
                         IExpressionBuilder expressionBuilder, IContainerContext containerContext,
                         AvlTreeKeyValue <object, Func <IResolutionScope, object> >[] serviceDelegateCache,
                         AvlTreeKeyValue <object, Func <IResolutionScope, Delegate> >[] factoryDelegates, object name)
 {
     this.disposed            = new AtomicBool();
     this.rootItem            = DisposableItem.Empty;
     this.rootFinalizableItem = FinalizableItem.Empty;
     this.scopedItems         = AvlTreeKeyValue <object, object> .Empty;
     this.scopedInstances     = AvlTreeKeyValue <Type, object> .Empty;
     this.resolverSelector    = resolverSelector;
     this.serviceRegistrator  = serviceRegistrator;
     this.expressionBuilder   = expressionBuilder;
     this.containerContext    = containerContext;
     this.Name             = name;
     this.serviceDelegates = serviceDelegateCache;
     this.factoryDelegates = factoryDelegates;
     this.indexBound       = this.serviceDelegates.Length - 1;
 }
Esempio n. 5
0
        public void TimedLRUCache_AutoDispose()
        {
            TimedLRUCache <int, TestItem> cache;
            DisposableItem dItem0 = new DisposableItem(0);
            DisposableItem dItem1 = new DisposableItem(1);
            DisposableItem dItem2 = new DisposableItem(2);
            TestItem       item0  = new TestItem(0);
            TestItem       item1  = new TestItem(1);
            TestItem       item2  = new TestItem(2);

            cache             = new TimedLRUCache <int, TestItem>();
            cache.AutoDispose = true;
            cache.DefaultTTL  = TimeSpan.FromMilliseconds(200);

            // Verify that disposable items are disposed when they
            // are implicitly removed from the cache when the maximum
            // number of items allowed has been exceeded.

            cache.MaxItems = 2;
            cache.Add(0, dItem0);
            cache.Add(1, dItem1);
            cache.Add(2, dItem2);

            Assert.AreEqual(2, cache.Count);
            Assert.IsTrue(dItem0.IsDisposed);
            Assert.IsFalse(dItem1.IsDisposed);
            Assert.IsFalse(dItem2.IsDisposed);

            // Verify that disposable items are disposed when the
            // cache is cleared.

            cache.Clear();
            Assert.IsTrue(dItem1.IsDisposed);
            Assert.IsTrue(dItem2.IsDisposed);

            // Verify that disposable items are disposed when they
            // are explicitly removed.

            dItem0.IsDisposed = false;
            cache.Add(0, dItem0);
            cache.Remove(0);
            Assert.IsTrue(dItem0.IsDisposed);

            // Verify that disposable items are disposed when they
            // are replaced in the cache.

            cache.Clear();
            dItem0.IsDisposed = false;
            dItem1.IsDisposed = false;
            cache.Add(0, dItem0);
            cache[0] = dItem1;
            Assert.IsTrue(dItem0.IsDisposed);

            // Verify that replacing the same disposable item instance
            // doesn't dispose the object.

            cache.Clear();
            dItem0.IsDisposed = false;
            cache.Add(0, dItem0);
            cache[0] = dItem0;
            Assert.IsFalse(dItem0.IsDisposed);

            // Verify disposal after flushing.

            cache.Clear();
            dItem0.IsDisposed = false;
            dItem1.IsDisposed = false;
            dItem2.IsDisposed = false;
            cache.Add(0, dItem0);
            cache.Add(1, dItem1);
            cache.Add(2, dItem2);
            Thread.Sleep(250);
            cache.Flush();
            Assert.IsTrue(dItem0.IsDisposed);
            Assert.IsTrue(dItem1.IsDisposed);
            Assert.IsTrue(dItem2.IsDisposed);

            cache.Clear();
            dItem0.IsDisposed = false;
            dItem1.IsDisposed = false;
            dItem2.IsDisposed = false;
            cache.Add(0, dItem0);
            cache.Add(1, dItem1);
            cache.Add(2, dItem2);
            Thread.Sleep(250);
            cache.FlushLRU();
            Assert.IsTrue(dItem0.IsDisposed);
            Assert.IsTrue(dItem1.IsDisposed);
            Assert.IsTrue(dItem2.IsDisposed);

            // Verify that non-disposable items don't cause trouble
            // when AutoDispose=true

            cache.Clear();
            cache.Add(0, item0);
            cache.Add(1, item1);
            cache.Add(2, item2);
            cache.Remove(1);
            cache[1] = new TestItem(3);
            cache[2] = cache[2];
            cache.Clear();

            cache.Add(0, item0);
            cache.Add(1, item1);
            cache.Add(2, item2);
            Thread.Sleep(250);
            cache.Flush();

            cache.Add(0, item0);
            cache.Add(1, item1);
            cache.Add(2, item2);
            Thread.Sleep(250);
            cache.FlushLRU();
        }