Example #1
0
        /// <summary>
        /// Create a lifetime scope for the provided components and nested beneath a parent.
        /// </summary>
        /// <param name="tag">The tag applied to the <see cref="ILifetimeScope"/>.</param>
        /// <param name="componentRegistry">Components used in the scope.</param>
        /// <param name="parent">Parent scope.</param>
        protected LifetimeScope(IComponentRegistry componentRegistry, LifetimeScope parent, object tag)
            : this(componentRegistry, tag)
        {
            if (parent == null) throw new ArgumentNullException(nameof(parent));

            _parent = parent;
            RootLifetimeScope = _parent.RootLifetimeScope;
        }
Example #2
0
        public void CreateUnitOfWorkContainer()
        {
            ILifetimeScope unitOfWorkContainer = new LifetimeScope(new ComponentRegistry());

            _applicationContainer.Expect(ac => ac.BeginLifetimeScope()).Return(unitOfWorkContainer);
            var actual = _provider.CreateUnitOfWork();

            Assert.AreNotSame(actual, _applicationContainer);
        }
Example #3
0
        /// <summary>
        /// Begin a new tagged sub-scope, with additional components available to it.
        /// Component instances created via the new scope
        /// will be disposed along with it.
        /// </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 registations visible only in the new scope.</param>
        /// <returns>A new lifetime scope.</returns>
        /// <example>
        /// IContainer cr = // ...
        /// using (var lifetime = cr.BeginLifetimeScope("unitOfWork", builder =&gt; {
        ///         builder.RegisterType&lt;Foo&gt;();
        ///         builder.RegisterType&lt;Bar&gt;().As&lt;IBar&gt;(); })
        /// {
        ///     var foo = lifetime.Resolve&lt;Foo&gt;();
        /// }
        /// </example>
        public ILifetimeScope BeginLifetimeScope(object tag, Action <ContainerBuilder> configurationAction)
        {
            if (configurationAction == null)
            {
                throw new ArgumentNullException(nameof(configurationAction));
            }

            CheckNotDisposed();

            var locals = CreateScopeRestrictedRegistry(tag, configurationAction);
            var scope  = new LifetimeScope(locals, this, tag);

            RaiseBeginning(scope);

            return(scope);
        }
        /// <summary>
        /// Begin a new tagged sub-scope, with additional components available to it.
        /// Component instances created via the new scope
        /// will be disposed along with it.
        /// </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 new scope.</param>
        /// <returns>A new lifetime scope.</returns>
        /// <example>
        /// <code>
        /// IContainer cr = // ...
        /// using (var lifetime = cr.BeginLifetimeScope("unitOfWork", builder =&gt; {
        ///         builder.RegisterType&lt;Foo&gt;();
        ///         builder.RegisterType&lt;Bar&gt;().As&lt;IBar&gt;(); })
        /// {
        ///     var foo = lifetime.Resolve&lt;Foo&gt;();
        /// }
        /// </code>
        /// </example>
        public ILifetimeScope BeginLifetimeScope(object tag, Action <ContainerBuilder> configurationAction)
        {
            if (configurationAction == null)
            {
                throw new ArgumentNullException(nameof(configurationAction));
            }

            CheckNotDisposed();

            var locals = CreateScopeRestrictedRegistry(tag, configurationAction);
            var scope  = new LifetimeScope(locals, this, tag);

            scope.Disposer.AddInstanceForDisposal(locals);

            if (locals.Properties.TryGetValue(MetadataKeys.ContainerBuildOptions, out var options) &&
                !((ContainerBuildOptions)options).HasFlag(ContainerBuildOptions.IgnoreStartableComponents))
            {
                StartableManager.StartStartableComponents(scope);
            }

            RaiseBeginning(scope);

            return(scope);
        }
        /// <summary>
        /// Begin a new tagged sub-scope. Instances created via the sub-scope
        /// will be disposed along with it.
        /// </summary>
        /// <param name="tag">The tag applied to the <see cref="ILifetimeScope"/>.</param>
        /// <returns>A new lifetime scope.</returns>
        public ILifetimeScope BeginLifetimeScope(object tag)
        {
            CheckNotDisposed();

            ISharingLifetimeScope parentScope = this;

            while (parentScope != RootLifetimeScope)
            {
                if (parentScope.Tag.Equals(tag))
                {
                    throw new InvalidOperationException(
                              string.Format(CultureInfo.CurrentCulture, LifetimeScopeResources.DuplicateTagDetected, tag));
                }

                parentScope = parentScope.ParentLifetimeScope;
            }

            var registry = new CopyOnWriteRegistry(ComponentRegistry, () => CreateScopeRestrictedRegistry(tag, NoConfiguration));
            var scope    = new LifetimeScope(registry, this, tag);

            scope.Disposer.AddInstanceForDisposal(registry);
            RaiseBeginning(scope);
            return(scope);
        }
