Ejemplo n.º 1
0
        public string WriteCode(OpenApiSchema schema, string name, string @namespace)
        {
            Clear();
            GenerateFileHeader();
            WriteLine("using System;");
            WriteLine("using System.Collections.Generic;");
            WriteLine();
            WriteLine($"namespace {@namespace}");
            using (OpenBlock())
            {
                string className = Dependencies.Namer.Entity(name);
                WriteLine($"public partial class {className}");
                using (OpenBlock())
                {
                    WriteLine($"public {className}()");
                    var simpleProperties     = schema.GetSimpleProperties();
                    var navigationProperties = schema.GetNavigationProperties();
                    var allproperties        = schema.GetAllPropertiesRecursive();

                    using (OpenBlock())
                    {
                        foreach (var kvp in navigationProperties)
                        {
                            string n = StringUtilities.MakePascal(kvp.Key);
                            string t = Dependencies.Schema.ConvertToType(kvp.Value);
                            if (!string.IsNullOrEmpty(t) && t.StartsWith("ICollection", StringComparison.CurrentCultureIgnoreCase))
                            {
                                t = t.Replace("ICollection", string.Empty);
                                t = t.TrimStart('<');
                                t = t.TrimEnd('>');
                                WriteLine($"{n} = new HashSet<{t}>();");
                            }
                        }
                    }

                    var primaryKeyTypeName = Dependencies.Document.Options.PrimaryKeyTypeName;

                    WriteLine($"public {primaryKeyTypeName} {className}ID {{ get; set; }}");


                    foreach (var kvp in allproperties)
                    {
                        string n = StringUtilities.MakePascal(kvp.Key);
                        string t = Dependencies.Schema.ConvertToType(kvp.Value);
                        WriteLine();

                        if (n == className)
                        {
                            n = StringUtilities.MakeCamel(n);
                        }

                        WriteLine($"public {t} {n} {{ get; set; }}");
                    }
                }
            }

            return(GetText());
        }