Beispiel #1
0
        /// <summary>
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="includeParent"></param>
        /// <param name="consider"></param>
        /// <returns></returns>
        public static string WhatDoIHave(this IExportLocatorScope scope, bool includeParent = true,
                                         ActivationStrategyFilter consider = null)
        {
            var builder = new StringBuilder();

            var locator = scope.GetInjectionScope();

            builder.AppendLine(new string('-', 80));

            builder.AppendFormat("Exports for scope '{0}' with id {1}{2}",
                                 locator.ScopeName,
                                 locator.ScopeId,
                                 Environment.NewLine);

            foreach (var exportStrategy in locator.StrategyCollectionContainer.GetAllStrategies())
            {
                ProcessExportStrategy(builder, exportStrategy);
            }

            if (includeParent && locator.Parent != null)
            {
                builder.Append(WhatDoIHave(scope.Parent, true, consider));
            }

            return(builder.ToString());
        }
Beispiel #2
0
        /// <summary>
        /// Try to locate an export by type
        /// </summary>
        /// <param name="type">locate type</param>
        /// <param name="value">out value</param>
        /// <param name="extraData">extra data to use during locate</param>
        /// <param name="consider">filter out exports you don't want</param>
        /// <param name="withKey">key to use during locate</param>
        /// <param name="isDynamic">skip cache and look at all exports</param>
        /// <returns>returns tue if export found</returns>
        public bool TryLocate(Type type, out object value, object extraData = null, ActivationStrategyFilter consider = null, object withKey = null, bool isDynamic = false)
        {
            var context = CreateInjectionContextFromExtraData(type, extraData);

            value = InternalLocate(this, this, type, consider, withKey, context, true, isDynamic);

            return(value != null);
        }
Beispiel #3
0
 /// <summary>
 /// Locate all by specific name
 /// </summary>
 /// <param name="name"></param>
 /// <param name="extraData"></param>
 /// <param name="consider"></param>
 /// <returns></returns>
 public List <object> LocateAllByName(string name, object extraData = null, ActivationStrategyFilter consider = null)
 {
     return(((IInjectionScope)this).InternalLocateAllByName(this,
                                                            DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this),
                                                            name,
                                                            extraData,
                                                            consider));
 }
Beispiel #4
0
        public void FluentDecoratorWithCtor_Consider([Freeze] ConstructorParameterInfo constructorParameterInfo,
                                                     FluentDecoratorWithCtorConfiguration <int> configuration)
        {
            ActivationStrategyFilter filter = strategy => true;

            configuration.Consider(filter);

            Assert.Same(filter, constructorParameterInfo.ExportStrategyFilter);
        }
Beispiel #5
0
        /// <summary>
        /// Locate specific type using extra data or key
        /// </summary>
        /// <param name="type">type to locate</param>
        /// <param name="extraData">extra data to be used during construction</param>
        /// <param name="consider"></param>
        /// <param name="withKey">key to use for locating type</param>
        /// <param name="isDynamic"></param>
        /// <returns>located instance</returns>
        // ReSharper disable once MethodOverloadWithOptionalParameter
        public override object Locate(Type type, object extraData = null, ActivationStrategyFilter consider = null, object withKey = null, bool isDynamic = false)
        {
            if (isDynamic || withKey != null || consider != null)
            {
                return(LocateFromParent(type, extraData, consider, withKey, false, isDynamic));
            }

            return(DelegateCache.ExecuteActivationStrategyDelegateWithContext(type, this, false, extraData != null ? CreateContext(extraData) : null));
        }
