public void FunctionalLifeSpanTest_ShouldProduceDesiredResults()
        {
            // Arrange.
            var disposableObjectCount = 30;
            var disposableObjects     = new SimulatedInstrument[disposableObjectCount];
            var target = new ReferenceManager();

            for (var i = 0; i < disposableObjectCount; i++)
            {
                // Arrange.
                var disposableObject = new SimulatedInstrument(ConcurrencyControlMode.Unconstrained);
                disposableObject.StoreIntegerValue(i);
                disposableObjects[i] = disposableObject;

                // Act.
                target.AddObject(disposableObject);
                target.AddObject(new Object());
                target.AddObject((Object)null);

                // Assert.
                target.ObjectCount.Should().Be((i + 1) * 3);
            }

            // Act.
            target.Dispose();

            // Assert.
            target.ObjectCount.Should().Be(0);

            foreach (var disposableObject in disposableObjects)
            {
                // Assert.
                disposableObject.NullableIntegerValue.Should().BeNull();
            }
        }
示例#2
0
        private IMessageSubscriptionProfile CreateSubscriptionProfile()
        {
            var dependencyScope = CreateDependencyScope();

            ReferenceManager.AddObject(dependencyScope);
            return(new MessageSubscriptionProfile(dependencyScope));
        }
        private CommandMediator CreateMediator(IComponentContext provider)
        {
            var lifetimeScope   = provider.Resolve <ILifetimeScope>();
            var dependencyScope = new AutofacDependencyScope(lifetimeScope);

            ReferenceManager.AddObject(dependencyScope);
            return(new CommandMediator(dependencyScope));
        }
示例#4
0
        private CommandMediator CreateMediator(IServiceProvider provider)
        {
            var sourceScope     = provider.CreateScope();
            var dependencyScope = new DotNetNativeDependencyScope(sourceScope);

            ReferenceManager.AddObject(dependencyScope);
            return(new CommandMediator(dependencyScope));
        }
示例#5
0
        private void ScheduleHeartbeat(IHeartbeatScheduleItem heartbeatScheduleItem)
        {
            var timerCallback = new TimerCallback((state) => Task.Factory.StartNew(async() => await PublishHeartbeatMessageAsync(state as IHeartbeatScheduleItem).ConfigureAwait(false)));
            var timer         = new Timer(timerCallback, heartbeatScheduleItem, TimeSpan.Zero, TimeSpan.FromSeconds(heartbeatScheduleItem.IntervalInSeconds));

            HeartbeatTimers.Add(timer);
            ReferenceManager.AddObject(timer);
        }
        /// <summary>
        /// Creates a new child initialization and disposal scope for the current <see cref="DependencyScope{TScope}" />.
        /// </summary>
        /// <returns>
        /// A new child initialization and disposal scope for the current <see cref="DependencyScope{TScope}" />.
        /// </returns>
        /// <exception cref="CreateDependencyScopeException">
        /// An exception was raised while attempting to create the scope.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// The object is disposed.
        /// </exception>
        public IDependencyScope CreateChildScope()
        {
            RejectIfDisposed();

            try
            {
                var childScope = CreateChildScope(SourceScope);
                ReferenceManager.AddObject(childScope);
                return(childScope);
            }
            catch (Exception exception)
            {
                throw new CreateDependencyScopeException(exception);
            }
        }
        internal static IServiceCollection AddDependencyPackage <TConfigurator, TEngine, TPackage>(this IServiceCollection target, IConfiguration applicationConfiguration, out IServiceProvider serviceProvider)
            where TConfigurator : class, new()
            where TEngine : class, IDependencyEngine
            where TPackage : class, IDependencyPackage <TConfigurator, TEngine>, new()
        {
            if (target.Any(element => element.ServiceType == typeof(IDependencyEngine)))
            {
                throw new InvalidOperationException("Another dependency package has already been added to the service collection.");
            }

            var package = new TPackage();
            var engine  = DependencyEngine.New <TConfigurator, TEngine, TPackage>(applicationConfiguration, target);

            ReferenceManager.AddObject(engine);
            serviceProvider = engine.Provider;
            return(target);
        }
