/// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="activationType"></param>
 /// <param name="injectionScope"></param>
 /// <param name="dependentStrategy"></param>
 /// <param name="propertyOrFieldName"></param>
 public ExportedPropertyOrFieldStrategy(Type activationType, IInjectionScope injectionScope,
                                        ICompiledExportStrategy dependentStrategy, string propertyOrFieldName)
     : base(activationType, injectionScope)
 {
     _dependentStrategy   = dependentStrategy;
     _propertyOrFieldName = propertyOrFieldName;
 }
        public void FluentExportStrategyConfigurationGeneric_WithCtorParam_Two_Arg_Null(
            FluentExportStrategyConfiguration <BasicService> configuration, ICompiledExportStrategy strategy)
        {
            Func <MultipleService1, MultipleService2, IDependentService <IMultipleService> > func = null;

            Assert.Throws <ArgumentNullException>(() => configuration.WithCtorParam(func));
        }
        /// <summary>
        /// Decorate an export strategy with decorators
        /// </summary>
        /// <param name="scope">scope</param>
        /// <param name="request">request</param>
        /// <param name="strategy">strategy being decorated</param>
        /// <returns></returns>
        public IActivationExpressionResult DecorateExportStrategy(IInjectionScope scope, IActivationExpressionRequest request,
                                                                  ICompiledExportStrategy strategy)
        {
            var decorators = FindDecoratorsForStrategy(scope, request, strategy);

            return(decorators.Count != 0 ? CreateDecoratedActivationStrategy(scope, request, strategy, decorators) : null);
        }
Esempio n. 4
0
        private static void ProcessExportStrategy(StringBuilder builder, ICompiledExportStrategy exportStrategy)
        {
            builder.AppendLine(new string('-', 80));

            builder.AppendLine("Export Type: " + exportStrategy.ActivationType.FullName);

            foreach (var exportType in exportStrategy.ExportAs)
            {
                builder.AppendLine("As Type: " + exportType.FullName);
            }

            foreach (var valuePair in exportStrategy.ExportAsKeyed)
            {
                builder.AppendLine($"As Keyed Type: {valuePair.Value} {valuePair.Key.FullName}");
            }

            builder.AppendLine("Priority: " + exportStrategy.Priority);

            builder.AppendLine("Externally Owned: " + exportStrategy.ExternallyOwned);

            if (exportStrategy.Lifestyle != null)
            {
                builder.AppendLine("Lifestyle: " + exportStrategy.Lifestyle.GetType().Name);
            }
            else
            {
                builder.AppendLine("Lifestyle: Transient");
            }

            builder.AppendLine("Depends On");

            var hasDependency = false;

            foreach (var exportStrategyDependency in exportStrategy.GetDependencies())
            {
                builder.AppendLine("\tDependency Type: " + exportStrategyDependency.DependencyType);

                builder.AppendLine("\tMember Name: " + exportStrategyDependency.MemberName);

                builder.AppendLine("\tImport Type: " + exportStrategyDependency.TypeBeingImported.FullName);

                builder.AppendLine("\tHas Filter: " + exportStrategyDependency.HasFilter);

                builder.AppendLine("\tHas Value Provider: " + exportStrategyDependency.HasValueProvider);

                builder.AppendLine("\tIs Satisfied: " + exportStrategyDependency.IsSatisfied);

                builder.AppendLine();

                hasDependency = true;
            }

            if (!hasDependency)
            {
                builder.AppendLine("\tNone");
            }
        }
        public void FluentExportStrategyConfigurationGeneric_WithCtorParam_Two_Arg(
            FluentExportStrategyConfiguration <BasicService> configuration, ICompiledExportStrategy strategy)
        {
            Func <MultipleService1, MultipleService2, IDependentService <IMultipleService> > func = (service, service2) => null;

            configuration.WithCtorParam(func);

            strategy.Received().ConstructorParameter(NSubstitute.Arg.Is <ConstructorParameterInfo>(info => info.ExportFunc == func));
        }