Beispiel #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="childScope"></param>
        /// <param name="disposalScope"></param>
        /// <param name="name"></param>
        /// <param name="extraData"></param>
        /// <param name="consider"></param>
        /// <param name="allowNull"></param>
        /// <returns></returns>
        object IInjectionScope.LocateByNameFromChildScope(IExportLocatorScope childScope, IDisposalScope disposalScope,
                                                          string name,
                                                          object extraData, ActivationStrategyFilter consider, bool allowNull)
        {
            var collection = StrategyCollectionContainer.GetActivationStrategyCollectionByName(name);

            ICompiledExportStrategy strategy = null;

            if (collection != null)
            {
                if (consider != null)
                {
                    var context = new StaticInjectionContext(typeof(object));

                    strategy =
                        collection.GetStrategies()
                        .FirstOrDefault(
                            s => (!s.HasConditions || s.Conditions.All(con => con.MeetsCondition(s, context))) && consider(s));
                }
                else
                {
                    strategy = collection.GetPrimary();

                    if (strategy == null)
                    {
                        var context = new StaticInjectionContext(typeof(object));

                        strategy = collection.GetStrategies()
                                   .FirstOrDefault(
                            s => !s.HasConditions || s.Conditions.All(con => con.MeetsCondition(s, context)));
                    }
                }
            }

            if (strategy != null)
            {
                var strategyDelegate =
                    strategy.GetActivationStrategyDelegate(this, InternalFieldStorage.ActivationStrategyCompiler, typeof(object));

                return(strategyDelegate(childScope, disposalScope, CreateContext(extraData)));
            }

            if (Parent != null)
            {
                return(((IInjectionScope)Parent).LocateByNameFromChildScope(childScope, disposalScope, name, extraData,
                                                                            consider, allowNull));
            }

            if (!allowNull)
            {
                throw new LocateException(new StaticInjectionContext(typeof(object)));
            }

            return(null);
        }
        /// <summary>
        /// Applies a filter to be used when resolving a parameter constructor
        /// It will be called each time the parameter is resolved
        /// </summary>
        /// <param name="filter">filter delegate to be used when resolving parameter</param>
        /// <returns>configuration object</returns>
        public IFluentWithCtorConfiguration <TParam> Consider(ActivationStrategyFilter filter)
        {
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }

            _constructorParameterInfo.ExportStrategyFilter = filter;

            return(this);
        }
Beispiel #8
0
        /// <summary>
        /// Locate specific type using extra data or key
        /// </summary>
        /// <param name="type">type to locate</param>
        /// <param name="extraData">extra data to be used during construction</param>
        /// <param name="consider">filter out exports you don't want to consider</param>
        /// <param name="withKey">key to use for locating type</param>
        /// <param name="isDynamic">skip cache and look through exports</param>
        /// <returns>located instance</returns>
        // ReSharper disable once MethodOverloadWithOptionalParameter
        public override object Locate(Type type, object extraData = null, ActivationStrategyFilter consider = null, object withKey = null, bool isDynamic = false)
        {
            IInjectionContext context = extraData == null ?
                                        null : CreateInjectionContextFromExtraData(type, extraData);

            if (withKey == null && consider == null && !isDynamic)
            {
                return(DelegateCache.ExecuteActivationStrategyDelegateWithContext(type, this, false, context));
            }

            return(InternalLocate(this, this, type, consider, withKey, context, false, isDynamic));
        }
Beispiel #9
0
        /// <summary>
        /// Locate specific type using extra data or key
        /// </summary>
        /// <param name="type">type to locate</param>
        /// <param name="extraData">extra data to be used during construction</param>
        /// <param name="consider"></param>
        /// <param name="withKey">key to use for locating type</param>
        /// <param name="isDynamic"></param>
        /// <returns>located instance</returns>
        // ReSharper disable once MethodOverloadWithOptionalParameter
        public object Locate(Type type, object extraData = null, ActivationStrategyFilter consider = null, object withKey = null, bool isDynamic = false)
        {
            if (isDynamic || withKey != null || consider != null)
            {
                return(LocateFromParent(type, extraData, consider, withKey, false, isDynamic));
            }

            var hashCode = type.GetHashCode();

            var func = ActivationDelegates[hashCode & ArrayLengthMinusOne].GetValueOrDefault(type, hashCode);

            return(func != null?
                   func(this, this, extraData == null?null : CreateContext(extraData)) :
                       LocateFromParent(type, null, null, null, allowNull: false, isDynamic: false));
        }
Beispiel #10
0
        /// <summary>
        /// Try to locate an export by type
        /// </summary>
        /// <typeparam name="T">locate type</typeparam>
        /// <param name="value">out value</param>
        /// <param name="extraData"></param>
        /// <param name="consider"></param>
        /// <param name="withKey"></param>
        /// <param name="isDynamic"></param>
        /// <returns></returns>
        public override bool TryLocate <T>(out T value, object extraData = null, ActivationStrategyFilter consider = null, object withKey = null, bool isDynamic = false)
        {
            object outValue;

            if (TryLocate(typeof(T), out outValue, extraData, consider, withKey, isDynamic))
            {
                value = (T)outValue;

                return(true);
            }

            value = default(T);

            return(false);
        }
