Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.Collections.Generic;
11 : using System.Linq;
12 : using System.Linq.Expressions;
13 : using MongoDB.Driver.Builders;
14 :
15 : namespace Cqrs.Mongo.Factories
16 : {
17 : /// <summary>
18 : /// A set of extension methods for <see cref="IndexKeysBuilder"/>.
19 : /// </summary>
20 : public static class IndexKeysBuilderExtension
21 1 : {
22 : /// <summary>
23 : /// Define the index as ascending.
24 : /// </summary>
25 1 : public static IndexKeysBuilder Ascending<T>(this IndexKeysBuilder mongoIndexKeys, params Expression<Func<T, object>>[] selectors)
26 : {
27 : var properties = new List<string>();
28 : foreach (Expression<Func<T, object>> selector in selectors)
29 : {
30 : var selectorUnaryExpression = selector.Body as UnaryExpression;
31 : MemberExpression selectorMemberExpression;
32 : if (selectorUnaryExpression != null)
33 : {
34 : selectorMemberExpression = (MemberExpression)selectorUnaryExpression.Operand;
35 : }
36 : else
37 : {
38 : selectorMemberExpression = (MemberExpression) selector.Body;
39 : }
40 : string memberName = CheckForChildProperty(selectorMemberExpression);
41 : properties.Add(memberName);
42 : }
43 : return mongoIndexKeys.Ascending(properties.ToArray());
44 : }
45 :
46 : /// <summary>
47 : /// Define the index as descending.
48 : /// </summary>
49 1 : public static IndexKeysBuilder Descending<T>(this IndexKeysBuilder mongoIndexKeys, params Expression<Func<T, object>>[] selectors)
50 : {
51 : var properties = new List<string>();
52 : foreach (Expression<Func<T, object>> selector in selectors)
53 : {
54 : var selectorUnaryExpression = selector.Body as UnaryExpression;
55 : MemberExpression selectorMemberExpression;
56 : if (selectorUnaryExpression != null)
57 : {
58 : selectorMemberExpression = (MemberExpression)selectorUnaryExpression.Operand;
59 : }
60 : else
61 : {
62 : selectorMemberExpression = (MemberExpression) selector.Body;
63 : }
64 : string memberName = CheckForChildProperty(selectorMemberExpression);
65 : properties.Add(memberName);
66 : }
67 : return mongoIndexKeys.Descending(properties.ToArray());
68 : }
69 :
70 : /// <summary>
71 : /// Checks if the property name is a child and maps it appropriately.
72 : /// </summary>
73 1 : public static string CheckForChildProperty(MemberExpression selectorMemberExpression)
74 : {
75 : string memberName = selectorMemberExpression.Member.Name;
76 : var selectorMethodCallExpression = selectorMemberExpression.Expression as MethodCallExpression;
77 : if (selectorMethodCallExpression != null)
78 : {
79 : var selectorMethodCallExpressionArgument = selectorMethodCallExpression.Arguments.FirstOrDefault() as MemberExpression;
80 : if (selectorMethodCallExpressionArgument != null)
81 : memberName = string.Format("{1}.{0}", memberName, selectorMethodCallExpressionArgument.Member.Name);
82 : }
83 : else
84 : {
85 : if (selectorMemberExpression.Expression.GetType().Name == "PropertyExpression")
86 : {
87 : dynamic propertyExpression = selectorMemberExpression.Expression;
88 : if (propertyExpression.Expression.NodeType == ExpressionType.MemberAccess)
89 : memberName = string.Format("{2}.{1}.{0}", memberName, propertyExpression.Member.Name, propertyExpression.Expression.Member.Name);
90 : else
91 : memberName = string.Format("{1}.{0}", memberName, propertyExpression.Member.Name);
92 : }
93 : }
94 : return memberName;
95 : }
96 : }
97 : }
|