Beispiel #1
0
        /// <summary>
        /// Checks whether the projection references the projector's parameter.
        /// </summary>
        /// <param name="expression">Projector.</param>
        public void Validate(Expression expression)
        {
            var projector = (LambdaExpression)expression;

            var paramSeeker = new ParameterSeeker();

            if (!paramSeeker.Exists(projector.Body, projector.Parameters[0]))
            {
                throw new InvalidProjectionException("The projection does not hold any reference to the projector's parameter.", projector.Body);
            }
        }
Beispiel #2
0
        protected override Expression VisitColumn(ColumnExpression c)
        {
            string     columnName     = string.Empty;
            Type       columnDataType = null;
            bool       isNullable     = false;
            Expression columnMapper   = c.ColumnMapper;

            if (_memberInScope != null)
            {
                columnName     = _memberInScope.Name;
                columnDataType = ((PropertyInfo)_memberInScope).PropertyType;

                _memberInScope = null;
            }

            if (columnMapper is ConstantExpression constantExp && constantExp.Value is ColumnSettings columnSettings)
            {
                columnMapper   = ((LambdaExpression)columnSettings.Mapper).Body;
                columnName     = !string.IsNullOrEmpty(columnSettings.ColumnName) ? columnSettings.ColumnName : columnName;
                columnDataType = columnSettings.ColumnDataType;
            }

            if (string.IsNullOrEmpty(columnName))
            {
                throw new InvalidOperationException("Can't find/infer the column's name. Review your projection and ensure you either implicit or explicitily you set the column's name.");
            }

            if (columnDataType == typeof(string))
            {
                isNullable = true;
            }
            else
            {
                Type nullableType = Nullable.GetUnderlyingType(columnDataType);
                if (nullableType != null)
                {
                    columnDataType = nullableType;
                    isNullable     = true;
                }
            }

            if (!columnDataType.IsBuiltInType())
            {
                throw new InvalidColumnDataTypeException($"The data type for the column \"{columnName}\" is invalid: {columnDataType.ToString()}.");
            }

            LambdaExpression le = null;
            bool             isMapperExprParameterless = !_parameterSeeker.Exists(columnMapper, _projectorParameter);

            if (isMapperExprParameterless)
            {
                le = Expression.Lambda(columnMapper);
            }
            else
            {
                le = Expression.Lambda(columnMapper, _projectorParameter);
            }

            var column = new Column()
            {
                Name                  = columnName,
                DataType              = columnDataType,
                IsNullable            = isNullable,
                Mapper                = le.Compile(),
                IsMapperParameterless = isMapperExprParameterless
            };

            _columns.Add(column);

            return(c);
        }