Ejemplo n.º 1
0
        public override IEnumerable <IComponentRegistration> GetComponentRegistrations(ITreeNode registrationRootElement)
        {
            IStructuralMatchResult match = Match(registrationRootElement);

            if (match.Matched)
            {
                var statements = match.GetMatchedElementList("statements").Cast <ICSharpStatement>();

                var collectedTypes = statements.SelectMany(statement =>
                {
                    var returnTypeCollector = new ReturnTypeCollector(new UniversalContext(statement.GetPsiModule()));
                    statement.ProcessThisAndDescendants(returnTypeCollector);
                    return(returnTypeCollector.CollectedTypes);
                });

                foreach (var type in collectedTypes)
                {
                    var declaredType = type as IDeclaredType;
                    if (declaredType != null)
                    {
                        var typeElement = declaredType.GetTypeElement();
                        if (typeElement != null)
                        {
                            yield return(new ElementBasedOnRegistration(registrationRootElement, typeElement));
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public override IEnumerable <IComponentRegistration> GetComponentRegistrations(ITreeNode registrationRootElement)
        {
            IExpressionStatement parentExpression = GetParentExpressionStatemenmt(registrationRootElement);

            if (parentExpression == null)
            {
                yield break;
            }

            IStructuralMatchResult match = Match(registrationRootElement);

            if (match.Matched)
            {
                var arguments = match.GetMatchedElementList("assemblies").Cast <ICSharpArgument>();

                IEnumerable <IModule> modules = arguments.SelectNotNull(argument => ModuleExtractor.GetTargetModule(argument.Value));

                IEnumerable <FilteredRegistrationBase> basedOnRegistrations = basedOnPatterns.SelectMany(
                    basedOnPattern => basedOnPattern.GetBasedOnRegistrations(parentExpression.Expression)).ToList();

                foreach (IModule module in modules)
                {
                    // todo blech, fix this
                    yield return(new CompositeRegistration(registrationRootElement, basedOnRegistrations.Union(
                                                               new ComponentRegistrationBase[]
                    {
                        new DefaultScanAssemblyRegistration(registrationRootElement),
                        new ModuleBasedOnRegistration(registrationRootElement, module)
                    })));
                }
            }
        }
Ejemplo n.º 3
0
        public override IEnumerable <IComponentRegistration> GetComponentRegistrations(ITreeNode registrationRootElement)
        {
            // todo this is duplicated, needs refactoring!
            // This entire thing is one big hack. Need to come back to it one day :)
            // There is (currently) no way to create a pattern that would match the Scan() call with implicit 'this' in ReSharper SSR
            // (i.e. a call to Scan being made withing a method, located in the Registry class)
            // Therefore I'm only matching by the method name only, and later verifying that the method indeed belongs to StructureMap, by
            // making sure the invocation's qualifier derived from global::StructureMap.Configuration.DSL.IRegistry

            if (!registrationRootElement.IsContainerCall(Constants.StructureMapRegistryTypeName))
            {
                yield break;
            }

            IInvocationExpression invocationExpression = registrationRootElement.GetInvocationExpression();

            if (invocationExpression == null)
            {
                yield break;
            }

            IStructuralMatchResult match = Match(invocationExpression);

            if (match.Matched)
            {
                var invocationExpressions = match.GetMatchedElementList("statements")
                                            .OfType <IExpressionStatement>()
                                            .WhereNotNull()
                                            .Select(statement => statement.Expression)
                                            .OfType <IInvocationExpression>()
                                            .ToList();

                var registrations = (from expression in invocationExpressions
                                     from basedOnPattern in basedOnPatterns
                                     from registration in basedOnPattern.GetBasedOnRegistrations(expression)
                                     select registration).ToList();

                if (!registrations.Any())
                {
                    yield break;
                }

                var moduleRegistrations = (from expression in invocationExpressions
                                           from pattern in fromAssemblyPatterns
                                           from registration in pattern.GetComponentRegistrations(expression)
                                           select registration).ToList();

                foreach (var moduleRegistration  in moduleRegistrations)
                {
                    yield return(new CompositeRegistration(registrationRootElement, registrations.Union(new[]
                    {
                        moduleRegistration
                    })));
                }
            }
        }
Ejemplo n.º 4
0
        public override IEnumerable <IComponentRegistration> GetComponentRegistrations(ITreeNode registrationRootElement)
        {
            IStructuralMatchResult match = Match(registrationRootElement);

            if (match.Matched)
            {
                foreach (IBasedOnPattern basedOnPattern in BasedOnPatterns)
                {
                    foreach (BasedOnRegistrationBase basedOnRegistration in basedOnPattern.GetBasedOnRegistrations(registrationRootElement))
                    {
                        IEnumerable <ICSharpArgument> matchedArguments = match.GetMatchedElementList("services").OfType <ICSharpArgument>();
                        IEnumerable <ITypeElement>    typeElements     = matchedArguments.SelectMany(argument => GetRegisteredTypes(match, argument.Value));

                        yield return(new TypesBasedOnRegistration(typeElements, basedOnRegistration));
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public override IEnumerable <IComponentRegistration> GetComponentRegistrations(ITreeNode registrationRootElement)
        {
            IStructuralMatchResult match = Match(registrationRootElement);

            if (match.Matched)
            {
                IEnumerable <IInvocationExpression> invocationExpressions =
                    match.GetMatchedElementList("arguments").Cast <ICSharpArgument>()
                    .Select(argument => argument.Value)
                    .OfType <IInvocationExpression>();

                return(from argumentPattern in argumentsPatterns
                       from expression in invocationExpressions
                       from registration in argumentPattern.GetComponentRegistrations(expression)
                       select registration);
            }

            return(Enumerable.Empty <IComponentRegistration>());
        }
Ejemplo n.º 6
0
        public override IEnumerable <IComponentRegistration> GetComponentRegistrations(ITreeNode registrationRootElement)
        {
            var parentExpression = registrationRootElement.GetParentExpression <IExpressionStatement>();

            if (parentExpression == null)
            {
                yield break;
            }

            IStructuralMatchResult match = Match(registrationRootElement);

            if (match.Matched)
            {
                var statements     = match.GetMatchedElementList("statements").Cast <ICSharpStatement>().ToList();
                var collectedTypes = statements.SelectMany(statement =>
                {
                    var returnTypeCollector = new ReturnTypeCollector(new UniversalContext(statement.GetPsiModule()));
                    statement.ProcessThisAndDescendants(returnTypeCollector);
                    return(returnTypeCollector.CollectedTypes);
                });

                IEnumerable <FilteredRegistrationBase> basedOnRegistrations = basedOnPatterns.SelectMany(
                    basedOnPattern => basedOnPattern.GetBasedOnRegistrations(parentExpression.Expression)).ToList();

                foreach (var type in collectedTypes)
                {
                    var declaredType = type as IDeclaredType;
                    if (declaredType != null)
                    {
                        var typeElement = declaredType.GetTypeElement();
                        if (typeElement != null)
                        {
                            yield return(new CompositeRegistration(registrationRootElement,
                                                                   new[] { new ServiceRegistration(registrationRootElement, typeElement) }
                                                                   .Concat(basedOnRegistrations)));
                        }
                    }
                }
            }
        }
        public override IEnumerable <IComponentRegistration> GetComponentRegistrations(ITreeNode registrationRootElement)
        {
            IStructuralMatchResult match = Match(registrationRootElement);

            if (match.Matched)
            {
                var registrations = (from p in basedOnPatterns
                                     from registration in p.GetBasedOnRegistrations(registrationRootElement)
                                     select registration).ToList();

                if (registrations.Any())
                {
                    foreach (FilteredRegistrationBase basedOnRegistration in registrations)
                    {
                        IEnumerable <ICSharpArgument> matchedArguments = match.GetMatchedElementList("services").OfType <ICSharpArgument>();
                        IEnumerable <ITypeElement>    typeElements     = matchedArguments.SelectMany(argument => argument.Value.GetRegisteredTypes());

                        yield return(new TypesBasedOnRegistration(typeElements, basedOnRegistration));
                    }
                }
            }
        }
Ejemplo n.º 8
0
        protected override INamespace GetNamespaceElement(IStructuralMatchResult match, out bool includeSubnamespaces)
        {
            var arguments = match.GetMatchedElementList("arguments").Cast<ICSharpArgument>().ToArray();

            INamespace namespaceElement = null;
            if (arguments.Length > 0)
            {
                namespaceElement = GetNamespaceDeclaration(arguments[0].Value);
            }

            includeSubnamespaces = false;
            if (arguments.Length == 2)
            {
                ICSharpArgument boolArgument = arguments[1];
                if (boolArgument.Value.ConstantValue != null &&
                    boolArgument.Value.ConstantValue.IsBoolean())
                {
                    includeSubnamespaces = Convert.ToBoolean(boolArgument.Value.ConstantValue.Value);
                }
            }

            return namespaceElement;
        }
Ejemplo n.º 9
0
        protected override IEnumerable<FilteredRegistrationBase> DoCreateRegistrations(ITreeNode registrationRootElement, IStructuralMatchResult match)
        {
            var arguments = match.GetMatchedElementList("arguments").Cast<ICSharpArgument>();

            foreach (var argument in arguments)
            {
                // match typeof() expressions
                var typeOfExpression = argument.Value as ITypeofExpression;
                if (typeOfExpression != null)
                {
                    var argumentType = typeOfExpression.ArgumentType as IDeclaredType;
                    if (argumentType != null)
                    {
                        var typeElement = argumentType.GetTypeElement();
                        if (typeElement == null) // can happen if the typeof() expression is empty
                        {
                            yield break;
                        }

                        yield return new ServiceRegistration(registrationRootElement, typeElement);
                    }
                }
            }
        }
Ejemplo n.º 10
0
        protected override IEnumerable <FilteredRegistrationBase> DoCreateRegistrations(ITreeNode registrationRootElement, IStructuralMatchResult match)
        {
            var arguments = match.GetMatchedElementList("arguments").Cast <ICSharpArgument>();

            foreach (var argument in arguments)
            {
                // match typeof() expressions
                var typeOfExpression = argument.Value as ITypeofExpression;
                if (typeOfExpression != null)
                {
                    var argumentType = typeOfExpression.ArgumentType as IDeclaredType;
                    if (argumentType != null)
                    {
                        var typeElement = argumentType.GetTypeElement();
                        if (typeElement == null) // can happen if the typeof() expression is empty
                        {
                            yield break;
                        }

                        yield return(new ServiceRegistration(registrationRootElement, typeElement));
                    }
                }
            }
        }
        protected override INamespace GetNamespaceElement(IStructuralMatchResult match, out bool includeSubnamespaces)
        {
            var arguments = match.GetMatchedElementList("arguments").Cast<ICSharpArgument>().ToArray();

            return NamespaceExtractor.GetNamespace(arguments, out includeSubnamespaces);
        }
Ejemplo n.º 12
0
        protected override INamespace GetNamespaceElement(IStructuralMatchResult match, out bool includeSubnamespaces)
        {
            var arguments = match.GetMatchedElementList("arguments").Cast <ICSharpArgument>().ToArray();

            return(NamespaceExtractor.GetNamespace(arguments, out includeSubnamespaces));
        }