Beispiel #1
0
        /// /// <summary>
        /// Initializes a new instance of the <see cref="DelegateServiceLocator" /> class.
        /// </summary>
        /// <param name="baseLocator">The value for <see cref="DelegateServiceLocator.BaseLocator" /> property.</param>
        /// <param name="multiFallback">The value for <see cref="DelegateServiceLocator.MultiInstanceFallback" /> property.</param>
        /// <param name="singleFallback">The value for <see cref="DelegateServiceLocator.SingleInstanceFallback" /> property.</param>
        /// <param name="syncRoot">The value for the <see cref="ServiceLocatorBase._SYNC_ROOT" /> field.</param>
        public DelegateServiceLocator(IServiceLocator baseLocator = null,
                                      MultiInstanceFallbackProvider multiFallback = null, SingleInstanceFallbackProvider singleFallback = null,
                                      object syncRoot = null)
            : base(syncRoot: syncRoot)
        {
            this.BaseLocator = baseLocator;

            this.MultiInstanceFallback  = multiFallback;
            this.SingleInstanceFallback = singleFallback;
        }
Beispiel #2
0
        // Protected Methods (2) 

        /// <inheriteddoc />
        protected override IEnumerable <object> OnGetAllInstances(Type serviceType, object key)
        {
            IEnumerable <object> result = null;

            InstanceProvider provider;

            if (this._MULTI_PROVIDERS.TryGetValue(serviceType, out provider))
            {
                IEnumerable seq = provider.Invoke <IEnumerable>(this.BaseLocator,
                                                                key);

                if (seq != null)
                {
                    result = CollectionHelper.AsSequence <object>(seq);
                }

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

            bool tryFallback = true;

            if (result == null)
            {
                if (this._BASE_LOCATOR != null)
                {
                    // use base service locator instead

                    tryFallback = false;
                    result      = this._BASE_LOCATOR
                                  .GetAllInstances(serviceType, key);
                }
            }

            if (tryFallback)
            {
                // try by fallback, if defined

                MultiInstanceFallbackProvider fb = this.MultiInstanceFallback;
                if (fb != null)
                {
                    result = fb(this, key, serviceType);
                }
            }

            return(result);
        }