Пример #1
0
        /// <summary>
        /// Registriert ein Delegate zum Erzeugen einer Liste von Dienstinstanzen.
        /// </summary>
        /// <typeparam name="T">Typ des Dienstes.</typeparam>
        /// <param name="provider">Das Delegate, das Registriert werden soll.</param>
        /// <param name="addDefaultSingleProvider">
        /// Zusätzliches Delegate zum Erzeugen einer einzelnen Instanz
        /// hinzufügen oder nicht.
        /// </param>
        /// <returns>Diese Instanz.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="provider" /> ist eine <see langword="null" /> Referenz.
        /// </exception>
        /// <exception cref="DuplicateWaitObjectException">
        /// Ein Delegate wurde bereits registriert.
        /// </exception>
        public DelegateServiceLocatorWrapper RegisterMulti <T>(MultiInstanceProvider <T> provider,
                                                               bool addDefaultSingleProvider)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }

            this.InvokeForFactoryLists(
                (singleList, multiList, args) =>
            {
                var factory = multiList.FirstOrDefault(f => args.ServiceType
                                                       .Equals(f.SERVICE_TYPE));
                if (factory != null)
                {
                    throw new DuplicateWaitObjectException(args.ServiceType
                                                           .FullName);
                }

                multiList.Add(new MultiInstanceFactory <T>(args.InstanceProvider));
            }, new
            {
                InstanceProvider = provider,
                ServiceType      = typeof(T),
            });

            if (addDefaultSingleProvider)
            {
                this.RegisterSingle <T>(MultiToSingle <T>(provider),
                                        false);
            }

            return(this);
        }
Пример #2
0
        private static SingleInstanceProvider <T> MultiToSingle <T>(MultiInstanceProvider <T> multiProvider)
        {
            if (multiProvider != null)
            {
                return(new SingleInstanceProvider <T>(
                           (sender, key) =>
                {
                    if (DBNull.Value.Equals(key))
                    {
                        key = null;
                    }

                    if (key != null)
                    {
                        throw new ArgumentException("key");
                    }

                    var instances = multiProvider(sender);
                    if (instances == null)
                    {
                        throw new ServiceActivationException(typeof(T),
                                                             key);
                    }

                    return instances.Single();
                }));
            }

            return(null);
        }
Пример #3
0
        /// <summary>
        /// Registers a provider for resolving a list of instances of a service.
        /// </summary>
        /// <typeparam name="T">Type of the service.</typeparam>
        /// <param name="provider">The provider to register.</param>
        /// <param name="registerDefaultSingleProvider">
        /// Also register a default single instance provider or not.
        /// </param>
        /// <returns>That instance.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="provider" /> is <see langword="null" />.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// A provider is already registrated for the service type.
        /// </exception>
        public DelegateServiceLocator RegisterMultiProvider <T>(MultiInstanceProvider <T> provider,
                                                                bool registerDefaultSingleProvider)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }

            lock (this._SYNC)
            {
                Type serviceType = typeof(T);

                if (this._MULTI_PROVIDERS.ContainsKey(serviceType))
                {
                    throw new InvalidOperationException();
                }

                this._MULTI_PROVIDERS
                .Add(serviceType,
                     new InstanceProvider(serviceType, provider));
            }

            if (registerDefaultSingleProvider)
            {
                this.RegisterSingleProvider <T>(MultiToSingle <T>(provider),
                                                false);
            }

            return(this);
        }
Пример #4
0
        // Private Methods (2) 

        private static SingleInstanceProvider <T> MultiToSingle <T>(MultiInstanceProvider <T> provider)
        {
            return(new SingleInstanceProvider <T>(delegate(DelegateServiceLocator locator, object key)
            {
                Type serviceType = typeof(T);

                IEnumerable <T> result = provider(locator, key);
                if (result == null)
                {
                    throw new ServiceActivationException(serviceType, key);
                }

                try
                {
                    return CollectionHelper.Single <T>(result);
                }
                catch (Exception ex)
                {
                    throw new ServiceActivationException(serviceType, key, ex);
                }
            }));
        }
Пример #5
0
        private static SingleInstanceProvider <T> MultiToSingle <T>(MultiInstanceProvider <T> provider)
        {
            return((locator, key) =>
            {
                var serviceType = typeof(T);

                var result = provider(locator, key);
                if (result == null)
                {
                    throw new ServiceActivationException(serviceType, key);
                }

                try
                {
                    return result.Single();
                }
                catch (Exception ex)
                {
                    throw new ServiceActivationException(serviceType, key, ex);
                }
            });
        }
Пример #6
0
 /// <summary>
 /// Registriert ein Delegate zum Erzeugen einer Liste von Dienstinstanzen
 /// und fügt zusätzlich ein Standard-Delegate zum Erzeugen einer einzelnen Instanz
 /// hinzu.
 /// </summary>
 /// <typeparam name="T">Typ des Dienstes.</typeparam>
 /// <param name="provider">Das Delegate, das Registriert werden soll.</param>
 /// <returns>Diese Instanz.</returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="provider" /> ist eine <see langword="null" /> Referenz.
 /// </exception>
 /// <exception cref="DuplicateWaitObjectException">
 /// Ein Delegate wurde bereits registriert.
 /// </exception>
 public DelegateServiceLocatorWrapper RegisterMulti <T>(MultiInstanceProvider <T> provider)
 {
     return(this.RegisterMulti <T>(provider,
                                   true));
 }
 internal MultiInstanceFactory(MultiInstanceProvider <T> provider)
     : base(typeof(T),
            provider)
 {
 }