Exemple #1
0
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {
            this.Write("    /// <summary>\r\n");
            this.Write(this.ToStringHelper.ToStringWithCulture(CommentUtilities.FormatTripleSlashComment(type.SummaryComment, CommentUtilities.Indentation.TypeLevel)));
            this.Write("\r\n    /// </summary>\r\n");

            if (!string.IsNullOrEmpty(type.RemarksComment))
            {
                this.Write("    /// <remarks>\r\n");
                this.Write(this.ToStringHelper.ToStringWithCulture(CommentUtilities.FormatTripleSlashComment(type.RemarksComment, CommentUtilities.Indentation.TypeLevel)));
                this.Write("\r\n    /// </remarks>\r\n");
            }

            this.Write("    public partial class ");
            this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
            this.Write(" :");
            if (type.ShouldDefineGetTransportObject)
            {
                this.Write(" ITransportObjectProvider<");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.TransportObjectTypeName));
                this.Write(">,");
            }
            this.Write(" IPropertyMetadata\r\n    {\r\n        #region Constructors\r\n");
            if (type.ShouldDefineCustomConstructor)
            {
                this.Write("        /// <summary>\r\n        /// Initializes a new instance of the <see cref=\"");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
                this.Write("\"/> class.\r\n        /// </summary>\r\n");

                foreach (var property in type.ConstructorProperties)
                {
                    this.Write("        /// <param name=\'");
                    this.Write(this.ToStringHelper.ToStringWithCulture(property.CamelName));
                    this.Write("\'>");
                    this.Write(this.ToStringHelper.ToStringWithCulture(CommentUtilities.FormatTripleSlashComment(property.SummaryComment, CommentUtilities.Indentation.TypeMemberLevel, false)));
                    this.Write("</param>\r\n");
                }

                this.Write("        ");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.ConstructorVisibilityString));
                this.Write(" ");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
                this.Write("(");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.GetCustomConstructorParametersString(CommentUtilities.Indentation.TabSizeInSpaces * 3)));
                this.Write(")\r\n        {\r\n");

                foreach (var property in type.ConstructorProperties)
                {
                    this.Write("            this.");
                    this.Write(this.ToStringHelper.ToStringWithCulture(property.Name));
                    this.Write(" = ");
                    this.Write(this.ToStringHelper.ToStringWithCulture(property.CamelName));
                    this.Write(";\r\n");
                }

                this.Write("        }\r\n");
            }

            this.Write("\r\n        internal ");
            this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
            this.Write("(");
            this.Write(this.ToStringHelper.ToStringWithCulture(type.GetBoundConstructorParameterString(CommentUtilities.Indentation.TabSizeInSpaces * 3)));
            this.Write(")\r\n        {\r\n");

            foreach (var kvp in type.BoundProperties)
            {
                var protocolObjectGetter = CodeGenerationUtilities.GetProtocolToObjectModelString(kvp.Key, kvp.Value);

                this.Write("            this.");
                this.Write(this.ToStringHelper.ToStringWithCulture(kvp.Key.Name));
                this.Write(" = ");
                this.Write(this.ToStringHelper.ToStringWithCulture(protocolObjectGetter));
                this.Write(";\r\n");
            }

            this.Write("        }\r\n\r\n        #endregion Constructors\r\n\r\n        #region ");
            this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
            this.Write("\r\n\r\n");

            foreach (var property in type.OrderedPublicProperties.Select(p => p.Key))
            {
                this.Write("        /// <summary>\r\n");
                this.Write(this.ToStringHelper.ToStringWithCulture(CommentUtilities.FormatTripleSlashComment(property.FullSummaryComment, CommentUtilities.Indentation.TypeMemberLevel)));
                this.Write("\r\n        /// </summary>\r\n");

                if (!string.IsNullOrEmpty(property.RemarksComment))
                {
                    this.Write("        /// <remarks>\r\n");
                    this.Write(this.ToStringHelper.ToStringWithCulture(CommentUtilities.FormatTripleSlashComment(property.RemarksComment, CommentUtilities.Indentation.TypeMemberLevel)));
                    this.Write("\r\n        /// </remarks>\r\n");
                }

                this.Write("        public ");
                this.Write(this.ToStringHelper.ToStringWithCulture(property.Type));
                this.Write(" ");
                this.Write(this.ToStringHelper.ToStringWithCulture(property.Name));
                this.Write(" { get; }\r\n\r\n");
            }

            this.Write("        #endregion // ");
            this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
            this.Write(@"

        #region IPropertyMetadata

        bool IModifiable.HasBeenModified
        {
            //This class is compile time readonly so it cannot have been modified
            get { return false; }
        }

        bool IReadOnly.IsReadOnly
        {
            get { return true; }
            set
            {
                // This class is compile time readonly already
            }
        }

        #endregion // IPropertyMetadata
");

            string transportObjectText      = (new GetTransportObjectSnippet(type)).TransformText();
            string collectionConversionText = (new CollectionConversionSnippet(type)).TransformText();

            if (!string.IsNullOrEmpty(transportObjectText) || !string.IsNullOrEmpty(collectionConversionText))
            {
                this.Write("\r\n        #region Internal/private methods\r\n\r\n");
                this.Write(this.ToStringHelper.ToStringWithCulture(transportObjectText));
                this.Write("\r\n");
                this.Write(this.ToStringHelper.ToStringWithCulture(collectionConversionText));
                this.Write("\r\n        #endregion // Internal/private methods\r\n");
            }

            this.Write("    }");
            return(this.GenerationEnvironment.ToString());
        }
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {
            this.Write("    /// <summary>\r\n");
            this.Write(this.ToStringHelper.ToStringWithCulture(CommentUtilities.FormatTripleSlashComment(type.SummaryComment, CommentUtilities.Indentation.TypeLevel)));
            this.Write("\r\n    /// </summary>\r\n");

            if (!string.IsNullOrEmpty(type.RemarksComment))
            {
                this.Write("    /// <remarks>\r\n");
                this.Write(this.ToStringHelper.ToStringWithCulture(CommentUtilities.FormatTripleSlashComment(type.RemarksComment, CommentUtilities.Indentation.TypeLevel)));
                this.Write("\r\n    /// </remarks>\r\n");
            }

            this.Write("    public partial class ");
            this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
            this.Write(" :");
            if (type.ShouldDefineGetTransportObject)
            {
                this.Write(" ITransportObjectProvider<");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.TransportObjectTypeName));
                this.Write(">,");
            }
            if (type.IsTopLevelObject)
            {
                this.Write(" IInheritedBehaviors,");
            }
            this.Write(" IPropertyMetadata\r\n    {\r\n        private class PropertyContainer : PropertyColl" +
                       "ection\r\n        {\r\n");

            foreach (var property in type.OrderedPublicProperties.Select(p => p.Key))
            {
                this.Write("            public readonly PropertyAccessor<");
                this.Write(this.ToStringHelper.ToStringWithCulture(property.Type));
                this.Write("> ");
                this.Write(this.ToStringHelper.ToStringWithCulture(property.PropertyContainerPropertyName));
                this.Write(";\r\n");
            }
            if (type.UnboundProperties.Any())
            {
                this.Write("\r\n            public PropertyContainer() : base(BindingState.Unbound)\r\n          " +
                           "  {\r\n");

                foreach (var property in type.OrderedPublicProperties.Select(p => p.Key))
                {
                    this.Write("                this.");
                    this.Write(this.ToStringHelper.ToStringWithCulture(property.PropertyContainerPropertyName));
                    this.Write(" = this.CreatePropertyAccessor<");
                    this.Write(this.ToStringHelper.ToStringWithCulture(property.Type));
                    this.Write(">(nameof(");
                    this.Write(this.ToStringHelper.ToStringWithCulture(property.Name));
                    this.Write("), ");
                    this.Write(this.ToStringHelper.ToStringWithCulture(CodeGenerationUtilities.GenerateBindingAccessString(property.UnboundAccess)));
                    this.Write(");\r\n");
                }

                this.Write("            }\r\n\r\n");
            }
            if (type.BoundProperties.Any())
            {
                this.Write("            public PropertyContainer(");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.ProtocolName));
                this.Write(" protocolObject) : base(BindingState.Bound)\r\n            {\r\n");

                foreach (var kvp in type.OrderedPublicProperties)
                {
                    var protocolObjectGetter = CodeGenerationUtilities.GetProtocolToObjectModelString(kvp.Key, kvp.Value);
                    if (kvp.Key.BoundAccess != BindingAccess.None)
                    {
                        this.Write("                this.");
                        this.Write(this.ToStringHelper.ToStringWithCulture(kvp.Key.PropertyContainerPropertyName));
                        this.Write(" = this.CreatePropertyAccessor(\r\n                    ");
                        this.Write(this.ToStringHelper.ToStringWithCulture(protocolObjectGetter));
                        this.Write(",\r\n                    nameof(");
                        this.Write(this.ToStringHelper.ToStringWithCulture(kvp.Key.Name));
                        this.Write("),\r\n                    ");
                        this.Write(this.ToStringHelper.ToStringWithCulture(CodeGenerationUtilities.GenerateBindingAccessString(kvp.Key.BoundAccess)));
                        this.Write(");\r\n");
                    }
                    else
                    {
                        this.Write("                this.");
                        this.Write(this.ToStringHelper.ToStringWithCulture(kvp.Key.PropertyContainerPropertyName));
                        this.Write(" = this.CreatePropertyAccessor<");
                        this.Write(this.ToStringHelper.ToStringWithCulture(kvp.Key.Type));
                        this.Write(">(\r\n                    nameof(");
                        this.Write(this.ToStringHelper.ToStringWithCulture(kvp.Key.Name));
                        this.Write("),\r\n                    ");
                        this.Write(this.ToStringHelper.ToStringWithCulture(CodeGenerationUtilities.GenerateBindingAccessString(kvp.Key.BoundAccess)));
                        this.Write(");\r\n");
                    }
                }

                this.Write("            }\r\n");
            }

            this.Write("        }\r\n\r\n        private");
            if (!type.IsTopLevelObject)
            {
                this.Write(" readonly");
            }
            this.Write(" PropertyContainer propertyContainer;\r\n");

            if (type.IsTopLevelObject)
            {
                this.Write("\r\n        private readonly BatchClient parentBatchClient;\r\n");

                foreach (var parentPathVariableName in type.ParentPathVariables)
                {
                    this.Write("        private readonly string ");
                    this.Write(this.ToStringHelper.ToStringWithCulture(parentPathVariableName));
                    this.Write(";\r\n\r\n        internal string ");
                    this.Write(this.ToStringHelper.ToStringWithCulture(StringUtilities.ToPascalCase(parentPathVariableName)));
                    this.Write("\r\n        {\r\n            get\r\n            {\r\n                return this.");
                    this.Write(this.ToStringHelper.ToStringWithCulture(parentPathVariableName));
                    this.Write(";\r\n            }\r\n        }\r\n");
                }
            }

            this.Write("\r\n        #region Constructors\r\n\r\n");

            if (type.ShouldDefineCustomConstructor)
            {
                this.Write("        /// <summary>\r\n        /// Initializes a new instance of the <see cref=\"");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
                this.Write("\"/> class.\r\n        /// </summary>\r\n");

                foreach (var property in type.ConstructorProperties)
                {
                    this.Write("        /// <param name=\'");
                    this.Write(this.ToStringHelper.ToStringWithCulture(property.CamelName));
                    this.Write("\'>");
                    this.Write(this.ToStringHelper.ToStringWithCulture(CommentUtilities.FormatTripleSlashComment(property.SummaryComment, CommentUtilities.Indentation.TypeMemberLevel, false)));
                    this.Write("</param>\r\n");
                }

                this.Write("        ");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.ConstructorAccess.ToString().ToLower()));
                this.Write(" ");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
                this.Write("(");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.GetCustomConstructorParametersString(CommentUtilities.Indentation.TabSizeInSpaces * 3)));
                this.Write(")\r\n        {\r\n            this.propertyContainer = new PropertyContainer();\r\n");

                foreach (var prop in type.ConstructorProperties)
                {
                    if (!prop.SkipPropertyAssignment)
                    {
                        this.Write("            this.");
                        this.Write(this.ToStringHelper.ToStringWithCulture(prop.Name));
                        this.Write(" = ");
                        this.Write(this.ToStringHelper.ToStringWithCulture(prop.CamelName));
                        this.Write(";\r\n");
                    }
                }
                foreach (var statement in type.AdditionalPropertyInitializationStatements())
                {
                    this.Write("            ");
                    this.Write(this.ToStringHelper.ToStringWithCulture(statement));
                    this.Write("\r\n");
                }

                this.Write("        }\r\n\r\n");
            }

            if (type.ShouldDefineMockableConstructor)
            {
                this.Write("        /// <summary>\r\n        /// Default constructor to support mocking the <see cref=\"");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
                this.Write("\"/> class.\r\n        /// </summary>\r\n");

                this.Write("        protected ");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
                this.Write("()\r\n        {\r\n            this.propertyContainer = new PropertyContainer();\r\n");

                foreach (var statement in type.AdditionalPropertyInitializationStatements(new List <string> {
                    "baseBehaviors"
                }))
                {
                    this.Write("            ");
                    this.Write(this.ToStringHelper.ToStringWithCulture(statement));
                    this.Write("\r\n");
                }

                this.Write("        }\r\n\r\n");
            }

            if (type.BoundProperties.Any())
            {
                this.Write("        internal ");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
                this.Write("(");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.GetBoundConstructorParameterString(CommentUtilities.Indentation.TabSizeInSpaces * 3)));
                this.Write(")\r\n        {\r\n");

                if (type.IsTopLevelObject)
                {
                    foreach (var parentPathVariable in type.ParentPathVariables)
                    {
                        this.Write("            this.");
                        this.Write(this.ToStringHelper.ToStringWithCulture(parentPathVariable));
                        this.Write(" = ");
                        this.Write(this.ToStringHelper.ToStringWithCulture(parentPathVariable));
                        this.Write(";\r\n");
                    }
                    //TODO: This is bad since we are assuming we have defined a param of parentBatchClient

                    this.Write("            this.parentBatchClient = parentBatchClient;\r\n");

                    foreach (var statement in type.AdditionalPropertyInitializationStatements())
                    {
                        this.Write("            ");
                        this.Write(this.ToStringHelper.ToStringWithCulture(statement));
                        this.Write("\r\n");
                    }
                }

                this.Write("            this.propertyContainer = new PropertyContainer(protocolObject);\r\n    " +
                           "    }\r\n");
            }

            this.Write("\r\n        #endregion Constructors\r\n\r\n");

            if (type.IsTopLevelObject)
            {
                this.Write("        #region IInheritedBehaviors\r\n\r\n        /// <summary>\r\n        /// Gets or" +
                           " sets a list of behaviors that modify or customize requests to the Batch service" +
                           "\r\n        /// made via this <see cref=\"");
                this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
                this.Write(@"""/>.
        /// </summary>
        /// <remarks>
        /// <para>These behaviors are inherited by child objects.</para>
        /// <para>Modifications are applied in the order of the collection. The last write wins.</para>
        /// </remarks>
        public IList<BatchClientBehavior> CustomBehaviors { get; set; }

        #endregion IInheritedBehaviors

");
            }

            this.Write("        #region ");
            this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
            this.Write("\r\n\r\n");

            foreach (var property in type.OrderedPublicProperties.Select(p => p.Key))
            {
                var propertyValueAccessor = "this.propertyContainer." + property.PropertyContainerPropertyName + ".Value";

                this.Write("        /// <summary>\r\n");
                this.Write(this.ToStringHelper.ToStringWithCulture(CommentUtilities.FormatTripleSlashComment(property.FullSummaryComment, CommentUtilities.Indentation.TypeMemberLevel)));
                this.Write("\r\n        /// </summary>\r\n");

                if (!string.IsNullOrEmpty(property.RemarksComment))
                {
                    this.Write("        /// <remarks>\r\n");
                    this.Write(this.ToStringHelper.ToStringWithCulture(CommentUtilities.FormatTripleSlashComment(property.RemarksComment, CommentUtilities.Indentation.TypeMemberLevel)));
                    this.Write("\r\n        /// </remarks>\r\n");
                }

                this.Write("        public ");
                this.Write(this.ToStringHelper.ToStringWithCulture(property.Type));
                this.Write(" ");
                this.Write(this.ToStringHelper.ToStringWithCulture(property.Name));
                this.Write("\r\n        {\r\n            get { return ");
                this.Write(this.ToStringHelper.ToStringWithCulture(propertyValueAccessor));
                this.Write("; }\r\n");

                if (property.IsWritable)
                {
                    var setterVisibility = property.HasPublicSetter ? "" : "private ";
                    if (!property.IsTypeCollection)
                    {
                        this.Write("            ");
                        this.Write(this.ToStringHelper.ToStringWithCulture(setterVisibility));
                        this.Write("set { ");
                        this.Write(this.ToStringHelper.ToStringWithCulture(propertyValueAccessor));
                        this.Write(" = value; }\r\n");
                    }
                    else
                    {
                        this.Write("            ");
                        this.Write(this.ToStringHelper.ToStringWithCulture(setterVisibility));
                        this.Write("set\r\n            {\r\n                ");
                        this.Write(this.ToStringHelper.ToStringWithCulture(propertyValueAccessor));
                        this.Write(" = ");
                        this.Write(this.ToStringHelper.ToStringWithCulture(property.GetCollectionSetterString()));
                        this.Write(";\r\n            }\r\n");
                    }
                }

                this.Write("        }\r\n\r\n");
            }

            this.Write("        #endregion // ");
            this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
            this.Write(@"

        #region IPropertyMetadata

        bool IModifiable.HasBeenModified
        {
            get { return this.propertyContainer.HasBeenModified; }
        }

        bool IReadOnly.IsReadOnly
        {
            get { return this.propertyContainer.IsReadOnly; }
            set { this.propertyContainer.IsReadOnly = value; }
        }

        #endregion //IPropertyMetadata

");

            string transportObjectText      = (new GetTransportObjectSnippet(type)).TransformText();
            string collectionConversionText = (new CollectionConversionSnippet(type)).TransformText();

            if (!string.IsNullOrEmpty(transportObjectText) || !string.IsNullOrEmpty(collectionConversionText))
            {
                this.Write("        #region Internal/private methods\r\n");
                this.Write(this.ToStringHelper.ToStringWithCulture(transportObjectText));
                this.Write("\r\n");
                this.Write(this.ToStringHelper.ToStringWithCulture(collectionConversionText));
                this.Write("\r\n        #endregion // Internal/private methods\r\n");
            }

            this.Write("    }");
            return(this.GenerationEnvironment.ToString());
        }