public void WithInstanceScope_CheckLifetimeTranslation(TypeRegistrationLifetime entLibLifetime, InstanceSharing autofacInstanceSharing, Type autofacLifetimeType)
        {
            var builder = new ContainerBuilder();

            builder.RegisterType <RegisteredServiceConsumer>().WithInstanceScope(entLibLifetime);
            var container = builder.Build();

            IComponentRegistration autofacRegistration = null;

            Assert.True(container.ComponentRegistry.TryGetRegistration(new TypedService(typeof(RegisteredServiceConsumer)), out autofacRegistration), "The service type was not registered into the container.");
            Assert.IsType(autofacLifetimeType, autofacRegistration.Lifetime);
            Assert.Equal(autofacInstanceSharing, autofacRegistration.Sharing);
        }
 /// <summary>
 /// Converts a TypeRegistrationLifetime to a LifestyleType.
 /// </summary>
 /// <param name="lifetime">The lifetime.</param>
 /// <returns></returns>
 private static LifestyleType ToLifestyle(TypeRegistrationLifetime lifetime)
 {
     if (lifetime == TypeRegistrationLifetime.Singleton)
     {
         return(LifestyleType.Singleton);
     }
     else if (lifetime == TypeRegistrationLifetime.Transient)
     {
         return(LifestyleType.Transient);
     }
     else
     {
         throw new ArgumentOutOfRangeException("entry.Lifetime",
                                               "Only Transient and Singleton are supported.");
     }
 }
Пример #3
0
        /// <summary>
        /// Sets an object registration to the Autofac equivalent of the provided
        /// Enterprise Library instance scope.
        /// </summary>
        /// <param name="registrar">
        /// The <see cref="Autofac.Builder.IRegistrationBuilder{TLimit, TActivatorData, TRegistrationStyle}"/>
        /// on which the instance scope should be set.
        /// </param>
        /// <param name="lifetime">The Enterprise Library lifetime to translate to Autofac.</param>
        /// <returns>A registration builder allowing further configuration of the component.</returns>
        /// <remarks>
        /// <para>
        /// Enterprise Library lifetimes translate to Autofac as follows:
        /// </para>
        /// <list type="table">
        /// <listheader>
        /// <term>Enterprise Library Lifetime</term>
        /// <description>Autofac Equivalent</description>
        /// </listheader>
        /// <item>
        /// <term>
        /// <see cref="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.TypeRegistrationLifetime.Singleton"/>
        /// </term>
        /// <description>
        /// <see cref="Autofac.Builder.IRegistrationBuilder{TLimit, TActivatorData, TRegistrationStyle}.SingleInstance"/>
        /// </description>
        /// </item>
        /// <item>
        /// <term>
        /// <see cref="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.TypeRegistrationLifetime.Transient"/>
        /// </term>
        /// <description>
        /// <see cref="Autofac.Builder.IRegistrationBuilder{TLimit, TActivatorData, TRegistrationStyle}.InstancePerDependency"/>
        /// </description>
        /// </item>
        /// </list>
        /// </remarks>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="registrar" /> is <see langword="null" />.
        /// </exception>
        public static IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> WithInstanceScope <TLimit, TActivatorData, TRegistrationStyle>(this IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> registrar, TypeRegistrationLifetime lifetime)
        {
            if (registrar == null)
            {
                throw new ArgumentNullException("registrar");
            }

            // There's only Singleton or Transient.
            if (lifetime == TypeRegistrationLifetime.Transient)
            {
                registrar.InstancePerDependency();
            }
            else
            {
                registrar.SingleInstance();
            }
            return(registrar);
        }
        public void WithInstanceScope_CheckLifetimeTranslation(TypeRegistrationLifetime entLibLifetime, InstanceSharing autofacInstanceSharing, Type autofacLifetimeType)
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<RegisteredServiceConsumer>().WithInstanceScope(entLibLifetime);
            var container = builder.Build();

            IComponentRegistration autofacRegistration = null;
            Assert.IsTrue(container.ComponentRegistry.TryGetRegistration(new TypedService(typeof(RegisteredServiceConsumer)), out autofacRegistration), "The service type was not registered into the container.");
            Assert.IsInstanceOf(autofacLifetimeType, autofacRegistration.Lifetime, "The registration lifetime type was not correct.");
            Assert.AreEqual(autofacInstanceSharing, autofacRegistration.Sharing, "The registration sharing was not correct.");
        }
 /// <summary>
 /// Converts a TypeRegistrationLifetime to a LifestyleType.
 /// </summary>
 /// <param name="lifetime">The lifetime.</param>
 /// <returns></returns>
 private static LifestyleType ToLifestyle(TypeRegistrationLifetime lifetime)
 {
     if (lifetime == TypeRegistrationLifetime.Singleton)
     {
         return LifestyleType.Singleton;
     }
     else if (lifetime == TypeRegistrationLifetime.Transient)
     {
         return LifestyleType.Transient;
     }
     else
     {
         throw new ArgumentOutOfRangeException("entry.Lifetime",
             "Only Transient and Singleton are supported.");
     }
 }