Beispiel #11
0
        /// <summary>
        /// Locate specific type using extra data or key
        /// </summary>
        /// <param name="type">type to locate</param>
        /// <param name="extraData">extra data to be used during construction</param>
        /// <param name="consider">filter out exports you don't want to consider</param>
        /// <param name="withKey">key to use for locating type</param>
        /// <param name="isDynamic">skip cache and look through exports</param>
        /// <returns>located instance</returns>
        // ReSharper disable once MethodOverloadWithOptionalParameter
        public object Locate(Type type, object extraData = null, ActivationStrategyFilter consider = null, object withKey = null, bool isDynamic = false)
        {
            var context = CreateInjectionContextFromExtraData(type, extraData);

            if (withKey == null && consider == null && !isDynamic)
            {
                var hash = type.GetHashCode();

                var func = ActivationDelegates[hash & ArrayLengthMinusOne].GetValueOrDefault(type, hash);

                if (func != null)
                {
                    return(func(this, DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), context));
                }
            }

            return(InternalLocate(this, DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), type, consider, withKey, context, false, isDynamic));
        }
Beispiel #12
0
        /// <summary>
        /// try to locate a specific type
        /// </summary>
        /// <param name="type">type to locate</param>
        /// <param name="value">located value</param>
        /// <param name="extraData">extra data to be used during locate</param>
        /// <param name="consider">filter to use during location</param>
        /// <param name="withKey">key to use during locate</param>
        /// <param name="isDynamic">is the request dynamic</param>
        /// <returns>true if export could be located</returns>
        public bool TryLocate(Type type, out object value, object extraData = null, ActivationStrategyFilter consider = null, object withKey = null, bool isDynamic = false)
        {
            if (!isDynamic && withKey == null && consider == null)
            {
                var hashCode = type.GetHashCode();

                var func = ActivationDelegates[hashCode & ArrayLengthMinusOne].GetValueOrDefault(type, hashCode);

                if (func != null)
                {
                    value = func(this, this, extraData == null ? null : CreateContext(extraData));

                    return(value != null);
                }
            }

            value = LocateFromParent(type, extraData, consider, withKey, true, isDynamic);

            return(value != null);
        }
Beispiel #13
0
        /// <summary>
        /// Try to locate a specific type
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">located value</param>
        /// <param name="extraData">extra data to be used during construction</param>
        /// <param name="consider">filter out exports you don't want</param>
        /// <param name="withKey">key to use while locating</param>
        /// <param name="isDynamic">skip cache and look at all exports</param>
        /// <returns></returns>
        public bool TryLocate <T>(out T value, object extraData = null, ActivationStrategyFilter consider = null, object withKey = null, bool isDynamic = false)
        {
            var context = CreateInjectionContextFromExtraData(typeof(T), extraData);

            var newValue = InternalLocate(this, this, typeof(T), consider, withKey, context, true, isDynamic);

            var returnValue = false;

            if (newValue != null)
            {
                returnValue = true;
                value       = (T)newValue;
            }
            else
            {
                value = default(T);
            }

            return(returnValue);
        }
