コード例 #1
0
        public void SingletonsFromRegistrationSourceAreWrappedWithLifetimeDecorator()
        {
            var restrictedRootScopeLifetime = new MatchingScopeLifetime(new object());
            var tracker = new ScopeRestrictedRegisteredServicesTracker(restrictedRootScopeLifetime);

            var builder = new ComponentRegistryBuilder(tracker, new Dictionary <string, object>());

            builder.AddRegistrationSource(new ObjectRegistrationSource());

            var typedService = new TypedService(typeof(object));
            var registry     = builder.Build();

            registry.TryGetRegistration(typedService, out IComponentRegistration registration);

            Assert.IsType <ComponentRegistrationLifetimeDecorator>(registration);
        }
コード例 #2
0
ファイル: LifetimeScope.cs プロジェクト: weelink/Autofac
        /// <summary>
        /// Creates and setup the registry for a child scope.
        /// </summary>
        /// <param name="tag">The tag applied to the <see cref="ILifetimeScope"/>.</param>
        /// <param name="configurationAction">Action on a <see cref="ContainerBuilder"/>
        /// that adds component registrations visible only in the child scope.</param>
        /// <returns>Registry to use for a child scope.</returns>
        /// <remarks>It is the responsibility of the caller to make sure that the registry is properly
        /// disposed of. This is generally done by adding the registry to the <see cref="Disposer"/>
        /// property of the child scope.</remarks>
        private IComponentRegistryBuilder CreateScopeRestrictedRegistry(object tag, Action <ContainerBuilder> configurationAction)
        {
            var restrictedRootScopeLifetime = new MatchingScopeLifetime(tag);
            var tracker = new ScopeRestrictedRegisteredServicesTracker(restrictedRootScopeLifetime);

            var fallbackProperties = new FallbackDictionary <string, object?>(ComponentRegistry.Properties);
            var registryBuilder    = new ComponentRegistryBuilder(tracker, fallbackProperties);

            var builder = new ContainerBuilder(fallbackProperties, registryBuilder);

            foreach (var source in ComponentRegistry.Sources)
            {
                if (source.IsAdapterForIndividualComponents)
                {
                    builder.RegisterSource(source);
                }
            }

            // Issue #272: Only the most nested parent registry with HasLocalComponents is registered as an external source
            // It provides all non-adapting registrations from itself and from it's parent registries
            ISharingLifetimeScope?parent = this;

            while (parent != null)
            {
                if (parent.ComponentRegistry.HasLocalComponents)
                {
                    var externalSource = new ExternalRegistrySource(parent.ComponentRegistry);
                    builder.RegisterSource(externalSource);
                    break;
                }

                parent = parent.ParentLifetimeScope;
            }

            configurationAction(builder);

            builder.UpdateRegistry(registryBuilder);
            return(registryBuilder);
        }