Esempio n. 6
0
        /// <summary>
        /// Add a secondary strategy for this export strategy
        /// </summary>
        /// <param name="secondaryStrategy">new secondary strategy</param>
        public void AddSecondaryStrategy(ICompiledExportStrategy secondaryStrategy)
        {
            if (secondaryStrategy == null)
            {
                throw new ArgumentNullException(nameof(secondaryStrategy));
            }

            _secondaryStrategies = _secondaryStrategies.Add(secondaryStrategy);
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        /// <summary>
        /// Test if a type is exported
        /// </summary>
        /// <param name="type"></param>
        /// <param name="key"></param>
        /// <param name="excludeStrategy"></param>
        /// <returns></returns>
        public bool IsExported(Type type, object key = null, ICompiledExportStrategy excludeStrategy = null)
        {
            ProcessCurrentProvider();

            if (key != null)
            {
                if (_exportStrategyProviders.Any(s => s.ExportAsKeyed.Any(kvp =>
                {
                    return(type == kvp.Key && key.Equals(kvp.Value) && !ReferenceEquals(excludeStrategy, s));
                })))
                {
                    return(true);
                }
            }
            else
            {
                if (_exportStrategyProviders.Any(s => !ReferenceEquals(s, excludeStrategy) && s.ExportAs.Contains(type)))
                {
                    return(true);
                }
            }

            var locateService = OwningScope.ScopeConfiguration.Implementation.Locate <ICanLocateTypeService>();

            var currentScope = OwningScope;

            while (currentScope != null)
            {
                if (locateService.CanLocate(currentScope, type, null, key, false))
                {
                    return(true);
                }

                currentScope = currentScope.Parent as IInjectionScope;
            }

            return(false);
        }
        /// <summary>
        /// Find decorators for a given type
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="request"></param>
        /// <param name="type"></param>
        /// <param name="strategy"></param>
        /// <returns></returns>
        protected virtual List <ICompiledDecoratorStrategy> FindDecoratorsForType(IInjectionScope scope, IActivationExpressionRequest request, Type type, ICompiledExportStrategy strategy)
        {
            List <ICompiledDecoratorStrategy> decorators             = new List <ICompiledDecoratorStrategy>();
            StaticInjectionContext            staticInjectionContext = null;
            var collection =
                scope.DecoratorCollectionContainer.GetActivationStrategyCollection(type);

            if (collection != null)
            {
                foreach (var decorator in collection.GetStrategies())
                {
                    if (decorator.HasConditions)
                    {
                        if (staticInjectionContext == null)
                        {
                            staticInjectionContext = request.GetStaticInjectionContext();
                        }

                        if (!decorator.Conditions.All(
                                condition => condition.MeetsCondition(strategy, staticInjectionContext)))
                        {
                            continue;
                        }
                    }

                    decorators.Add(decorator);
                }
            }

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

                collection = scope.DecoratorCollectionContainer.GetActivationStrategyCollection(generic);

                if (collection != null)
                {
                    foreach (var decorator in collection.GetStrategies())
                    {
                        if (decorator.HasConditions)
                        {
                            if (staticInjectionContext == null)
                            {
                                staticInjectionContext = request.GetStaticInjectionContext();
                            }

                            if (!decorator.Conditions.All(
                                    condition => condition.MeetsCondition(strategy, staticInjectionContext)))
                            {
                                continue;
                            }
                        }

                        decorators.Add(decorator);
                    }
                }
            }

            return(decorators);
        }
        private void ProcessAttributes(Type type, ICompiledExportStrategy strategy)
        {
            foreach (var customAttribute in type.GetTypeInfo().GetCustomAttributes())
            {
                var lifestyleAttribute = customAttribute as ILifestyleProviderAttribute;

                if (lifestyleAttribute != null)
                {
                    strategy.Lifestyle = lifestyleAttribute.ProvideLifestyle(type);
                }

                var condition = (customAttribute as IExportConditionAttribute)?.ProvideCondition(type);

                if (condition != null)
                {
                    strategy.AddCondition(condition);
                }

                var metadata = (customAttribute as IExportMetadataAttribute)?.ProvideMetadata(type);

                if (metadata != null)
                {
                    foreach (var keyValuePair in metadata)
                    {
                        strategy.SetMetadata(keyValuePair.Key, keyValuePair.Value);
                    }
                }
            }

            foreach (var property in type.GetRuntimeProperties())
            {
                foreach (var attribute in property.GetCustomAttributes())
                {
                    var importAttribute = attribute as IImportAttribute;

                    if (importAttribute != null)
                    {
                        var injecitonInfo = importAttribute.ProvideImportInfo(property.PropertyType, property.Name);

                        if (injecitonInfo != null)
                        {
                            strategy.MemberInjectionSelector(new KnownMemberInjectionSelector(
                                                                 new MemberInjectionInfo
                            {
                                MemberInfo = property,
                                IsRequired = injecitonInfo.IsRequired,
                                LocateKey  = injecitonInfo.ImportKey
                            }));
                        }
                    }
                }
            }

            foreach (var method in type.GetRuntimeMethods())
            {
                foreach (var attribute in method.GetCustomAttributes())
                {
                    var importAttribute = attribute as IImportAttribute;

                    var injectionInfo = importAttribute?.ProvideImportInfo(null, method.Name);

                    if (injectionInfo != null)
                    {
                        strategy.MethodInjectionInfo(new MethodInjectionInfo {
                            Method = method
                        });
                    }
                }
            }
        }
