コード例 #1
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));
 }
コード例 #2
0
        /// <summary>
        /// Adds the specified <paramref name="descriptors"/> 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="descriptors">The <see cref="RegistrationDescriptor"/>s to add.</param>
        public static void TryAdd(
            this IRegistrationCollection collection,
            IEnumerable <RegistrationDescriptor> descriptors)
        {
            if (collection is null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

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

            foreach (var d in descriptors)
            {
                collection.TryAdd(d);
            }
        }