public void GeneratePocos(CDBM db, CSharpProject p, CUsing NamespaceName, TextWriter mensajes) { foreach (CTableM t in db.lTable) { p.AddCSharpFile(PocoName(t.Name), NamespaceName); CClass c = p.AddClass(PocoName(t.Name)); c.lUsing.Add(CSharpStandarUsing.System); CConstructor consVacio = new CConstructor(c, CSharpVisibility.cvPublic); CConstructor cons = new CConstructor(c, CSharpVisibility.cvPublic); //Se añaden al final por estética de la clase generada foreach (CFieldM f in t.lFieldAll) { CType aux = CGenerator.SqlServerTD2CSharp(f.Td, f.IsNullable); if (aux.Equals(CSharpPrimitiveType.cDateTime)) { c.lUsing.Add(CSharpStandarUsing.System); } c.AddField(new CField(f.Name, aux, CSharpVisibility.cvPublic)); cons.AddParam(SUtil.LowerFirst(f.Name), CGenerator.SqlServerTD2CSharp(f.Td, f.IsNullable)); cons.AddSentence(new CTextualSentence($"this.{f.Name} = {SUtil.LowerFirst(f.Name)};")); } c.AddConstructor(consVacio); c.AddConstructor(cons); } mensajes.WriteLine("Pocos generados en: " + p.RootDirectory.ActualDirectory.FullName); }
private void GenerateDef(CDBM db, CSharpProject p, string namespaceName, TextWriter mensajes) { string className = DefName(db.Name); p.AddCSharpFile(className + ".generated", namespaceName); CClass c = p.AddClass(className, isStatic: true); c.lUsing.Add(CSharpStandarUsing.System); c.lUsing.Add(new CUsing("IvanCruz.Util.UtilBD")); foreach (CTableM t in db.lTable) { c.AddField(new CField(t.Name, new TextualType(ClassDefName(t.Name)), CSharpVisibility.cvPublic, isStatic: true, InitialValue: " new " + ClassDefName(t.Name) + "()")); //CConstructor consVacio = new CConstructor(c, CSharpVisibility.cvPublic); //CConstructor cons = new CConstructor(c, CSharpVisibility.cvPublic); ////Se añaden al final por estética de la clase generada //foreach (CFieldM f in t.lFieldAll) { // CType aux = CGenerator.SqlServerTD2CSharp(f.Td, f.IsNullable); // if (aux.Equals(CSharpPrimitiveType.cDateTime)) { // c.lUsing.Add(CSharpStandarUsing.System); // } // c.AddField(new CField(f.Name, aux, CSharpVisibility.cvPublic)); // cons.AddParam(SUtil.LowerFirst(f.Name), CGenerator.SqlServerTD2CSharp(f.Td, f.IsNullable)); // cons.AddSentence(new CTextualSentence($"this.{f.Name} = {SUtil.LowerFirst(f.Name)};")); //} //c.AddConstructor(consVacio); //c.AddConstructor(cons); CClass cTable = p.AddClass(ClassDefName(t.Name), isStatic: false); cTable.ParentClassName = "CTableDef"; CProperty prop; prop = new CProperty("TableName", CSharpVisibility.cvPublic, new TextualType("string"), hasGet: true, hasSet: false); prop.Override = true; prop.lSentenceGet.Add(new CTextualSentence("return " + SUtil.DoubleQuote(t.Name) + ";")); cTable.AddProperty(prop); prop = new CProperty("SingularTitle", CSharpVisibility.cvPublic, new TextualType("string"), hasGet: true, hasSet: false); prop.Override = true; prop.lSentenceGet.Add(new CTextualSentence("return " + SUtil.DoubleQuote(t.SingularTitle) + ";")); cTable.AddProperty(prop); CField auxField; StringBuilder sb = new StringBuilder(); prop = new CProperty("Fields", CSharpVisibility.cvPublic, new TextualType("System.Collections.Generic.IEnumerable<CFieldDef>"), hasGet: true, hasSet: false); prop.Override = true; foreach (CFieldM f in t.lFieldAll) { auxField = new CField(f.Name, new TextualType("CFieldDef"), CSharpVisibility.cvPublic, isStatic: false); sb.Clear(); sb.Append("new CFieldDef"); sb.Append("("); sb.Append(SUtil.DoubleQuote(f.Name)); if (!string.IsNullOrWhiteSpace(f.Title)) { sb.Append(","); sb.Append(SUtil.DoubleQuote(f.Title)); } sb.Append(")"); auxField.InitialValue = sb.ToString(); cTable.AddField(auxField); prop.lSentenceGet.Add(new CTextualSentence("yield return " + f.Name + ";")); } cTable.AddProperty(prop); //public override System.Collections.Generic.IEnumerable<CFieldDef> Fields { // get { // yield return Id; // yield return CodigoMedico; // yield return Nomb; // } //} } mensajes.WriteLine("Definición generada en: " + p.RootDirectory.ActualDirectory.FullName); }