protected static void DoOdataVocabulariesMethod(
            StringBuilder sb,
            string mainNamespaceName,
            string subNamespace,
            string className,
            PropertyInfoBase[] properties,
            PropertyInfoBase[] baseClassProperties)
        {
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// Gets the CDSL that defines the OData Vocabularies for this class");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine(
                "        public static " + (baseClassProperties != null ? "new " : string.Empty)
                + "System.Xml.XmlReader GetOdataVocabularies()");
            sb.AppendLine("        {");

            string fullNamespace = mainNamespaceName
                                   + (!string.IsNullOrWhiteSpace(subNamespace) ? "." + subNamespace : null);

            foreach (PropertyInfoBase property in properties)
            {
                sb.AppendLine("            // " + property.Name);
                sb.AppendLine(string.Empty);
            }

            sb.AppendLine("        var schema = new XElement(");
            sb.AppendLine("            \"Schema\",");
            sb.AppendLine("            new XAttribute(\"Namespace\", \"" + fullNamespace + "\"),");
            sb.AppendLine("            new XAttribute(\"xmlns\", \"http://schemas.microsoft.com/ado/2009/11/edm\"),");
            sb.AppendLine("            // using directive");
            sb.AppendLine("            new XElement(");
            sb.AppendLine("                \"Using\",");
            sb.AppendLine("                new XAttribute(\"Namespace\", \"Org.OData.Validation.V1\"),");
            sb.AppendLine("                new XAttribute(\"Alias\", \"Validation\"),");

            string fullClassName = fullNamespace + "." + className;
            if (baseClassProperties != null && baseClassProperties.Any())
            {
                for (int position = 0; position < baseClassProperties.Count() - 1; position++)
                {
                    sb.AppendLine("                new XElement(\"Annotations\",");
                    sb.AppendLine(
                        "                    new XAttribute(\"Target\", \"" + fullClassName + "/"
                        + baseClassProperties[position].Name + "\")),");
                }

                sb.AppendLine("                new XElement(");
                sb.AppendLine("                    \"Annotations\",");
                sb.AppendLine(
                    "                    new XAttribute(\"Target\", \"" + fullClassName + "/"
                    + baseClassProperties[baseClassProperties.Count() - 1].Name + "\")),");
            }

            for (int position = 0; position < properties.Count() - 1; position++)
            {
                sb.AppendLine("                new XElement(");
                sb.AppendLine("                    \"Annotations\",");
                sb.AppendLine(
                    "                    new XAttribute(\"Target\", \"" + fullClassName + "/"
                    + properties[position].Name + "\")),");
            }

            sb.AppendLine("                new XElement(");
            sb.AppendLine("                    \"Annotations\",");
            sb.AppendLine(
                "                    new XAttribute(\"Target\", \"" + fullClassName + "/"
                + properties[properties.Count() - 1].Name + "\"))));");
            sb.AppendLine(string.Empty);
            sb.AppendLine("        Debug.Assert(schema.Document != null, \"schema.Document != null\");");
            sb.AppendLine("        return schema.Document.CreateReader();");
            sb.AppendLine("        }");
        }
Exemple #2
0
        /// <summary>
        /// Entry point for generating the code
        /// </summary>
        /// <param name="mainNamespaceName">
        /// The main namespace
        /// </param>
        /// <param name="subNamespace">
        /// The sub namespace, if any
        /// </param>
        /// <param name="className">
        /// The class name
        /// </param>
        /// <param name="classRemarks">
        /// Remarks for the class
        /// </param>
        /// <param name="properties">
        /// Properties of the info class
        /// </param>
        /// <param name="baseClassName">
        /// The name of the base class
        /// </param>
        /// <param name="baseClassProperties">
        /// The properties of the base class
        /// </param>
        /// <returns>
        /// C# code
        /// </returns>
        public string Generate(
            string mainNamespaceName,
            string subNamespace,
            string className,
            string classRemarks,
            PropertyInfoBase[] properties,
            string baseClassName,
            PropertyInfoBase[] baseClassProperties)
        {
            if (string.IsNullOrWhiteSpace(mainNamespaceName))
            {
                throw new ArgumentNullException("mainNamespaceName");
            }

            if (string.IsNullOrWhiteSpace(className))
            {
                throw new ArgumentNullException("className");
            }

            if (string.IsNullOrWhiteSpace(classRemarks))
            {
                throw new ArgumentNullException("classRemarks");
            }

            if (properties == null)
            {
                throw new ArgumentNullException("properties");
            }

            if (properties.Count(p => p.IsKey) > 1)
            {
                throw new ArgumentException("Too many primary keys defined");
            }

            var sb = new StringBuilder();
            var tabCount = 2;

            sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "namespace {0}.Model.{1}{2}", mainNamespaceName, this.ClassTypeName, (!string.IsNullOrEmpty(subNamespace)) ? "." + subNamespace : null));
            sb.AppendLine("{");
            sb.AppendLine("        using System;");
            sb.AppendLine("        using System.ComponentModel.DataAnnotations;");
            sb.AppendLine("        using System.Data.Entity;");
            sb.AppendLine("        using System.Diagnostics;");
            sb.AppendLine("        using System.Diagnostics.CodeAnalysis;");
            sb.AppendLine("        using System.Runtime.Serialization;");
            sb.AppendLine("        using System.Xml;");
            sb.AppendLine("        using System.Xml.Linq;");
            sb.AppendLine(string.Empty);
            this.DoUnitTestUsingStatements(sb, tabCount);
            sb.AppendLine(string.Empty);
            sb.AppendLine(Helpers.GetAutoGeneratedWarning());
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// " + classRemarks);
            sb.AppendLine("        /// </summary>");
            this.DoUnitTestClassAttributes(sb, tabCount);
            sb.AppendLine("        public class " + className + this.GetClassSuffix() + "Test");
            sb.AppendLine("        {");

            this.DoConstructorMethod(sb, className, properties, baseClassName, baseClassProperties);
            //this.DoDisposeMethod(sb, properties, baseClassName);
            this.DoPropertiesRegion(sb, properties, baseClassProperties);
            //this.DoIComparableRegion(sb, className, properties, baseClassName, baseClassProperties);

            //sb.Append(Helpers.GetIEquatableRegion(className + this.GetClassSuffix(), baseClassName, baseClassProperties));

            //this.DoOurMethodsRegion(sb, mainNamespaceName, subNamespace, className, properties, baseClassName, baseClassProperties);

            sb.AppendLine("    }");
            sb.AppendLine("}");

            return sb.ToString();
        }