Beispiel #1
0
        private int RegisterSamplingMethods()
        {
            int counter = 0;

            foreach (var aggregationMethodAssembly in AggregationMethodsAssemblies)
            {
                var samplingMethodsTypes =
                    aggregationMethodAssembly.DefinedTypes.Where(
                        t => t.GetCustomAttributes(typeof(SamplingMethodAttribute), true).Any());


                foreach (var samplingMethodType in samplingMethodsTypes)
                {
                    if (!typeof(SamplingImplementationBase).IsAssignableFrom(samplingMethodType))
                    {
                        throw new ArgumentException("The decorated type does not derive from SamplingImplementationBase");
                    }

                    var att = samplingMethodType.GetCustomAttributes(typeof(SamplingMethodAttribute)).FirstOrDefault();
                    if (att != null)
                    {
                        var name = ((SamplingMethodAttribute)att).Name;

                        SamplingMethodsImplementations.RegisterAggregationImplementation(name,
                                                                                         (SamplingImplementationBase)Activator.CreateInstance(samplingMethodType));
                        counter++;
                    }
                }
            }
            return(counter);
        }
Beispiel #2
0
        /// <summary>
        /// Helper method to create a new dynamic type for the group-by key.
        /// </summary>
        /// <param name="transformation">The group-by query.</param>
        /// <returns>The type of the key which was dynamically generated.</returns>
        internal Type GetGroupByKeyType(ApplyGroupbyClause transformation)
        {
            Contract.Assert(transformation != null);
            Contract.Assert(transformation.SelectedStatements != null);
            Contract.Assert(transformation.SelectedStatements.Any());

            var keyProperties = new List <Tuple <Type, string> >();
            var selectedStatementsDictionary = GetSelectedStatementsDictionary(transformation.SelectedStatements);

            foreach (var statement in selectedStatementsDictionary)
            {
                // simple property
                var statementString = statement.Value.First();
                if ((statement.Value.Count() == 1) && (statementString == statement.Key))
                {
                    string samplingMethod, alias, samplingProperty;
                    GroupByImplementation.GetSamplingMethod(statementString, out samplingMethod, out alias, out samplingProperty);
                    if (samplingMethod != null)
                    {
                        var pi = this.Context.ElementClrType.GetProperty(samplingProperty);
                        if (pi == null)
                        {
                            throw new ArgumentException(string.Format("Entity does not contain {0}", samplingProperty));
                        }
                        var implementation = SamplingMethodsImplementations.GetAggregationImplementation(samplingMethod);
                        var samplingType   = implementation.GetResultType(pi.PropertyType);
                        keyProperties.Add(new Tuple <Type, string>(samplingType, alias));
                    }
                    else
                    {
                        var propName = statementString.TrimMethodCall().Split(' ').First();
                        var pi       = this.Context.ElementClrType.GetProperty(propName);
                        if (pi == null)
                        {
                            throw new ArgumentException(string.Format("Entity does not contain {0}", propName));
                        }
                        keyProperties.Add(new Tuple <Type, string>(pi.PropertyType, pi.Name));
                    }
                }
                else
                {
                    // complex property
                    var propName = statement.Key.TrimMethodCall();
                    var pi       = this.Context.ElementClrType.GetProperty(propName);
                    if (pi == null)
                    {
                        throw new ArgumentException(string.Format("Entity does not contain {0}", propName));
                    }
                    var newPropertyType = this.GenerateComplexType(pi.PropertyType, statement.Value);
                    keyProperties.Add(new Tuple <Type, string>(newPropertyType, propName));
                }
            }

            return(AggregationTypesGenerator.CreateType(keyProperties.Distinct(new TypeStringTupleComapere()).ToList(), Context, true));
        }
