示例#1
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));
        }
示例#2
0
 public static Scope GetCurrentLifetimeScope(this Container container)
 {
     return(Lifestyle.GetCurrentScope(container));
 }