public OptimizableLifestyleInfo(ScopedLifestyle lifestyle,
                                            IEnumerable <OptimizableRegistrationInfo> registrations)
            {
                this.Lifestyle     = lifestyle;
                this.Registrations = registrations.ToArray();
                this.Variable      = Expression.Variable(typeof(LazyScope));

                this.InitializeRegistrations();
            }
Beispiel #2
0
        /// <summary>
        /// The hybrid lifestyle allows mixing two lifestyles in a single registration. Based on the supplied
        /// <paramref name="lifestyleSelector"/> delegate the hybrid lifestyle will redirect the creation of
        /// the instance to the correct lifestyle. The result of the <paramref name="lifestyleSelector"/>
        /// delegate will not be cached; it is invoked each time an instance is requested or injected. By
        /// nesting hybrid lifestyles, any number of lifestyles can be mixed.
        /// </summary>
        /// <param name="lifestyleSelector">The <see cref="Func{TResult}"/> delegate that determines which
        /// lifestyle should be used. The <paramref name="trueLifestyle"/> will be used if <b>true</b> is
        /// returned; the <paramref name="falseLifestyle"/> otherwise. This delegate will be called every
        /// time an instance needs to be resolved or injected.</param>
        /// <param name="trueLifestyle">The scoped lifestyle to use when <paramref name="lifestyleSelector"/>
        /// returns <b>true</b>.</param>
        /// <param name="falseLifestyle">The scoped lifestyle to use when <paramref name="lifestyleSelector"/>
        /// returns <b>false</b>.</param>
        /// <returns>A new scoped hybrid lifestyle that wraps the supplied lifestyles.</returns>
        /// <exception cref="ArgumentNullException">Thrown when one of the supplied arguments is a null
        /// reference (Nothing in VB).</exception>
        /// <example>
        /// <para>
        /// The following example shows the creation of a <b>HybridLifestyle</b> that mixes an
        /// <b>WebRequestLifestyle</b> and <b>ThreadScopedLifestyle</b>:
        /// </para>
        /// <code lang="cs"><![CDATA[
        /// // NOTE: WebRequestLifestyle is located in SimpleInjector.Integration.Web.dll.
        /// var mixedScopeLifestyle = Lifestyle.CreateHybrid(
        ///     () => HttpContext.Current != null,
        ///     new WebRequestLifestyle(),
        ///     new ThreadScopedLifestyle());
        ///
        /// // The created lifestyle can be reused for many registrations.
        /// container.Register<IUserRepository, SqlUserRepository>(mixedScopeLifestyle);
        /// container.Register<ICustomerRepository, SqlCustomerRepository>(mixedScopeLifestyle);
        /// ]]></code>
        /// </example>
        public static ScopedLifestyle CreateHybrid(Func <bool> lifestyleSelector, ScopedLifestyle trueLifestyle,
                                                   ScopedLifestyle falseLifestyle)
        {
            Requires.IsNotNull(lifestyleSelector, nameof(lifestyleSelector));
            Requires.IsNotNull(trueLifestyle, nameof(trueLifestyle));
            Requires.IsNotNull(falseLifestyle, nameof(falseLifestyle));

            return(new ScopedHybridLifestyle(c => lifestyleSelector(), trueLifestyle, falseLifestyle));
        }
        /// <summary>
        /// The hybrid lifestyle allows mixing two lifestyles in a single registration. Based on the supplied
        /// <paramref name="lifestyleSelector"/> delegate the hybrid lifestyle will redirect the creation of
        /// the instance to the correct lifestyle. The result of the <paramref name="lifestyleSelector"/>
        /// delegate will not be cached; it is invoked each time an instance is requested or injected. By
        /// nesting hybrid lifestyles, any number of lifestyles can be mixed.
        /// </summary>
        /// <param name="lifestyleSelector">The <see cref="Func{TResult}"/> delegate that determines which
        /// lifestyle should be used. The <paramref name="trueLifestyle"/> will be used if <b>true</b> is
        /// returned; the <paramref name="falseLifestyle"/> otherwise. This delegate will be called every
        /// time an instance needs to be resolved or injected.</param>
        /// <param name="trueLifestyle">The scoped lifestyle to use when <paramref name="lifestyleSelector"/>
        /// returns <b>true</b>.</param>
        /// <param name="falseLifestyle">The scoped lifestyle to use when <paramref name="lifestyleSelector"/>
        /// returns <b>false</b>.</param>
        /// <returns>A new scoped hybrid lifestyle that wraps the supplied lifestyles.</returns>
        /// <exception cref="ArgumentNullException">Thrown when one of the supplied arguments is a null
        /// reference (Nothing in VB).</exception>
        /// <example>
        /// <para>
        /// The following example shows the creation of a <b>HybridLifestyle</b> that mixes an
        /// <b>WebRequestLifestyle</b> and <b>LifetimeScopeLifestyle</b>:
        /// </para>
        /// <code lang="cs"><![CDATA[
        /// // NOTE: WebRequestLifestyle is located in SimpleInjector.Integration.Web.dll.
        /// // NOTE: LifetimeScopeLifestyle is located in SimpleInjector.Extensions.LifetimeScoping.dll.
        /// var mixedScopeLifestyle = Lifestyle.CreateHybrid(
        ///     () => HttpContext.Current != null,
        ///     new WebRequestLifestyle(),
        ///     new LifetimeScopeLifestyle());
        ///
        /// // The created lifestyle can be reused for many registrations.
        /// container.Register<IUserRepository, SqlUserRepository>(mixedScopeLifestyle);
        /// container.Register<ICustomerRepository, SqlCustomerRepository>(mixedScopeLifestyle);
        /// ]]></code>
        /// </example>
        public static ScopedLifestyle CreateHybrid(Func <bool> lifestyleSelector, ScopedLifestyle trueLifestyle,
                                                   ScopedLifestyle falseLifestyle)
        {
            Requires.IsNotNull(lifestyleSelector, "lifestyleSelector");
            Requires.IsNotNull(trueLifestyle, "trueLifestyle");
            Requires.IsNotNull(falseLifestyle, "falseLifestyle");

            return(new ScopedHybridLifestyle(lifestyleSelector, trueLifestyle, falseLifestyle));
        }
