public override void GenerateEntities(EntityGeneratorOptions options)
        {
            Trace.WriteLine("Generating Entity Unit Tests");

            code.AppendLine("\nnamespace " + Database.Namespace);
            code.AppendLine("{");

            foreach (var table in Database.Tables)
            {
                code.AppendLine("\t[Microsoft.VisualStudio.TestTools.UnitTesting.TestClass]");
                code.AppendLine("\tpublic class " + table.TableName + "EntityTest");
                code.AppendLine("\t{");

                foreach (var column in table.Columns)
                {
                    GeneratePropertyTest(table, column);

                    if (column.Value.ManagedType.Equals(typeof(string)))
                    {
                        GenerateStringMaxLengthTest(table, column);
                    }
                }

                code.AppendLine("\t}");
                code.AppendLine();
            }

            GenerateRandomGenerator();
            code.AppendLine("}");
        }
Exemplo n.º 2
0
        private void GenerateEntity(Table table, EntityGeneratorOptions options)
        {
            var code = new StringBuilder();

            code.AppendLine("\nnamespace " + Database.DefaultNamespace);
            code.AppendLine("{");
            GenerateXmlDoc(code, 1, "Represents the " + table.ClassName + " table");
            code.AppendLine("\tpublic partial class " + table.ClassName);
            code.AppendLine("\t{");

            // Create backing fields if the type of the field is a string
            foreach (var column in table.Columns)
            {
                if (options.AutoPropertiesOnly)
                {
                    continue;
                }

                if (!(column.Value.MaxLength > 0) || column.Value.ManagedType != typeof(string))
                {
                    continue;
                }

                code.AppendLine("\t\tprivate " + column.Value.ManagedType + (column.Value.ManagedType.IsValueType ? "? _" : " _") + column.Value.FieldName + ";");
            }
            code.AppendLine();

            // Create public constants describing the max length allowed for each string field
            foreach (var column in table.Columns)
            {
                if (options.AutoPropertiesOnly)
                {
                    continue;
                }

                if (!(column.Value.MaxLength > 0) || column.Value.ManagedType != typeof(string))
                {
                    continue;
                }

                GenerateXmlDoc(code, 2, "The Maximum Length the " + column.Value.FieldName + " field allows");
                code.AppendLine("\t\tpublic const int " + column.Value.FieldName + "_Max_Length = " + column.Value.MaxLength + ";");
                code.AppendLine();
            }

            foreach (var column in table.Columns)
            {
                GenerateXmlDoc(code, 2, "Gets or sets the value of " + column.Value.FieldName);

                if (!options.AutoPropertiesOnly && column.Value.MaxLength > 0 && column.Value.ManagedType == typeof(string))
                {
                    // Property with backing field
                    code.AppendLine("\t\tpublic " + column.Value.ManagedType + (column.Value.ManagedType.IsValueType ? "? " : " ") + column.Value.FieldName);
                    code.AppendLine("\t\t{");
                    code.AppendLine("\t\t\tget { return _" + column.Value.FieldName + "; }");
                    code.AppendLine("\t\t\tset");
                    code.AppendLine("\t\t\t{");
                    code.AppendLine("\t\t\t\t_" + column.Value.FieldName + " = value;");

                    if (options.ThrowExceptions)
                    {
                        code.AppendLine("\t\t\t\tif (_" + column.Value.FieldName + " != null && " + column.Value.FieldName + ".Length > " + column.Value.FieldName + "_Max_Length)");
                        code.AppendLine("\t\t\t\t\tthrow new System.ArgumentException(\"Max length for " + column.Value.FieldName + " is " + column.Value.MaxLength + "\");");
                    }

                    code.AppendLine("\t\t\t}");
                    code.AppendLine("\t\t}");
                    code.AppendLine();
                }
                else
                {
                    // Auto property
                    code.AppendLine("\t\tpublic " + column.Value.ManagedType + (column.Value.ManagedType.IsValueType ? "? " : " ") + column.Value.FieldName + " { get; set; }");
                    code.AppendLine();
                }
            }

            code.AppendLine("\t}");
            code.AppendLine("}");

            AppendCode(table.ClassName, code);
        }