Esempio n. 11
0
 /// <summary>
 /// Add a secondary strategy for this export strategy
 /// </summary>
 /// <param name="secondaryStrategy">new secondary strategy</param>
 public void AddSecondaryStrategy(ICompiledExportStrategy secondaryStrategy)
 {
     throw new NotSupportedException("This type of export does not support secondary strategies");
 }
Esempio n. 12
0
 /// <summary>
 /// Add a secondary strategy for this export strategy
 /// </summary>
 /// <param name="secondaryStrategy">new secondary strategy</param>
 public void AddSecondaryStrategy(ICompiledExportStrategy secondaryStrategy)
 {
     _secondaryStrategies = _secondaryStrategies.Add(secondaryStrategy);
 }
Esempio n. 13
0
 /// <summary>
 /// Add a secondary strategy for this export strategy
 /// </summary>
 /// <param name="secondaryStrategy">new secondary strategy</param>
 public void AddSecondaryStrategy(ICompiledExportStrategy secondaryStrategy)
 {
     throw new NotSupportedException("Secondary strategies are not supported on this type");
 }
Esempio n. 14
0
 public void BaseInstanceExportStrategy_AddSecondaryStrategy_Null(ConstantInstanceExportStrategy <int> strategy, ICompiledExportStrategy addStrategy)
 {
     Assert.Throws <ArgumentNullException>(() => strategy.AddSecondaryStrategy(null));
 }
Esempio n. 15
0
        public void BaseInstanceExportStrategy_AddSecondaryStrategy(ConstantInstanceExportStrategy <int> strategy, ICompiledExportStrategy addStrategy)
        {
            strategy.AddSecondaryStrategy(addStrategy);

            var array = strategy.SecondaryStrategies().ToArray();

            Assert.Equal(1, array.Length);
            Assert.Same(addStrategy, array[0]);
        }
 /// <summary>
 /// Add a secondary strategy for this export strategy
 /// </summary>
 /// <param name="secondaryStrategy">new secondary strategy</param>
 public void AddSecondaryStrategy(ICompiledExportStrategy secondaryStrategy)
 {
     throw new NotSupportedException("Secondary strategies not supported on property export");
 }