Beispiel #4
0
        /// <summary>
        /// The hybrid lifestyle allows mixing two lifestyles in a single registration. The hybrid will use
        /// the <paramref name="defaultLifestyle"/> in case its
        /// <see cref="ScopedLifestyle.GetCurrentScope(Container)">GetCurrentScope</see> method returns a
        /// scope; otherwise the <paramref name="fallbackLifestyle"/> is used. The hybrid lifestyle will
        /// redirect the creation of the instance to the selected lifestyle. By nesting hybrid lifestyles,
        /// any number of lifestyles can be mixed.
        /// </summary>
        /// <param name="defaultLifestyle">The lifestyle to use when its
        /// <see cref="ScopedLifestyle.GetCurrentScope(Container)">GetCurrentScope</see> method returns a
        /// scope..</param>
        /// <param name="fallbackLifestyle">The lifestyle to use when the
        ///  <see cref="ScopedLifestyle.GetCurrentScope(Container)">GetCurrentScope</see> method of the
        /// <paramref name="defaultLifestyle"/> argument returns <b>null</b>.</param>
        /// <returns>A new hybrid lifestyle that wraps the supplied lifestyles.</returns>
        /// <exception cref="ArgumentNullException">Thrown when one of the supplied arguments is a null
        /// reference (Nothing in VB).</exception>
        /// <example>
        /// <para>
        /// The following example shows the creation of a <b>HybridLifestyle</b> that mixes an
        /// <b>ThreadScopedLifestyle</b> and <b>Transient</b>:
        /// </para>
        /// <code lang="cs"><![CDATA[
        /// // NOTE: WebRequestLifestyle is located in SimpleInjector.Integration.Web.dll.
        /// ScopedLifestyle hybridLifestyle = Lifestyle.CreateHybrid(
        ///     defaultLifestyle: new ThreadScopedLifestyle(),
        ///     fallbackLifestyle: new WebRequestLifestyle());
        ///
        /// // The created lifestyle can be reused for many registrations.
        /// container.Register<IUserRepository, SqlUserRepository>(hybridLifestyle);
        /// container.Register<ICustomerRepository, SqlCustomerRepository>(hybridLifestyle);
        /// ]]></code>
        /// <para>
        /// Hybrid lifestyles can be nested:
        /// </para>
        /// <code lang="cs"><![CDATA[
        /// ScopedLifestyle hybridLifestyle = Lifestyle.CreateHybrid(
        ///     defaultLifestyle: new ThreadScopedLifestyle(),
        ///     fallbackLifestyle: new WebRequestLifestyle());
        ///
        /// var hybridLifestyle = Lifestyle.CreateHybrid(hybridLifestyle, Lifestyle.Transient);
        /// ]]></code>
        /// <para>
        /// The <b>mixedScopeLifestyle</b> now mixed three lifestyles: Web Request, Thread Scoped and
        /// Transient.
        /// </para>
        /// </example>
        public static ScopedLifestyle CreateHybrid(ScopedLifestyle defaultLifestyle, ScopedLifestyle fallbackLifestyle)
        {
            Requires.IsNotNull(defaultLifestyle, nameof(defaultLifestyle));
            Requires.IsNotNull(fallbackLifestyle, nameof(fallbackLifestyle));

            return(new DefaultFallbackScopedHybridLifestyle(
                       defaultLifestyle: defaultLifestyle,
                       fallbackLifestyle: fallbackLifestyle));
        }
