private void GeneratePropertyGetter(PropertyDescriptor propertyDescriptor) { string fieldName = CodeGenUtilities.MakeCompliantFieldName(propertyDescriptor.Name); #line default #line hidden #line 94 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("get\r\n{\r\n\treturn this."); #line default #line hidden #line 97 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(this.ToStringHelper.ToStringWithCulture(fieldName)); #line default #line hidden #line 97 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(";\r\n} \r\n"); #line default #line hidden #line 99 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" }
public AssociationMetadata(PropertyDescriptor pd) { this.PropertyDescriptor = pd; AttributeCollection propertyAttributes = pd.ExplicitAttributes(); this.AssociationAttribute = (AssociationAttribute)propertyAttributes[typeof(AssociationAttribute)]; this.IsExternal = propertyAttributes[typeof(ExternalReferenceAttribute)] != null; this.IsCollection = EntityGenerator.IsCollectionType(pd.PropertyType); if (!this.IsCollection) { this.PropTypeName = CodeGenUtilities.GetTypeName(pd.PropertyType); this.AssociationTypeName = @"OpenRiaServices.DomainServices.Client.EntityRef<" + this.PropTypeName + ">"; this.Attributes = propertyAttributes.Cast <Attribute>().Where(a => a.GetType() != typeof(DataMemberAttribute)); } else { this.PropTypeName = CodeGenUtilities.GetTypeName(TypeUtility.GetElementType(pd.PropertyType)); this.AssociationTypeName = "OpenRiaServices.DomainServices.Client.EntityCollection<" + this.PropTypeName + ">"; List <Attribute> attributeList = propertyAttributes.Cast <Attribute>().ToList(); ReadOnlyAttribute readOnlyAttr = propertyAttributes.OfType <ReadOnlyAttribute>().SingleOrDefault(); if (readOnlyAttr != null && !propertyAttributes.OfType <EditableAttribute>().Any()) { attributeList.Add(new EditableAttribute(!readOnlyAttr.IsReadOnly)); } this.Attributes = attributeList.Where(a => a.GetType() != typeof(DataMemberAttribute)); } this.PropertyName = CodeGenUtilities.GetSafeName(pd.Name); this.FieldName = CodeGenUtilities.MakeCompliantFieldName(this.PropertyName); }
private void GeneratePropertyGetter(PropertyDescriptor propertyDescriptor) { string fieldName = CodeGenUtilities.MakeCompliantFieldName(propertyDescriptor.Name); this.Write("get\r\n{\r\n\treturn this."); this.Write(this.ToStringHelper.ToStringWithCulture(fieldName)); this.Write(";\r\n} \r\n"); }
private void GenerateBackingPrivateField(PropertyDescriptor propertyDescriptor) { Type propertyType = CodeGenUtilities.TranslateType(propertyDescriptor.PropertyType); string propertyTypeName = CodeGenUtilities.GetTypeName(propertyType); string fieldName = CodeGenUtilities.MakeCompliantFieldName(propertyDescriptor.Name); this.Write("private "); this.Write(this.ToStringHelper.ToStringWithCulture(propertyTypeName)); this.Write(" "); this.Write(this.ToStringHelper.ToStringWithCulture(fieldName)); this.Write(";\r\n"); }
private void GenerateBackingPrivateField(PropertyDescriptor propertyDescriptor) { Type propertyType = CodeGenUtilities.TranslateType(propertyDescriptor.PropertyType); string propertyTypeName = CodeGenUtilities.GetTypeName(propertyType); string fieldName = CodeGenUtilities.MakeCompliantFieldName(propertyDescriptor.Name); #line default #line hidden #line 149 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("private "); #line default #line hidden #line 150 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(this.ToStringHelper.ToStringWithCulture(propertyTypeName)); #line default #line hidden #line 150 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(" "); #line default #line hidden #line 150 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(this.ToStringHelper.ToStringWithCulture(fieldName)); #line default #line hidden #line 150 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(";\r\n"); #line default #line hidden #line 151 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" }
private void GeneratePropertySetter(PropertyDescriptor propertyDescriptor) { string fieldName = CodeGenUtilities.MakeCompliantFieldName(propertyDescriptor.Name); this.Write("set \r\n"); this.GenerateOpeningBrace(); this.Write("if(this."); this.Write(this.ToStringHelper.ToStringWithCulture(fieldName)); this.Write(" != value)\r\n"); this.GenerateOpeningBrace(); this.Write("this.On"); this.Write(this.ToStringHelper.ToStringWithCulture(propertyDescriptor.Name)); this.Write("Changing(value);\t\r\n"); bool propertyIsReadOnly = this.IsPropertyReadOnly(propertyDescriptor); if (!propertyIsReadOnly) { this.Write("this.RaiseDataMemberChanging(\""); this.Write(this.ToStringHelper.ToStringWithCulture(propertyDescriptor.Name)); this.Write("\");\r\n"); } this.Write("this.ValidateProperty(\""); this.Write(this.ToStringHelper.ToStringWithCulture(propertyDescriptor.Name)); this.Write("\", value);\r\nthis."); this.Write(this.ToStringHelper.ToStringWithCulture(fieldName)); this.Write(" = value;\r\n"); if (!propertyIsReadOnly) { this.Write("this.RaiseDataMemberChanged(\""); this.Write(this.ToStringHelper.ToStringWithCulture(propertyDescriptor.Name)); this.Write("\");\r\n"); } else { this.Write("this.RaisePropertyChanged(\""); this.Write(this.ToStringHelper.ToStringWithCulture(propertyDescriptor.Name)); this.Write("\");\r\n"); } this.Write("this.On"); this.Write(this.ToStringHelper.ToStringWithCulture(propertyDescriptor.Name)); this.Write("Changed();\r\n"); this.GenerateClosingBrace(); this.GenerateClosingBrace(); }
private void GeneratePropertySetter(PropertyDescriptor propertyDescriptor) { string fieldName = CodeGenUtilities.MakeCompliantFieldName(propertyDescriptor.Name); #line default #line hidden #line 105 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("set \r\n"); #line default #line hidden #line 107 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.GenerateOpeningBrace(); #line default #line hidden #line 108 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("if(this."); #line default #line hidden #line 109 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(this.ToStringHelper.ToStringWithCulture(fieldName)); #line default #line hidden #line 109 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(" != value)\r\n"); #line default #line hidden #line 110 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.GenerateOpeningBrace(); #line default #line hidden #line 111 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("this.On"); #line default #line hidden #line 112 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(this.ToStringHelper.ToStringWithCulture(propertyDescriptor.Name)); #line default #line hidden #line 112 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("Changing(value);\t\r\n"); #line default #line hidden #line 113 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" bool propertyIsReadOnly = this.IsPropertyReadOnly(propertyDescriptor); if (!propertyIsReadOnly) { #line default #line hidden #line 117 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("this.RaiseDataMemberChanging(\""); #line default #line hidden #line 118 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(this.ToStringHelper.ToStringWithCulture(propertyDescriptor.Name)); #line default #line hidden #line 118 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("\");\r\n"); #line default #line hidden #line 119 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" } #line default #line hidden #line 121 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("this.ValidateProperty(\""); #line default #line hidden #line 122 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(this.ToStringHelper.ToStringWithCulture(propertyDescriptor.Name)); #line default #line hidden #line 122 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("\", value);\r\nthis."); #line default #line hidden #line 123 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(this.ToStringHelper.ToStringWithCulture(fieldName)); #line default #line hidden #line 123 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(" = value;\r\n"); #line default #line hidden #line 124 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" if (!propertyIsReadOnly) { #line default #line hidden #line 127 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("this.RaiseDataMemberChanged(\""); #line default #line hidden #line 128 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(this.ToStringHelper.ToStringWithCulture(propertyDescriptor.Name)); #line default #line hidden #line 128 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("\");\r\n"); #line default #line hidden #line 129 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" } else { #line default #line hidden #line 133 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("this.RaisePropertyChanged(\""); #line default #line hidden #line 134 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(this.ToStringHelper.ToStringWithCulture(propertyDescriptor.Name)); #line default #line hidden #line 134 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("\");\r\n"); #line default #line hidden #line 135 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" } #line default #line hidden #line 137 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("this.On"); #line default #line hidden #line 138 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write(this.ToStringHelper.ToStringWithCulture(propertyDescriptor.Name)); #line default #line hidden #line 138 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.Write("Changed();\r\n"); #line default #line hidden #line 139 "C:\Code\Repos\openriaservices\OpenRiaServices.DomainServices.Tools.TextTemplate\Framework\CSharpGenerators\Templates\DataContractGeneratorTemplate.ttinclude" this.GenerateClosingBrace(); this.GenerateClosingBrace(); }
/// <summary> /// Generates a property getter/setter pair into the given proxy class to match the given property info. /// </summary> /// <param name="propertyDescriptor">PropertyDescriptor for the property to generate for.</param> protected virtual void GenerateProperty(PropertyDescriptor propertyDescriptor) { string propertyName = propertyDescriptor.Name; Type propertyType = CodeGenUtilities.TranslateType(propertyDescriptor.PropertyType); // ---------------------------------------------------------------- // Property type ref // ---------------------------------------------------------------- var propTypeReference = CodeGenUtilities.GetTypeReference(propertyType, ClientProxyGenerator, ProxyClass); // ---------------------------------------------------------------- // Property decl // ---------------------------------------------------------------- var property = new CodeMemberProperty(); property.Name = propertyName; property.Type = propTypeReference; property.Attributes = MemberAttributes.Public | MemberAttributes.Final; // final needed, else becomes virtual List <Attribute> propertyAttributes = propertyDescriptor.ExplicitAttributes().Cast <Attribute>().ToList(); // Generate <summary> for property string comment = string.Format(CultureInfo.CurrentCulture, Resource.CodeGen_Entity_Property_Summary_Comment, propertyName); property.Comments.AddRange(CodeGenUtilities.GenerateSummaryCodeComment(comment, ClientProxyGenerator.IsCSharp)); // ---------------------------------------------------------------- // [DataMember] -> Add if not already present. // ---------------------------------------------------------------- // Add if not already present. if (!propertyAttributes.OfType <DataMemberAttribute>().Any()) { CodeAttributeDeclaration dataMemberAtt = CodeGenUtilities.CreateAttributeDeclaration(typeof(DataMemberAttribute), ClientProxyGenerator, ProxyClass); property.CustomAttributes.Add(dataMemberAtt); } // Here, we check for the existence of [ReadOnly(true)] attributes generated when // the property does not not have a setter. We want to inject an [Editable(false)] // attribute into the pipeline. ReadOnlyAttribute readOnlyAttr = propertyAttributes.OfType <ReadOnlyAttribute>().SingleOrDefault(); if (readOnlyAttr != null && !propertyAttributes.OfType <EditableAttribute>().Any()) { propertyAttributes.Add(new EditableAttribute(!readOnlyAttr.IsReadOnly)); // REVIEW: should we strip out [ReadOnly] attributes here? } // Here, we check for the presence of a complex type. If it exists we need to add a DisplayAttribute // if not already there. DataSources windows do not handle complex types if (TypeUtility.IsSupportedComplexType(propertyType) && !propertyAttributes.OfType <DisplayAttribute>().Any()) { CodeAttributeDeclaration displayAttribute = CodeGenUtilities.CreateDisplayAttributeDeclaration(ClientProxyGenerator, ProxyClass); property.CustomAttributes.Add(displayAttribute); } // ---------------------------------------------------------------- // Propagate the custom attributes // ---------------------------------------------------------------- CustomAttributeGenerator.GenerateCustomAttributes( ClientProxyGenerator, ProxyClass, ex => string.Format(CultureInfo.CurrentCulture, Resource.ClientCodeGen_Attribute_ThrewException_CodeTypeMember, ex.Message, property.Name, ProxyClass.Name, ex.InnerException.Message), propertyAttributes.Cast <Attribute>(), property.CustomAttributes, property.Comments); // ---------------------------------------------------------------- // backing private field (CodeDom doesn't yet know about auto properties) // ---------------------------------------------------------------- string fieldName = CodeGenUtilities.MakeCompliantFieldName(propertyName); var field = new CodeMemberField(propTypeReference, fieldName); ProxyClass.Members.Add(field); var fieldRef = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName); var valueRef = new CodePropertySetValueReferenceExpression(); // ---------------------------------------------------------------- // getter body // ---------------------------------------------------------------- property.GetStatements.Add(new CodeMethodReturnStatement(fieldRef)); // ---------------------------------------------------------------- // setter body // ---------------------------------------------------------------- List <CodeStatement> bodyStatements = new List <CodeStatement>(); // this.OnPropertyXxxChanging(PropType value); bodyStatements.Add(NotificationMethodGen.GetMethodInvokeExpressionStatementFor(propertyName + "Changing")); bool propertyIsReadOnly = IsPropertyReadOnly(propertyDescriptor); if (!propertyIsReadOnly) { bodyStatements.Add(new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "RaiseDataMemberChanging", new CodePrimitiveExpression(propertyDescriptor.Name)))); } // Generate the validation tests. CodeStatement validationCode = GeneratePropertySetterValidation(propertyDescriptor.Name); bodyStatements.Add(validationCode); // this._field = value bodyStatements.Add(new CodeAssignStatement(fieldRef, valueRef)); if (!propertyIsReadOnly) { bodyStatements.Add(new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "RaiseDataMemberChanged", new CodePrimitiveExpression(propertyDescriptor.Name)))); } else { // even read-only members need to raise PropertyChanged bodyStatements.Add(new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "RaisePropertyChanged", new CodePrimitiveExpression(propertyDescriptor.Name)))); } // this.OnPropertyXxxChanged(); bodyStatements.Add(NotificationMethodGen.GetMethodInvokeExpressionStatementFor(propertyName + "Changed")); // if (this._field != value)... CodeExpression valueTest = CodeGenUtilities.MakeNotEqual(propertyType, fieldRef, valueRef, ClientProxyGenerator.IsCSharp); CodeConditionStatement body = new CodeConditionStatement(valueTest, bodyStatements.ToArray <CodeStatement>()); property.SetStatements.Add(body); // add property ProxyClass.Members.Add(property); }