Beispiel #14
0
        private object InternalLocate(IExportLocatorScope scope, IDisposalScope disposalScope, Type type, ActivationStrategyFilter consider, object key, IInjectionContext injectionContext, bool allowNull, bool isDynamic)
        {
            if (type == typeof(ILocatorService) || type == typeof(IExportLocatorScope))
            {
                return(scope);
            }

            if (isDynamic)
            {
                if (type.IsArray)
                {
                    return(DynamicArray(scope, disposalScope, type, consider, injectionContext));
                }

                if (type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                {
                    return(DynamicIEnumerable(scope, disposalScope, type, consider, injectionContext));
                }
            }

            var compiledDelegate = InternalFieldStorage.ActivationStrategyCompiler.FindDelegate(this, type, consider, key, injectionContext, InternalFieldStorage.MissingExportStrategyProviders != ImmutableLinkedList <IMissingExportStrategyProvider> .Empty);

            if (compiledDelegate != null)
            {
                if (key == null && consider == null)
                {
                    compiledDelegate = AddObjectFactory(type, compiledDelegate);
                }

                return(compiledDelegate(scope, disposalScope ?? (DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(scope)), injectionContext));
            }

            if (Parent != null)
            {
                var injectionScopeParent = (IInjectionScope)Parent;

                return(injectionScopeParent.LocateFromChildScope(this, disposalScope, type, injectionContext, consider, key, allowNull, isDynamic));
            }

            if (!allowNull)
            {
                throw new LocateException(new StaticInjectionContext(type));
            }

            return(null);
        }
Beispiel #15
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="disposalScope"></param>
        /// <param name="exportName"></param>
        /// <param name="extraData"></param>
        /// <param name="consider"></param>
        /// <returns></returns>
        List <object> IInjectionScope.InternalLocateAllByName(IExportLocatorScope scope, IDisposalScope disposalScope, string exportName, object extraData, ActivationStrategyFilter consider)
        {
            var context = CreateContext(extraData);

            var returnList = new List <object>();

            var collection = StrategyCollectionContainer.GetActivationStrategyCollectionByName(exportName);

            foreach (var strategy in collection.GetStrategies())
            {
                if (consider == null || consider(strategy))
                {
                    var activation = strategy.GetActivationStrategyDelegate(this,
                                                                            InternalFieldStorage.ActivationStrategyCompiler, typeof(object));

                    returnList.Add(activation(scope, disposalScope, context.Clone()));
                }
            }

            var injectionScopeParent = Parent as IInjectionScope;

            if (injectionScopeParent != null)
            {
                returnList.AddRange(injectionScopeParent.InternalLocateAllByName(scope, disposalScope, exportName, context, consider));
            }

            return(returnList);
        }
Beispiel #16
0
        /// <summary>
        /// Internal locate all method
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="scope"></param>
        /// <param name="disposalScope"></param>
        /// <param name="type"></param>
        /// <param name="extraData"></param>
        /// <param name="consider"></param>
        /// <param name="comparer"></param>
        /// <returns></returns>
        List <T> IInjectionScope.InternalLocateAll <T>(IExportLocatorScope scope, IDisposalScope disposalScope, Type type, object extraData, ActivationStrategyFilter consider, IComparer <T> comparer)
        {
            var returnList = new List <T>();

            var context = CreateInjectionContextFromExtraData(typeof(T), extraData);

            var collection = StrategyCollectionContainer.GetActivationStrategyCollection(type);

            if (collection != null)
            {
                LocateEnumerablesFromStrategyCollection(collection, scope, disposalScope, type, context, consider, returnList);
            }

            if (type.IsConstructedGenericType)
            {
                var genericType = type.GetGenericTypeDefinition();

                collection = StrategyCollectionContainer.GetActivationStrategyCollection(genericType);

                if (collection != null)
                {
                    LocateEnumerablesFromStrategyCollection(collection, scope, disposalScope, type, context, consider, returnList);
                }
            }

            var injectionParent = Parent as IInjectionScope;

            if (injectionParent != null)
            {
                returnList.AddRange(injectionParent.InternalLocateAll <T>(scope, disposalScope, type, context, consider, null));
            }

            if (comparer != null)
            {
                returnList.Sort(comparer);
            }

            return(returnList);
        }
Beispiel #17
0
 /// <summary>
 /// Locate an export from a child scope
 /// </summary>
 /// <param name="childScope">scope where the locate originated</param>
 /// <param name="disposalScope"></param>
 /// <param name="type">type to locate</param>
 /// <param name="extraData"></param>
 /// <param name="consider"></param>
 /// <param name="key"></param>
 /// <param name="allowNull"></param>
 /// <param name="isDynamic"></param>
 /// <returns>configuration object</returns>
 object IInjectionScope.LocateFromChildScope(IExportLocatorScope childScope, IDisposalScope disposalScope, Type type, object extraData, ActivationStrategyFilter consider, object key, bool allowNull, bool isDynamic)
 {
     return(InternalLocate(childScope, disposalScope, type, consider, key, CreateInjectionContextFromExtraData(type, extraData), allowNull, isDynamic));
 }
Beispiel #18
0
        /// <summary>
        /// Try to locate by name
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="extraData"></param>
        /// <param name="consider"></param>
        /// <returns></returns>
        public bool TryLocateByName(string name, out object value, object extraData = null, ActivationStrategyFilter consider = null)
        {
            value = ((IInjectionScope)this).LocateByNameFromChildScope(this,
                                                                       DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), name, extraData, consider, true);

            return(value != null);
        }
Beispiel #19
0
 public List <T> LocateAll <T>(Type type = null, object extraData = null, ActivationStrategyFilter consider = null,
                               IComparer <T> comparer = null)
 {
     throw new NotImplementedException();
 }
Beispiel #20
0
 /// <summary>
 /// Locate by name
 /// </summary>
 /// <param name="name"></param>
 /// <param name="extraData"></param>
 /// <param name="consider"></param>
 /// <returns></returns>
 public object LocateByName(string name, object extraData = null, ActivationStrategyFilter consider = null)
 {
     return(((IInjectionScope)this).LocateByNameFromChildScope(this,
                                                               DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), name, extraData, consider, false));
 }
Beispiel #21
0
 public bool TryLocate(Type type, out object value, object extraData = null, ActivationStrategyFilter consider = null,
                       object withKey = null, bool isDynamic = false)
 {
     throw new NotImplementedException();
 }
Beispiel #22
0
 public List <object> LocateAllByName(string name, object extraData = null, ActivationStrategyFilter consider = null)
 {
     throw new NotImplementedException();
 }