Beispiel #5
0
        /// <summary>
        /// The hybrid lifestyle allows mixing two lifestyles in a single registration. The hybrid will use
        /// the <paramref name="defaultLifestyle"/> in case its
        /// <see cref="ScopedLifestyle.GetCurrentScope(Container)">GetCurrentScope</see> method returns a
        /// scope; otherwise the <paramref name="fallbackLifestyle"/> is used. The hybrid lifestyle will
        /// redirect the creation of the instance to the selected lifestyle. By nesting hybrid lifestyles,
        /// any number of lifestyles can be mixed.
        /// </summary>
        /// <param name="defaultLifestyle">The lifestyle to use when its
        /// <see cref="ScopedLifestyle.GetCurrentScope(Container)">GetCurrentScope</see> method returns a
        /// scope..</param>
        /// <param name="fallbackLifestyle">The lifestyle to use when the
        ///  <see cref="ScopedLifestyle.GetCurrentScope(Container)">GetCurrentScope</see> method of the
        /// <paramref name="defaultLifestyle"/> argument returns <b>null</b>.</param>
        /// <returns>A new hybrid lifestyle that wraps the supplied lifestyles.</returns>
        /// <exception cref="ArgumentNullException">Thrown when one of the supplied arguments is a null
        /// reference (Nothing in VB).</exception>
        /// <example>
        /// <para>
        /// The following example shows the creation of a <b>HybridLifestyle</b> that mixes an
        /// <b>ThreadScopedLifestyle</b> and <b>Transient</b>:
        /// </para>
        /// <code lang="cs"><![CDATA[
        /// // NOTE: WebRequestLifestyle is located in SimpleInjector.Integration.Web.dll.
        /// ScopedLifestyle hybridLifestyle = Lifestyle.CreateHybrid(
        ///     defaultLifestyle: new ThreadScopedLifestyle(),
        ///     fallbackLifestyle: new WebRequestLifestyle());
        ///
        /// // The created lifestyle can be reused for many registrations.
        /// container.Register<IUserRepository, SqlUserRepository>(hybridLifestyle);
        /// container.Register<ICustomerRepository, SqlCustomerRepository>(hybridLifestyle);
        /// ]]></code>
        /// <para>
        /// Hybrid lifestyles can be nested:
        /// </para>
        /// <code lang="cs"><![CDATA[
        /// ScopedLifestyle hybridLifestyle = Lifestyle.CreateHybrid(
        ///     defaultLifestyle: new ThreadScopedLifestyle(),
        ///     fallbackLifestyle: new WebRequestLifestyle());
        ///
        /// var hybridLifestyle = Lifestyle.CreateHybrid(hybridLifestyle, Lifestyle.Transient);
        /// ]]></code>
        /// <para>
        /// The <b>mixedScopeLifestyle</b> now mixed three lifestyles: Web Request, Thread Scoped and
        /// Transient.
        /// </para>
        /// </example>
        public static ScopedLifestyle CreateHybrid(ScopedLifestyle defaultLifestyle, ScopedLifestyle fallbackLifestyle)
        {
            Requires.IsNotNull(defaultLifestyle, nameof(defaultLifestyle));
            Requires.IsNotNull(fallbackLifestyle, nameof(fallbackLifestyle));

            return(new ScopedHybridLifestyle(
                       lifestyleSelector: container => defaultLifestyle.GetCurrentScope(container) != null,
                       trueLifestyle: defaultLifestyle,
                       falseLifestyle: fallbackLifestyle));
        }
Beispiel #6
0
 internal static string TheServiceIsRequestedOutsideTheContextOfAScopedLifestyle(Type serviceType,
                                                                                 ScopedLifestyle lifestyle)
 {
     return(string.Format(CultureInfo.InvariantCulture,
                          "The {0} is registered as '{1}' lifestyle, but the instance is requested outside the " +
                          "context of a {1}.",
                          serviceType.ToFriendlyName(),
                          lifestyle.Name));
 }