Example #1
0
        private Expression CreateLambdaBodyInitializer(IDictionary <ResourceFieldAttribute, QueryLayer> selectors, ResourceContext resourceContext,
                                                       LambdaScope lambdaScope, bool lambdaAccessorRequiresTestForNull)
        {
            var propertySelectors = ToPropertySelectors(selectors, resourceContext, lambdaScope.Parameter.Type);

            MemberBinding[] propertyAssignments = propertySelectors.Select(selector => CreatePropertyAssignment(selector, lambdaScope)).Cast <MemberBinding>().ToArray();

            NewExpression newExpression = _resourceFactory.CreateNewExpression(lambdaScope.Accessor.Type);
            Expression    memberInit    = Expression.MemberInit(newExpression, propertyAssignments);

            if (lambdaScope.HasManyThrough != null)
            {
                MemberBinding outerPropertyAssignment = Expression.Bind(lambdaScope.HasManyThrough.RightProperty, memberInit);

                NewExpression outerNewExpression = _resourceFactory.CreateNewExpression(lambdaScope.HasManyThrough.ThroughType);
                memberInit = Expression.MemberInit(outerNewExpression, outerPropertyAssignment);
            }

            if (!lambdaAccessorRequiresTestForNull)
            {
                return(memberInit);
            }

            return(TestForNull(lambdaScope.Accessor, memberInit));
        }
Example #2
0
        private Expression CreateCollectionInitializer(LambdaScope lambdaScope, PropertyInfo collectionProperty,
                                                       Type elementType, QueryLayer layer, LambdaScopeFactory lambdaScopeFactory)
        {
            MemberExpression propertyExpression = Expression.Property(lambdaScope.Accessor, collectionProperty);

            var builder = new QueryableBuilder(propertyExpression, elementType, typeof(Enumerable), _nameFactory,
                                               _resourceFactory, _resourceContextProvider, _entityModel, lambdaScopeFactory);

            Expression layerExpression = builder.ApplyQuery(layer);

            Type enumerableOfElementType = typeof(IEnumerable <>).MakeGenericType(elementType);
            Type typedCollection         = collectionProperty.PropertyType.ToConcreteCollectionType();

            ConstructorInfo typedCollectionConstructor = typedCollection.GetConstructor(new[]
            {
                enumerableOfElementType
            });

            if (typedCollectionConstructor == null)
            {
                throw new Exception(
                          $"Constructor on '{typedCollection.Name}' that accepts '{enumerableOfElementType.Name}' not found.");
            }

            return(Expression.New(typedCollectionConstructor, layerExpression));
        }
Example #3
0
 public IncludeClauseBuilder(Expression source, LambdaScope lambdaScope, ResourceContext resourceContext,
                             IResourceContextProvider resourceContextProvider)
     : base(lambdaScope)
 {
     _source                  = source ?? throw new ArgumentNullException(nameof(source));
     _resourceContext         = resourceContext ?? throw new ArgumentNullException(nameof(resourceContext));
     _resourceContextProvider = resourceContextProvider ?? throw new ArgumentNullException(nameof(resourceContextProvider));
 }
Example #4
0
 public SelectClauseBuilder(Expression source, LambdaScope lambdaScope, IModel entityModel, Type extensionType,
                            LambdaParameterNameFactory nameFactory, IResourceFactory resourceFactory, IResourceContextProvider resourceContextProvider)
     : base(lambdaScope)
 {
     _source                  = source ?? throw new ArgumentNullException(nameof(source));
     _entityModel             = entityModel ?? throw new ArgumentNullException(nameof(entityModel));
     _extensionType           = extensionType ?? throw new ArgumentNullException(nameof(extensionType));
     _nameFactory             = nameFactory ?? throw new ArgumentNullException(nameof(nameFactory));
     _resourceFactory         = resourceFactory ?? throw new ArgumentNullException(nameof(resourceFactory));
     _resourceContextProvider = resourceContextProvider ?? throw new ArgumentNullException(nameof(resourceContextProvider));
 }
Example #5
0
        private MemberAssignment CreatePropertyAssignment(PropertySelector selector, LambdaScope lambdaScope)
        {
            MemberExpression propertyAccess = Expression.Property(lambdaScope.Accessor, selector.Property);

            Expression assignmentRightHandSide = propertyAccess;

            if (selector.NextLayer != null)
            {
                HasManyThroughAttribute hasManyThrough = selector.OriginatingField as HasManyThroughAttribute;
                var lambdaScopeFactory = new LambdaScopeFactory(_nameFactory, hasManyThrough);

                assignmentRightHandSide = CreateAssignmentRightHandSideForLayer(selector.NextLayer, lambdaScope, propertyAccess,
                                                                                selector.Property, lambdaScopeFactory);
            }

            return(Expression.Bind(selector.Property, assignmentRightHandSide));
        }
Example #6
0
        private Expression CreateAssignmentRightHandSideForLayer(QueryLayer layer, LambdaScope outerLambdaScope, MemberExpression propertyAccess,
                                                                 PropertyInfo selectorPropertyInfo, LambdaScopeFactory lambdaScopeFactory)
        {
            Type collectionElementType = TypeHelper.TryGetCollectionElementType(selectorPropertyInfo.PropertyType);
            Type bodyElementType       = collectionElementType ?? selectorPropertyInfo.PropertyType;

            if (collectionElementType != null)
            {
                return(CreateCollectionInitializer(outerLambdaScope, selectorPropertyInfo, bodyElementType, layer, lambdaScopeFactory));
            }

            if (layer.Projection == null || !layer.Projection.Any())
            {
                return(propertyAccess);
            }

            using var scope = lambdaScopeFactory.CreateScope(bodyElementType, propertyAccess);
            return(CreateLambdaBodyInitializer(layer.Projection, layer.ResourceContext, scope, true));
        }
 public OrderClauseBuilder(Expression source, LambdaScope lambdaScope, Type extensionType)
     : base(lambdaScope)
 {
     _source        = source ?? throw new ArgumentNullException(nameof(source));
     _extensionType = extensionType ?? throw new ArgumentNullException(nameof(extensionType));
 }
 protected QueryClauseBuilder(LambdaScope lambdaScope)
 {
     LambdaScope = lambdaScope ?? throw new ArgumentNullException(nameof(lambdaScope));
 }