Beispiel #23
0
 public bool TryLocateByName(string name, out object value, object extraData = null, ActivationStrategyFilter consider = null)
 {
     throw new NotImplementedException();
 }
Beispiel #24
0
        private object DynamicArray(IExportLocatorScope scope, IDisposalScope disposalScope, Type type, ActivationStrategyFilter consider, IInjectionContext injectionContext)
        {
            if (InternalFieldStorage.DynamicArrayLocator == null)
            {
                Interlocked.CompareExchange(ref InternalFieldStorage.DynamicArrayLocator,
                                            ScopeConfiguration.Implementation.Locate <IDynamicArrayLocator>(), null);
            }

            return(InternalFieldStorage.DynamicArrayLocator.Locate(this, scope, disposalScope, type, consider, injectionContext));
        }
Beispiel #25
0
 /// <summary>
 /// Can Locator type
 /// </summary>
 /// <param name="type">type to locate</param>
 /// <param name="consider"></param>
 /// <param name="key">key to use while locating</param>
 /// <returns></returns>
 public bool CanLocate(Type type, ActivationStrategyFilter consider = null, object key = null)
 {
     return(InternalFieldStorage.CanLocateTypeService.CanLocate(this, type, consider, key));
 }
Beispiel #26
0
 public bool CanLocate(Type type, ActivationStrategyFilter consider = null, object key = null)
 {
     throw new NotImplementedException();
 }
Beispiel #27
0
        private void LocateEnumerablesFromStrategyCollection <TStrategy, TValue>(IActivationStrategyCollection <TStrategy> collection, IExportLocatorScope scope, IDisposalScope disposalScope, Type type, IInjectionContext context, ActivationStrategyFilter filter, List <TValue> returnList) where TStrategy : IWrapperOrExportActivationStrategy
        {
            foreach (var strategy in collection.GetStrategies())
            {
                if (strategy.HasConditions)
                {
                    var pass = true;

                    foreach (var condition in strategy.Conditions)
                    {
                        if (!condition.MeetsCondition(strategy, new StaticInjectionContext(type)))
                        {
                            pass = false;
                            break;
                        }
                    }

                    if (!pass)
                    {
                        continue;
                    }
                }

                if (filter != null && !filter(strategy))
                {
                    continue;
                }

                var activationDelegate = strategy.GetActivationStrategyDelegate(this, InternalFieldStorage.ActivationStrategyCompiler, type);

                if (activationDelegate != null)
                {
                    returnList.Add(
                        (TValue)activationDelegate(scope, disposalScope, context?.Clone()));
                }
            }
        }
Beispiel #28
0
 /// <summary>
 /// Locate specific type using extra data or key
 /// </summary>
 /// <typeparam name="T">type to locate</typeparam>
 /// <param name="extraData">extra data</param>
 /// <param name="consider">filter out exports you don't want to consider</param>
 /// <param name="withKey">key to use during construction</param>
 /// <param name="isDynamic">skip cache and look at all strategies</param>
 /// <returns>located instance</returns>
 // ReSharper disable once MethodOverloadWithOptionalParameter
 public T Locate <T>(object extraData = null, ActivationStrategyFilter consider = null, object withKey = null, bool isDynamic = false)
 {
     return((T)Locate(typeof(T), extraData, consider, withKey, isDynamic));
 }
Beispiel #29
0
        /// <summary>
        /// Applies a filter to be used when resolving a parameter constructor
        /// It will be called each time the parameter is resolved
        /// </summary>
        /// <param name="filter">filter delegate to be used when resolving parameter</param>
        /// <returns>configuration object</returns>
        public IFluentWithCtorConfiguration Consider(ActivationStrategyFilter filter)
        {
            _constructorParameterInfo.ExportStrategyFilter = filter;

            return(this);
        }
Beispiel #30
0
 /// <summary>
 /// Locate all of a specific type
 /// </summary>
 /// <typeparam name="T">type to locate</typeparam>
 /// <param name="type">type to locate</param>
 /// <param name="extraData">extra data to use during construction</param>
 /// <param name="consider">provide method to filter out exports</param>
 /// <param name="comparer">comparer to use for sorting</param>
 /// <returns>list of all located</returns>
 public List <T> LocateAll <T>(Type type = null, object extraData = null, ActivationStrategyFilter consider = null, IComparer <T> comparer = null)
 {
     return(((IInjectionScope)this).InternalLocateAll(this, DisposalScope ?? DisposalScopeProvider.ProvideDisposalScope(this), type ?? typeof(T), extraData, consider, comparer));
 }