Beispiel #3
0
        /// <summary>
        /// Continue the recursive operation of creating the Group-By key.
        /// </summary>
        /// <param name="declaringType">The type based on which we are going to create the new type.</param>
        /// <param name="segments">The select segments that declare what to create.</param>
        /// <returns>A new type.</returns>
        private Type GenerateComplexType(Type declaringType, IEnumerable <string> segments)
        {
            Contract.Assert(declaringType != null);
            Contract.Assert(segments != null);

            var keyProperties = new List <Tuple <Type, string> >();
            var selectedStatementsDictionary = GetSelectedStatementsDictionary(segments);

            foreach (var statement in selectedStatementsDictionary)
            {
                // simple property
                var statementString = statement.Value.First();
                if ((statement.Value.Count() == 1) && (statementString == statement.Key))
                {
                    string samplingMethod, alias, samplingProperty;
                    GroupByImplementation.GetSamplingMethod(statementString.TrimMethodCallSufix(), out samplingMethod, out alias, out samplingProperty);
                    if (samplingMethod != null)
                    {
                        var pi             = declaringType.GetProperty(samplingProperty);
                        var implementation = SamplingMethodsImplementations.GetAggregationImplementation(samplingMethod);
                        var samplingType   = implementation.GetResultType(pi.PropertyType);
                        keyProperties.Add(new Tuple <Type, string>(samplingType, alias));
                    }
                    else
                    {
                        statementString = statementString.Split(' ').First().TrimMethodCallSufix();
                        var pi = declaringType.GetProperty(statementString);
                        keyProperties.Add(new Tuple <Type, string>(pi.PropertyType, pi.Name));
                    }
                }
                else
                {
                    // complex property
                    var key             = statement.Key.Split(' ').First().TrimMethodCallSufix();
                    var pi              = declaringType.GetProperty(key);
                    var newPropertyType = GenerateComplexType(pi.PropertyType, statement.Value);
                    keyProperties.Add(new Tuple <Type, string>(newPropertyType, key));
                }
            }
            return(AggregationTypesGenerator.CreateType(keyProperties.Distinct(new TypeStringTupleComapere()).ToList(), Context, false));
        }
Beispiel #4
0
        /// <summary>
        /// Create a <see cref="LambdaExpression"/> such as: Expression{Func{Sales, keyType}} projectionLambda = s => new KeyType(Amount = s.Amount, Id=s.Id).
        /// </summary>
        /// <param name="selectStatements">The selected statements creating the key of the group-by operation.</param>
        /// <param name="keyType">The type of the key of the group-by operation.</param>
        /// <param name="propertiesToGroupByExpressions">List of expressions to the properties to group by.</param>
        /// <returns><see cref="LambdaExpression"/>.</returns>
        private LambdaExpression GetGroupByProjectionLambda(string[] selectStatements, Type keyType, LambdaExpression[] propertiesToGroupByExpressions)
        {
            Contract.Assert(keyType != null);
            Contract.Assert(selectStatements != null && selectStatements.Any());

            ParameterExpression entityParam;

            if (propertiesToGroupByExpressions != null && propertiesToGroupByExpressions.Any())
            {
                entityParam = propertiesToGroupByExpressions.First().Parameters.First();
            }
            else
            {
                entityParam = Expression.Parameter(this.Context.ElementClrType, "e");
            }

            var bindings = new List <MemberAssignment>();

            for (int i = 0; i < selectStatements.Length; i++)
            {
                var        statement = selectStatements[i];
                Expression selectedProperyExpression = null;
                if (propertiesToGroupByExpressions != null)
                {
                    selectedProperyExpression = propertiesToGroupByExpressions[i].Body;
                }

                string samplingMethod, alias, samplingProperty;
                GroupByImplementation.GetSamplingMethod(statement, out samplingMethod, out alias, out samplingProperty);
                if (samplingMethod == null)
                {
                    var prop = statement;
                    if (prop.Contains('/'))
                    {
                        prop = statement.Substring(0, prop.IndexOf('/'));
                    }

                    prop = prop.TrimMethodCall().Split(' ').First();
                    var mi = keyType.GetMember(prop).First();
                    bindings.Add(Expression.Bind(mi, ApplyImplementationBase.GetPropertyExpression(keyType, statement, entityParam, selectedProperyExpression)));
                }
                else
                {
                    string prop = alias;
                    if (statement.Contains('/'))
                    {
                        prop = statement.Substring(0, statement.IndexOf('/'));
                    }

                    var mi           = keyType.GetMember(prop).First();
                    var propertyType = GetPropertyInfo(this.Context.ElementClrType, statement).Last().PropertyType;

                    var        implementation = SamplingMethodsImplementations.GetAggregationImplementation(samplingMethod);
                    MethodInfo method         = implementation.GetSamplingProcessingMethod(propertyType);

                    var aggregationParamsExpressions = GetAggregationArgumentsExpressions(samplingMethod, method);

                    bindings.Add(Expression.Bind(mi, ApplyImplementationBase.GetComputedPropertyExpression(keyType, statement, entityParam, method, selectedProperyExpression, aggregationParamsExpressions)));
                }
            }

            var body = Expression.MemberInit(Expression.New(keyType), bindings);

            return(Expression.Lambda(body, entityParam));
        }
Beispiel #5
0
 static AggregationImplementations()
 {
     AggregationMethodsImplementations.Init();
     SamplingMethodsImplementations.Init();
 }