/// <summary> /// Begins the asynchronous variation. /// </summary> /// <param name="name">The name of the variation (will be the name of the method in the generated code).</param> /// <returns> /// CodeDom <see cref="CodeExpression"/> which can be used to refer to asynchronous continuation parameter in the generated code. /// </returns> public override CodeExpression BeginAsyncVariation(string name) { this.EnsureNotInBlock(); this.nextVariableSuffix.Clear(); this.currentVariation = this.currentBlock = new BlockInformation(name, BlockType.Method, new CodeStatementCollection(), true); return(Code.Argument("continuation")); }
/// <summary> /// Generates System.CodeDom.CodeExpression from the given expression. /// </summary> /// <param name="expression">Expression from which System.CodeDom.CodeExpression is generated.</param> /// <returns>Generated System.CodeDom.CodeExpression.</returns> public override CodeExpression Visit(LinqParameterExpression expression) { CodeParameterDeclarationExpression prm; if (!this.ParameterNamesDictionary.TryGetValue(expression, out prm)) { // for query syntax specific methods we use ParameterNamesDictionary, for non-specific methods (like Count, All, Any) we fall back to // using parametr name (just like in method syntax) return(Code.Argument(expression.Name)); } else { return(Code.Argument(prm.Name)); } }
private void BuildExecuteAndCompareAsyncCall(Dictionary <string, ReadOnlyCollection <QueryExpression> > freeVariableValues, CodeExpression continuationExpression, CodeExpressionWithFreeVariables generatedCode, int maxValueCount, Dictionary <string, QueryExpression> freeVariableAssignments, CodeExpression queryVariable) { var listVariable = this.CodeBuilder.Declare( "actionList", Code.New(Code.TypeRef <List <Action <IAsyncContinuation> > >())); for (int j = 0; j < maxValueCount; ++j) { if (j > 0) { // for anything but the first value, set the new value of each free variable var lambda = Code.Lambda().WithParameter("c"); this.CodeBuilder.BeginLambda(lambda); foreach (var freeVar in generatedCode.FreeVariables) { this.CodeBuilder.Add(Code.Variable(freeVar.Name).Assign(freeVar.PossibleValues[j % freeVar.PossibleValues.Count])); } this.CodeBuilder.Add(Code.Argument("c").Call("Continue")); this.CodeBuilder.EndLambda(); this.CodeBuilder.Add(listVariable.Call("Add", lambda)); } foreach (var free in generatedCode.FreeVariables) { freeVariableAssignments[free.Name] = freeVariableValues[free.Name][j % freeVariableValues[free.Name].Count]; } var lambdaBody = this.BuildQueryExecuteCallAndVariables(Code.Argument("c"), queryVariable, freeVariableAssignments); // add the lambda body to the list this.CodeBuilder.Add(listVariable.Call("Add", Code.Lambda().WithParameter("c").WithBody(lambdaBody))); } // add code to run all lambdas accumulated so far this.CodeBuilder.Add(Code.Type(typeof(AsyncHelpers).Name).Call("RunActionSequence", continuationExpression, listVariable)); }
private CodeExpression RewritePropertyReference(CodePropertyReferenceExpression propertyReference, ref bool didRewrite) { var targetRef = propertyReference.TargetObject as CodePropertyReferenceExpression; if (targetRef != null) { // // Replace this pattern : // context.entity // // With: // context.GetQueryRootForResourceSet(<entity reference name>).Cast<entity type>() // if (targetRef.PropertyName.Equals(DictionaryProviderServiceMethodResolver.ContextInstancePropertyName)) { var entitySet = this.model.GetDefaultEntityContainer().EntitySets.SingleOrDefault(s => s.Name == propertyReference.PropertyName); if (entitySet != null) { this.rootEntitySet = entitySet; var setLambda = Code.Lambda().WithParameter("s").WithBody(Code.Argument("s").Property("Name").Equal(Code.Primitive(entitySet.Name), DataTypes.String)); var setReference = Code.This().Property(DictionaryProviderServiceMethodResolver.ContextInstancePropertyName).Property(ResourceSetsPropertyName).Call("Single", setLambda); var setBaseTypeReference = this.GetClrTypeReference(entitySet.EntityType); didRewrite = true; return(targetRef.Call(RootQueryMethodName, setReference).Call("Cast", new CodeTypeReference[] { setBaseTypeReference })); } } } else if (propertyReference.TargetObject is CodeArgumentReferenceExpression) { if (this.rootEntitySet != null) { // TODO: more derived types? var entityType = this.rootEntitySet.EntityType; var property = entityType.AllProperties.Where(p => p.Name == propertyReference.PropertyName && !p.IsTypeBacked()).SingleOrDefault(); if (property != null) { // // Replace this pattern: // arg.EntityProperty // With: // (<property type>)arg[<PropertyName>] // var propertyType = property.PropertyType; CodeTypeReference propertyTypeRef = Code.TypeRef(typeof(object)); if (propertyType != null) { var primitivePropertyType = propertyType as PrimitiveDataType; if (primitivePropertyType != null) { var clrType = primitivePropertyType.GetFacetValue <PrimitiveClrTypeFacet, Type>(typeof(object)); // We need to make sure we have the right primitive type for the cast below. // If its a nullable property, then the CLR type we cast to must be nullable as well bool clrTypeIsNullable = clrType.IsClass() || Nullable.GetUnderlyingType(clrType) != null; if (primitivePropertyType.IsNullable && !clrTypeIsNullable) { clrType = typeof(Nullable <>).MakeGenericType(clrType); } propertyTypeRef = Code.TypeRef(clrType); } var complexPropertyType = propertyType as ComplexDataType; if (complexPropertyType != null) { propertyTypeRef = this.GetClrTypeReference(complexPropertyType.Definition); } } didRewrite = true; return(propertyReference.TargetObject.Index(Code.Primitive(propertyReference.PropertyName)).Cast(propertyTypeRef)); } } } return(base.Rewrite(propertyReference, ref didRewrite)); }
/// <summary> /// Generates System.CodeDom.CodeExpression from the given expression. /// </summary> /// <param name="expression">Expression from which System.CodeDom.CodeExpression is generated.</param> /// <returns>Generated System.CodeDom.CodeExpression.</returns> public override CodeExpression Visit(LinqParameterExpression expression) { return(Code.Argument(expression.Name)); }