示例#1
0
        public SortSpecification <TAggregateRoot> Parse(string sorter)
        {
            var sortSpecification = new SortSpecification <TAggregateRoot>();

            if (!string.IsNullOrWhiteSpace(sorter))
            {
                sorter = sorter.Trim();
                var sorts = sorter.Split(',');
                foreach (var sort in sorts)
                {
                    var tokens = sort.Trim().Split(' ');
                    if (tokens.Length == 1)
                    {
                        sortSpecification.Add(tokens[0].Trim(), SortDirection.Asc);
                    }
                    else if (tokens.Length == 2)
                    {
                        sortSpecification.Add(tokens[0].Trim(), ParseDirection(tokens[1]));
                    }
                    else
                    {
                        throw new Exception.SorterParseException();
                    }
                }
            }

            return(sortSpecification);
        }
        private void ParseNode <TKey, TAggregateRoot>(ParseTreeNode node, ref SortSpecification <TKey, TAggregateRoot> sortSpecification)
            where TKey : IEquatable <TKey>
            where TAggregateRoot : class, IAggregateRoot <TKey>
        {
            switch (node.Term.Name)
            {
            case "sort-specification":
                var suggestedPropertyName = node.ChildNodes[0].ChildNodes[0].Token.ValueString;
                var suggestedSortingOrder = node.ChildNodes[1].ChildNodes[0].Token.ValueString;
                var propertyName          = ParsingUtils.InferProperty <TAggregateRoot>(suggestedPropertyName)?.Name;
                if (string.IsNullOrEmpty(propertyName))
                {
                    throw new ParsingException("Expression parsed failed.", new[] { $"The property that has the name similar to {suggestedPropertyName} does not exist." });
                }

                var sortOrder = suggestedSortingOrder.ToUpper() == "A" ? SortOrder.Ascending : SortOrder.Descending;
                sortSpecification.Add(propertyName, sortOrder);
                break;

            case "concatenated-sort-specification":
                var leftNode  = node.ChildNodes[0];
                var rightNode = node.ChildNodes[2];
                ParseNode(leftNode, ref sortSpecification);
                ParseNode(rightNode, ref sortSpecification);
                break;
            }
        }