Esempio n. 17
0
        public void BaseGenericEnumerableStrategy_SecondaryStrategy(LocalBaseGenericEnumerableStrategy strategy, ICompiledExportStrategy addStrategy)
        {
            strategy.AddSecondaryStrategy(addStrategy);

            var strategies = strategy.SecondaryStrategies().ToArray();

            Assert.Equal(1, strategies.Length);
            Assert.Same(addStrategy, strategies[0]);
        }
        /// <summary>
        /// Finds decorators for a strategy
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="request"></param>
        /// <param name="strategy"></param>
        /// <returns></returns>
        protected virtual List <ICompiledDecoratorStrategy> FindDecoratorsForStrategy(IInjectionScope scope, IActivationExpressionRequest request, ICompiledExportStrategy strategy)
        {
            var decorators = FindDecoratorsForType(scope, request, request.ActivationType, strategy);

            if (request.ActivationType != strategy.ActivationType)
            {
                var activationTypeDecorators = FindDecoratorsForType(scope, request, strategy.ActivationType, strategy);

                foreach (var decorator in activationTypeDecorators)
                {
                    if (decorators.Contains(decorator))
                    {
                        continue;
                    }

                    decorators.Add(decorator);
                }
            }

            return(decorators);
        }
 /// <summary>
 /// Gets an activation expression for a given strategy
 /// </summary>
 /// <param name="scope"></param>
 /// <param name="request"></param>
 /// <param name="strategy"></param>
 /// <returns></returns>
 protected virtual IActivationExpressionResult ActivationExpressionForStrategy(IInjectionScope scope, IActivationExpressionRequest request, ICompiledExportStrategy strategy)
 {
     return(strategy.GetActivationExpression(scope, request));
 }
Esempio n. 20
0
        private void AddExportStrategy(ICompiledExportStrategy strategy)
        {
            ProcessCurrentProvider();

            _exportStrategyProviders.Add(strategy);
        }
        /// <summary>
        /// Creates decorated expression for activation strategy
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="request"></param>
        /// <param name="strategy"></param>
        /// <param name="decorators"></param>
        /// <returns></returns>
        protected virtual IActivationExpressionResult CreateDecoratedActivationStrategy(IInjectionScope scope, IActivationExpressionRequest request, ICompiledExportStrategy strategy, List <ICompiledDecoratorStrategy> decorators)
        {
            decorators.Sort((x, y) => Comparer <int> .Default.Compare(x.Priority, y.Priority));

            var pathNodes = ImmutableLinkedList <IActivationPathNode> .Empty;

            if (decorators.All(d => d.ApplyAfterLifestyle))
            {
                pathNodes = pathNodes.Add(new DecoratorActivationPathNode(strategy, request.ActivationType, strategy.Lifestyle));

                foreach (var decorator in decorators)
                {
                    pathNodes = pathNodes.Add(new DecoratorActivationPathNode(decorator, request.ActivationType, null));
                }
            }
            else
            {
                pathNodes = pathNodes.Add(new DecoratorActivationPathNode(strategy, request.ActivationType, null));

                DecoratorActivationPathNode currentNode = null;

                foreach (var decorator in decorators.Where(d => !d.ApplyAfterLifestyle))
                {
                    currentNode = new DecoratorActivationPathNode(decorator, request.ActivationType, null);

                    pathNodes = pathNodes.Add(currentNode);
                }

                if (currentNode != null)
                {
                    currentNode.Lifestyle = strategy.Lifestyle;
                }

                foreach (var decorator in decorators.Where(d => d.ApplyAfterLifestyle))
                {
                    pathNodes = pathNodes.Add(new DecoratorActivationPathNode(decorator, request.ActivationType, null));
                }
            }

            request.SetDecoratorPath(pathNodes);

            var pathNode = request.PopDecoratorPathNode();

            return(pathNode.GetActivationExpression(scope, request));
        }
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="strategy">strategy to wrap</param>
 /// <param name="exportStrategy">export strategy for member</param>
 public FluentExportMemberConfiguration(IFluentExportStrategyConfiguration <T> strategy, ICompiledExportStrategy exportStrategy) : base(strategy)
 {
     _exportStrategy = exportStrategy;
 }
Esempio n. 23
0
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="activationType"></param>
 /// <param name="injectionScope"></param>
 /// <param name="methodInfo"></param>
 /// <param name="dependentStrategy"></param>
 public ExportMethodStrategy(Type activationType, IInjectionScope injectionScope, ICompiledExportStrategy dependentStrategy, MethodInfo methodInfo) : base(activationType, injectionScope)
 {
     _methodInfo        = methodInfo;
     _dependentStrategy = dependentStrategy;
 }