Ejemplo n.º 1
0
 public void Dispose()
 {
     if (!disposed)
     {
         disposed = true;
         lifetime.Dispose();
     }
 }
Ejemplo n.º 2
0
        public void DisposingContainerDisposesOwnedObjects()
        {
            ILifetimeContainer container = new LifetimeContainer();
            DisposableObject   mdo       = new DisposableObject();

            container.Add(mdo);
            container.Dispose();

            Assert.IsTrue(mdo.WasDisposed);
        }
		public void DisposingContainerDisposesOwnedObjects()
		{
			ILifetimeContainer container = new LifetimeContainer();
			MockDisposableObject mdo = new MockDisposableObject();

			container.Add(mdo);
			container.Dispose();

			Assert.IsTrue(mdo.WasDisposed);
		}
Ejemplo n.º 4
0
        public void RemovingItemsFromContainerDoesNotDisposeThem()
        {
            ILifetimeContainer container = new LifetimeContainer();
            DisposableObject   mdo       = new DisposableObject();

            container.Add(mdo);
            container.Remove(mdo);
            container.Dispose();

            Assert.IsFalse(mdo.WasDisposed);
        }
		public void RemovingItemsFromContainerDoesNotDisposeThem()
		{
			ILifetimeContainer container = new LifetimeContainer();
			MockDisposableObject mdo = new MockDisposableObject();

			container.Add(mdo);
			container.Remove(mdo);
			container.Dispose();

			Assert.IsFalse(mdo.WasDisposed);
		}
        /// <summary>
        /// Dispose this container instance.
        /// </summary>
        /// <remarks>
        /// Disposing the container also disposes any child containers,
        /// and disposes any instances whose lifetimes are managed
        /// by the container.
        /// </remarks>
        public void Dispose()
        {
            List <Exception> exceptions = null;

            try
            {
                _parent?.LifetimeContainer.Remove(this);
                LifetimeContainer.Dispose();
            }
            catch (Exception exception)
            {
                exceptions = new List <Exception> {
                    exception
                };
            }

            if (null != _extensions)
            {
                foreach (IDisposable disposable in _extensions.OfType <IDisposable>()
                         .ToList())
                {
                    try
                    {
                        disposable.Dispose();
                    }
                    catch (Exception e)
                    {
                        if (null == exceptions)
                        {
                            exceptions = new List <Exception>();
                        }
                        exceptions.Add(e);
                    }
                }

                _extensions = null;
            }

            if (null != exceptions && exceptions.Count == 1)
            {
                throw exceptions[0];
            }
            else if (null != exceptions && exceptions.Count > 1)
            {
                throw new AggregateException(exceptions);
            }
        }
Ejemplo n.º 7
0
        public void DisposingItemsFromContainerDisposesInReverseOrderAdded()
        {
            ILifetimeContainer  container = new LifetimeContainer();
            DisposeOrderCounter obj1      = new DisposeOrderCounter();
            DisposeOrderCounter obj2      = new DisposeOrderCounter();
            DisposeOrderCounter obj3      = new DisposeOrderCounter();

            container.Add(obj1);
            container.Add(obj2);
            container.Add(obj3);

            container.Dispose();

            Assert.AreEqual(1, obj3.DisposePosition);
            Assert.AreEqual(2, obj2.DisposePosition);
            Assert.AreEqual(3, obj1.DisposePosition);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing,
        /// or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (lifetime != null)
            {
                List <object> items = new List <object>();
                items.AddRange(lifetime);

                foreach (object item in items)
                {
                    builder.TearDown(locator, item);
                }

                lifetime.Dispose();
                lifetime = null;
                locator  = null;
                builder  = null;
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Dispose this container instance.
        /// </summary>
        /// <remarks>
        /// This class doesn't have a finalizer, so <paramref name="disposing"/> will always be true.</remarks>
        /// <param name="disposing">True if being called from the IDisposable.Dispose
        /// method, false if being called from a finalizer.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (lifetimeContainer != null)
                {
                    lifetimeContainer.Dispose();
                    lifetimeContainer = null;

                    if (parent != null && parent.lifetimeContainer != null)
                    {
                        parent.lifetimeContainer.Remove(this);
                    }
                }

                extensions.OfType <IDisposable>().ForEach(ex => ex.Dispose());
                extensions.Clear();
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Dispose this container instance.
        /// </summary>
        /// <remarks>
        /// This class doesn't have a finalizer, so <paramref name="disposing"/> will always be true.</remarks>
        /// <param name="disposing">True if being called from the IDisposable.Dispose
        /// method, false if being called from a finalizer.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (lifetimeContainer != null)
                {
                    lifetimeContainer.Dispose();
                    lifetimeContainer = null;

                    if (parent != null && parent.lifetimeContainer != null)
                    {
                        parent.lifetimeContainer.Remove(this);
                    }
                }
                foreach (IDisposable disposableExtension in Sequence.OfType <IDisposable>(extensions))
                {
                    disposableExtension.Dispose();
                }
                extensions.Clear();
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Dispose this container instance.
        /// </summary>
        /// <remarks>
        /// This class doesn't have a finalizer, so <paramref name="disposing"/> will always be true.</remarks>
        /// <param name="disposing">True if being called from the IDisposable.Dispose
        /// method, false if being called from a finalizer.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (lifetimeContainer != null)
                {
                    // Avoid infinite loop when someone
                    //  registers something which would end up
                    //  disposing this container (e.g. container.RegisterInsance(container))
                    LifetimeContainer lifetimeContainerCopy = lifetimeContainer;
                    lifetimeContainer = null;
                    lifetimeContainerCopy.Dispose();

                    if (parent != null && parent.lifetimeContainer != null)
                    {
                        parent.lifetimeContainer.Remove(this);
                    }
                }

                extensions.OfType <IDisposable>().ForEach(ex => ex.Dispose());
                extensions.Clear();
            }
        }
Ejemplo n.º 12
0
        public void DisposingItemsFromContainerDisposesInReverseOrderAdded()
        {
            ILifetimeContainer container = new LifetimeContainer();
            DisposeOrderCounter obj1 = new DisposeOrderCounter();
            DisposeOrderCounter obj2 = new DisposeOrderCounter();
            DisposeOrderCounter obj3 = new DisposeOrderCounter();

            container.Add(obj1);
            container.Add(obj2);
            container.Add(obj3);

            container.Dispose();

            Assert.AreEqual(1, obj3.DisposePosition);
            Assert.AreEqual(2, obj2.DisposePosition);
            Assert.AreEqual(3, obj1.DisposePosition);
        }