示例#8
0
        /// <summary>
        /// Returns the instance of specified type that is managed by the current <see cref="IFactoryProducedInstanceGroup" />.
        /// </summary>
        /// <typeparam name="T">
        /// The type of the instance to return.
        /// </typeparam>
        /// <returns>
        /// The instance of specified type that is managed by the current <see cref="IFactoryProducedInstanceGroup" />.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// <typeparamref name="T" /> is not a supported type for the group.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// The object is disposed.
        /// </exception>
        /// <exception cref="ObjectProductionException">
        /// An exception was raised during object production.
        /// </exception>
        public T Get <T>()
            where T : class
        {
            var instanceType = typeof(T);

            using (var controlToken = StateControl.Enter())
            {
                RejectIfDisposed();

                if (Instances.TryGetValue(instanceType, out var extantInstance))
                {
                    return(extantInstance as T);
                }

                var newInstance = Factory.Produce(instanceType) as T;
                Instances.Add(instanceType, newInstance);
                ReferenceManager.AddObject(newInstance);
                return(newInstance);
            }
        }
        /// <summary>
        /// Requests an object of specified type from the associated container for the current <see cref="IDependencyScope" />.
        /// </summary>
        /// <param name="type">
        /// The type of the object to resolve.
        /// </param>
        /// <returns>
        /// An object of specified type from the associated container.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="type" /> is <see langword="null" />.
        /// </exception>
        /// <exception cref="DependencyResolutionException">
        /// An exception was raised while attempting to resolve the dependency.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// The object is disposed.
        /// </exception>
        public Object Resolve(Type type)
        {
            RejectIfDisposed();

            try
            {
                var resolvedObject = Resolve(SourceScope, type.RejectIf().IsNull(nameof(type)));

                if (ManagesReferencesForSourceScope)
                {
                    ReferenceManager.AddObject(resolvedObject);
                }

                return(resolvedObject);
            }
            catch (Exception exception)
            {
                throw new DependencyResolutionException(type, exception);
            }
        }
示例#10
0
        /// <summary>
        /// Requests an object of specified type from the associated container for the current
        /// <see cref="DependencyScope{TScope}" />.
        /// </summary>
        /// <typeparam name="T">
        /// The type of the object to resolve.
        /// </typeparam>
        /// <returns>
        /// An object of specified type from the associated container.
        /// </returns>
        /// <exception cref="DependencyResolutionException">
        /// An exception was raised while attempting to resolve the dependency.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// The object is disposed.
        /// </exception>
        public T Resolve <T>()
            where T : class
        {
            RejectIfDisposed();

            try
            {
                var resolvedObject = Resolve <T>(SourceScope);

                if (ManagesReferencesForSourceScope)
                {
                    ReferenceManager.AddObject(resolvedObject);
                }

                return(resolvedObject);
            }
            catch (Exception exception)
            {
                throw new DependencyResolutionException(typeof(T), exception);
            }
        }
示例#11
0
        /// <summary>
        /// Creates a new initialization and disposal scope for the current
        /// <see cref="DependencyContainer{TContainer, TConfigurator}" />.
        /// </summary>
        /// <returns>
        /// A new initialization and disposal scope for the current <see cref="DependencyContainer{TContainer, TConfigurator}" />.
        /// </returns>
        /// <exception cref="CreateDependencyScopeException">
        /// An exception was raised while attempting to create the scope.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        /// The object is disposed.
        /// </exception>
        public IDependencyScope CreateScope()
        {
            using (var controlToken = StateControl.Enter())
            {
                RejectIfDisposed();

                try
                {
                    var scope = RootScope.CreateChildScope();
                    ReferenceManager.AddObject(scope);
                    return(scope);
                }
                catch (CreateDependencyScopeException)
                {
                    throw;
                }
                catch (Exception exception)
                {
                    throw new CreateDependencyScopeException(exception);
                }
            }
        }