Ejemplo n.º 1
0
        private static string GetPath(ClassMappingDescriptor descriptor, MemberMappingDescriptor member)
        {
            // todo: optimize - don't do this for every member, this is also done in AggregateFunctionContext ctor
            IList <AggregateExpressionPathItem> pathItems = ExpressionParser.BuildAggregatePathItem(descriptor, member);
            bool isLastItemCollection = pathItems[pathItems.Count - 1].IsCollection;

            string path = member.Expression;
            int    pos  = path.IndexOf(':');

            if (pos >= 0)
            {
                path = path.Substring(pos + 1);
            }

            if (!isLastItemCollection)
            {
                pos = path.LastIndexOf('/');
                if (pos >= 0)
                {
                    path = path.Substring(0, pos);
                }
            }

            return(path);
        }
        private IEnumerable <CodeStatement> GetPathTraversalStatements()
        {
            string exp = "";
            IList <AggregateExpressionPathItem> pathItems
                = ExpressionParser.BuildAggregatePathItem(m_contexts[0].Descriptor, m_contexts[0].Member);

            foreach (AggregateExpressionPathItem pathItem in pathItems)
            {
                if (pathItem.IsCollection)
                {
                    exp += string.Format("foreach({0} {1} in {2}.{3})",
                                         TypeHelper.GetTypeDefinition(pathItem.Type),
                                         pathItem.Object,
                                         pathItem.Target,
                                         pathItem.Expression);
                }
            }

            exp += Environment.NewLine + "\t\t\t\t{" + Environment.NewLine;
            foreach (AggregateFunctionContext context in m_contexts)
            {
                IEnumerable <string> itemExpressions = context.Generator.GetIterationStatements(context, context.PathItems);
                foreach (string itemExpression in itemExpressions)
                {
                    exp += "\t\t\t\t\t";
                    exp += itemExpression;
                    if (!exp.EndsWith(";"))
                    {
                        exp += ";";
                    }
                    exp += Environment.NewLine;                     // todo: smarter way
                }
            }
            exp += "\t\t\t\t}";

            CodeConditionStatement ifStatement = new CodeConditionStatement(
                new CodeSnippetExpression(pathItems[0].Target + "." + pathItems[0].Expression + " != null"),
                new CodeStatement[] { new CodeExpressionStatement(new CodeSnippetExpression(exp)) },
                new CodeStatement[0]);


            CodeStatementCollection statements = new CodeStatementCollection();

            statements.Add(ifStatement);

            CodeStatement[] arr = new CodeStatement[statements.Count];
            statements.CopyTo(arr, 0);
            return(arr);
        }
        public AggregateFunctionContext(MemberMappingDescriptor member,
                                        ClassMappingDescriptor descriptor,
                                        Type implementationType,
                                        string functionObjectName,
                                        IAggregateFunctionCodeGenerator generator)
        {
            m_member             = member;
            m_generator          = generator;
            m_functionObjectName = functionObjectName;
            m_implementationType = implementationType;
            m_descriptor         = descriptor;

            m_pathItems  = ExpressionParser.BuildAggregatePathItem(descriptor, member);
            m_sourceItem = m_pathItems[m_pathItems.Count - 1];
        }