Exemplo n.º 1
0
 /// <summary>
 /// Adds the specified <typeparamref name="TService"/> as a <see cref="Lifecycle.Singleton"/> service
 /// using the factory specified in <paramref name="implementationFactory"/>
 /// to the <paramref name="services"/> if the service type hasn't already been registered.
 /// </summary>
 /// <typeparam name="TService">The type of the service to add.</typeparam>
 /// <param name="services">The <see cref="IRegistrationCollection"/>.</param>
 /// <param name="implementationFactory">The factory that creates the service.</param>
 public static void TryAddSingleton <TService>(
     this IRegistrationCollection services,
     Func <IServiceProvider, TService> implementationFactory)
     where TService : class
 {
     services.TryAdd(RegistrationDescriptor.Singleton(implementationFactory));
 }
Exemplo n.º 2
0
        /// <summary>
        /// Adds an instance
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="registry"></param>
        /// <param name="instance"></param>
        /// <returns></returns>
        public static IRegistrationCollection AddInstance <TService>(this IRegistrationCollection registry, TService instance)
        {
            var descriptor = RegistrationDescriptor.Singleton(typeof(TService), instance);

            registry.Add(descriptor);
            return(registry);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds the specified <typeparamref name="TService"/> as a <see cref="Lifecycle.Singleton"/> service
        /// with an instance specified in <paramref name="instance"/>
        /// to the <paramref name="collection"/> if the service type hasn't already been registered.
        /// </summary>
        /// <typeparam name="TService">The type of the service to add.</typeparam>
        /// <param name="collection">The <see cref="IRegistrationCollection"/>.</param>
        /// <param name="instance">The instance of the service to add.</param>
        public static void TryAddSingleton <TService>(this IRegistrationCollection collection, TService instance)
            where TService : class
        {
            if (collection is null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            if (instance is null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            var descriptor = RegistrationDescriptor.Singleton(typeof(TService), instance);

            TryAdd(collection, descriptor);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adds the specified <paramref name="service"/> as a <see cref="Lifecycle.Singleton"/> service
        /// to the <paramref name="collection"/> if the service type hasn't already been registered.
        /// </summary>
        /// <param name="collection">The <see cref="IRegistrationCollection"/>.</param>
        /// <param name="service">The type of the service to register.</param>
        public static void TryAddSingleton(
            this IRegistrationCollection collection,
            Type service)
        {
            if (collection is null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            if (service is null)
            {
                throw new ArgumentNullException(nameof(service));
            }

            var descriptor = RegistrationDescriptor.Singleton(service, service);

            TryAdd(collection, descriptor);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Adds the specified <paramref name="service"/> as a <see cref="Lifecycle.Singleton"/> service
        /// using the factory specified in <paramref name="implementationFactory"/>
        /// to the <paramref name="collection"/> if the service type hasn't already been registered.
        /// </summary>
        /// <param name="collection">The <see cref="IRegistrationCollection"/>.</param>
        /// <param name="service">The type of the service to register.</param>
        /// <param name="implementationFactory">The factory that creates the service.</param>
        public static void TryAddSingleton(
            this IRegistrationCollection collection,
            Type service,
            Func <IServiceProvider, object> implementationFactory)
        {
            if (collection is null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            if (service is null)
            {
                throw new ArgumentNullException(nameof(service));
            }

            if (implementationFactory is null)
            {
                throw new ArgumentNullException(nameof(implementationFactory));
            }

            var descriptor = RegistrationDescriptor.Singleton(service, implementationFactory);

            TryAdd(collection, descriptor);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Adds a singleton lifecycle service
 /// </summary>
 /// <typeparam name="TService"></typeparam>
 /// <typeparam name="TImplementation"></typeparam>
 /// <param name="registry"></param>
 /// <returns></returns>
 public static IRegistrationCollection AddSingleton <TService, TImplementation>(this IRegistrationCollection registry) where TImplementation : TService
 {
     registry.Add(RegistrationDescriptor.Singleton(typeof(TService), typeof(TImplementation)));
     return(registry);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Adds a singleton lifecycle service
 /// </summary>
 /// <param name="registry"></param>
 /// <param name="serviceType"></param>
 /// <param name="implementationType"></param>
 /// <returns></returns>
 public static IRegistrationCollection AddSingleton(this IRegistrationCollection registry, Type serviceType, Type implementationType)
 {
     registry.Add(RegistrationDescriptor.Singleton(serviceType, implementationType));
     return(registry);
 }