Example #6
0
 /// <summary>
 /// Create a lifetime scope for the provided components and nested beneath a parent.
 /// </summary>
 /// <param name="tag">The tag applied to the <see cref="ILifetimeScope"/>.</param>
 /// <param name="componentRegistry">Components used in the scope.</param>
 /// <param name="parent">Parent scope.</param>
 protected LifetimeScope(IComponentRegistry componentRegistry, LifetimeScope parent, object tag)
     : this(componentRegistry, tag)
 {
     _parent = Enforce.ArgumentNotNull(parent, "parent");
     _root = _parent.RootLifetimeScope;
 }
Example #7
0
        /// <summary>
        /// Begin a new tagged sub-scope, with additional components available to it.
        /// Component instances created via the new scope
        /// will be disposed along with it.
        /// </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 registations visible only in the new scope.</param>
        /// <returns>A new lifetime scope.</returns>
        /// <example>
        /// IContainer cr = // ...
        /// using (var lifetime = cr.BeginLifetimeScope("unitOfWork", builder =&gt; {
        ///         builder.RegisterType&lt;Foo&gt;();
        ///         builder.RegisterType&lt;Bar&gt;().As&lt;IBar&gt;(); })
        /// {
        ///     var foo = lifetime.Resolve&lt;Foo&gt;();
        /// }
        /// </example>
        public ILifetimeScope BeginLifetimeScope(object tag, Action<ContainerBuilder> configurationAction)
        {
            if (configurationAction == null) throw new ArgumentNullException("configurationAction");
            CheckNotDisposed();

            var locals = CreateScopeRestrictedRegistry(tag, configurationAction);
            var scope = new LifetimeScope(locals, this, tag);

            RaiseBeginning(scope);

            return scope;
        }
Example #8
0
 /// <summary>
 /// Begin a new tagged sub-scope. Instances created via the sub-scope
 /// will be disposed along with it.
 /// </summary>
 /// <param name="tag">The tag applied to the <see cref="ILifetimeScope"/>.</param>
 /// <returns>A new lifetime scope.</returns>
 public ILifetimeScope BeginLifetimeScope(object tag)
 {
     CheckNotDisposed();
     var registry = new CopyOnWriteRegistry(_componentRegistry, () => CreateScopeRestrictedRegistry(tag, NoConfiguration));
     var scope = new LifetimeScope(registry, this, tag);
     RaiseBeginning(scope);
     return scope;
 }
Example #9
0
 protected LifetimeScope(IComponentRegistry componentRegistry, LifetimeScope parent, object tag) : this(componentRegistry, tag)
 {
     this._parent = Enforce.ArgumentNotNull <LifetimeScope>(parent, "parent");
     this._root   = this._parent.RootLifetimeScope;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="LifetimeScope"/> class.
 /// </summary>
 /// <param name="tag">The tag applied to the <see cref="ILifetimeScope"/>.</param>
 /// <param name="componentRegistry">Components used in the scope.</param>
 /// <param name="parent">Parent scope.</param>
 protected LifetimeScope(IComponentRegistry componentRegistry, LifetimeScope parent, object tag)
     : this(componentRegistry, tag)
 {
     ParentLifetimeScope = parent ?? throw new ArgumentNullException(nameof(parent));
     RootLifetimeScope   = ParentLifetimeScope.RootLifetimeScope;
 }