Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LifetimeScope"/> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        private LifetimeScope(LifetimeScope parent)
        {
            this.container = parent.container;
            this.parent    = parent;
            this.cachedKey = parent.cachedKey;
            this.context   = new ResolveContext()
            {
                Disposer = new Disposer(),
                Scope    = new ScopeComponentLifeStyleStorager()
                {
                    ContextCache = new TransientContextCache()
                },
                Singleton = parent.context.Singleton,
            };

            //自己resolve自己的时候,直接返回自身
            this.context.Scope.ContextCache.Set <ILifetimeScope>(cachedKey, this);
        }
Beispiel #2
0
        public void Dispose()
        {
            if (isDisposed)
            {
                return;
            }

            this.parent = null;
            if (this.context.Scope != null && this.context.Scope.ContextCache != null)
            {
                this.context.Scope.ContextCache.Clear();
            }

            if (this.context.Disposer == null)
            {
                if (this.context.Singleton.ContextCache != null)
                {
                    var ator = this.context.Singleton.ContextCache.GetEnumerator();
                    while (ator.MoveNext())
                    {
                        if (ator.Current is IDisposable)
                        {
                            ((IDisposable)ator.Current).Dispose();
                        }
                    }

                    this.context.Singleton.ContextCache.Clear();
                }
            }
            else
            {
                this.context.Disposer.Dispose();
            }

            this.isDisposed = true;
            if (this.OnDisposed != null)
            {
                this.OnDisposed.Invoke(this, EventArgs.Empty);
                this.OnDisposed = null;
            }
        }