Exemplo n.º 1
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.º 2
0
 /// <summary>
 /// Adds the specified <typeparamref name="TService"/> as a <see cref="Lifecycle.Transient"/> 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 TryAddTransient <TService>(
     this IRegistrationCollection services,
     Func <IServiceProvider, TService> implementationFactory)
     where TService : class
 {
     services.TryAdd(RegistrationDescriptor.Transient(implementationFactory));
 }
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="descriptor"/> to the <paramref name="collection"/>.
        /// </summary>
        /// <param name="collection">The <see cref="IRegistrationCollection"/>.</param>
        /// <param name="descriptor">The <see cref="RegistrationDescriptor"/> to add.</param>
        /// <returns>A reference to the current instance of <see cref="IRegistrationCollection"/>.</returns>
        public static IRegistrationCollection Add(
            this IRegistrationCollection collection,
            RegistrationDescriptor descriptor)
        {
            if (collection is null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

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

            collection.Add(descriptor);
            return(collection);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Adds the specified <paramref name="service"/> as a <see cref="Lifecycle.Transient"/> 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 TryAddTransient(
            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.Transient(service, service);

            TryAdd(collection, descriptor);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Adds the specified <paramref name="descriptor"/> 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="descriptor">The <see cref="RegistrationDescriptor"/> to add.</param>
        public static void TryAdd(
            this IRegistrationCollection collection,
            RegistrationDescriptor descriptor)
        {
            if (collection is null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

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

            if (!collection.Any(d => d.ServiceType == descriptor.ServiceType))
            {
                collection.Add(descriptor);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Adds the specified <paramref name="service"/> as a <see cref="Lifecycle.Transient"/> 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 TryAddTransient(
            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.Transient(service, implementationFactory);

            TryAdd(collection, descriptor);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Adds the specified <paramref name="service"/> as a <see cref="Lifecycle.Singleton"/> service
        /// with the <paramref name="implementationType"/> implementation
        /// 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="implementationType">The implementation type of the service.</param>
        public static void TryAddSingleton(
            this IRegistrationCollection collection,
            Type service,
            Type implementationType)
        {
            if (collection is null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

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

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

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

            TryAdd(collection, descriptor);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Removes the first service in <see cref="IRegistrationCollection"/> with the same service type
        /// as <paramref name="descriptor"/> and adds <paramef name="descriptor"/> to the collection.
        /// </summary>
        /// <param name="collection">The <see cref="IRegistrationCollection"/>.</param>
        /// <param name="descriptor">The <see cref="RegistrationDescriptor"/> to replace with.</param>
        /// <returns></returns>
        public static IRegistrationCollection Replace(
            this IRegistrationCollection collection,
            RegistrationDescriptor descriptor)
        {
            if (collection is null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

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

            var registeredRegistrationDescriptor = collection.FirstOrDefault(s => s.ServiceType == descriptor.ServiceType);

            if (registeredRegistrationDescriptor is object)
            {
                collection.Remove(registeredRegistrationDescriptor);
            }

            collection.Add(descriptor);
            return(collection);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Adds a scoped lifecycle service
 /// </summary>
 /// <param name="registry"></param>
 /// <param name="serviceType"></param>
 /// <param name="implementationType"></param>
 /// <returns></returns>
 public static IRegistrationCollection AddScoped(this IRegistrationCollection registry, Type serviceType, Type implementationType)
 {
     registry.Add(RegistrationDescriptor.Scoped(serviceType, implementationType));
     return(registry);
 }
Exemplo n.º 11
0
 /// <summary>
 /// Adds a transient lifecycle service
 /// </summary>
 /// <typeparam name="TService"></typeparam>
 /// <typeparam name="TImplementation"></typeparam>
 /// <param name="registry"></param>
 /// <returns></returns>
 public static IRegistrationCollection AddTransient <TService, TImplementation>(this IRegistrationCollection registry) where TImplementation : TService
 {
     registry.Add(RegistrationDescriptor.Transient(typeof(TService), typeof(TImplementation)));
     return(registry);
 }