/// <summary> /// Generate C# code for all POCO classes in the model /// </summary> /// <returns></returns> public string GeneratePoco() { List <string> ns = ClassList.Select(x => x.NameSpace).Distinct() .OrderBy(x => x).ToList(); var template = new FluentCsTextTemplate { Header = Header }; template.WriteLine(UsingAssembly(ns)); foreach (var s in ns) { //Use a user supplied namespace prefix combined with the schema namepace or just the schema namespace var namespc = PrefixNamespace(s); template.StartNamespace(namespc); var pocoModel = ClassList.Where(x => x.NameSpace == s); foreach (var item in pocoModel) { template.WriteLine(ClassToString(item)); } template.EndNamespace(); } return(template.ToString()); }
/// <summary> /// Generte C# code for a given Entity using FluentCsTextTemplate /// </summary> /// <param name="ent"> Class to generate code</param> /// <param name="includeNamespace"></param> /// <returns></returns> internal string ClassToString(ClassTemplate ent, bool includeNamespace = false) { var csTemplate = new FluentCsTextTemplate(); if (includeNamespace) { csTemplate.WriteLine(GetHeader()); } ////for enum if (ent.IsEnum) { var elements = string.Join(",\r\n ", ent.EnumElements.ToArray()); var flagAttribute = ent.IsFlags ? "[Flags] " : ""; var enumString = $"\t{flagAttribute}public enum {ent.Name}\r\n\t {{\r\n {elements} \r\n\t}}"; return(enumString); } foreach (var item in ent.GetAllAttributes()) //not depend on pocosetting { csTemplate.PushIndent("\t").WriteLine(item).PopIndent(); } var baseClass = ent.BaseType != null && PocoSetting.UseInheritance ? ent.BaseType : PocoSetting.Inherit; csTemplate.StartClass(ent.Name, baseClass, partial: true); foreach (var p in ent.Properties) { var pp = new PropertyGenerator(p, PocoSetting); if (p.IsNavigate) { if (!PocoSetting.AddNavigation && !PocoSetting.AddEager) { continue; } } foreach (var item in pp.GetAllAttributes()) { csTemplate.WriteLine(item); } csTemplate.WriteLine(pp.Declaration); } csTemplate.EndClass(); if (includeNamespace) { csTemplate.EndNamespace(); //"}" for namespace } CodeText = csTemplate.ToString(); return(CodeText); }
public void class_declare_default_Test() { FluentCsTextTemplate ft = new FluentCsTextTemplate(); string result = ft.StartClass("Circle"); var expected = @" public partial class Circle {"; Assert.That(result.TrimAllSpace(), Does.Contain(expected.TrimAllSpace())); }
public void class_inherit_abstarct_Test() { FluentCsTextTemplate ft = new FluentCsTextTemplate(); string result = ft.StartClass("Circle", inherit: "Shape", abstractClass: true); var expected = @" public abstract partial class Circle : Shape { "; Assert.That(result.TrimAllSpace(), Does.Contain(expected.TrimAllSpace())); }
/// <summary> /// Constructor /// </summary> /// <param name="pocoGen"></param> /// <param name="setting"></param> public PocoClassGeneratorCs(IPocoGenerator pocoGen, PocoSetting setting = null) { PocoSetting = setting ?? new PocoSetting(); _pocoGen = pocoGen; _classDictionary = new Dictionary<string, ClassTemplate>(); Template = new FluentCsTextTemplate(); var list = _pocoGen.GeneratePocoList(); //generate all classes from model if (list != null) foreach (var item in list) _classDictionary[item.Name] = item; //Console.WriteLine("PocoClassGeneratorCs constructor key: {0}", PocoSetting.AddKeyAttribute); CodeText = null; }
// private readonly Func<string> _header = () => // { // var comment = @" ////------------------------------------------------------------------------------ //// <auto-generated> //// This code was generated using OData2Poco Class library. //// Service Url: {0} //// MetaData Version: {1} //// </auto-generated> ////------------------------------------------------------------------------------ //"; // //Use a user supplied namespace or the default if one wasn't supplied // var namespc = _pocoGen.MetaData.SchemaNamespace; // if(!string.IsNullOrWhiteSpace(PocoSetting.Namespace)) // namespc = PocoSetting.Namespace; // var h = new FluentCsTextTemplate(); // h.UsingNamespace("System") // .UsingNamespace("System.Collections.Generic") // .UsingNamespace("System.ComponentModel.DataAnnotations") //for attributes // .UsingNamespace("System.ComponentModel.DataAnnotations.Schema") //for Table attributes // .UsingNamespace("System.IO") // // .UsingNamespace("System.Spatial") // .WriteLineComment("uncomment for spatial data type and run Install-Package System.Spatial ") // .WriteLineComment("using System.Spatial;") // .WriteLine(comment, _pocoGen.MetaData.ServiceUrl, _pocoGen.MetaData.MetaDataVersion) // .StartNamespace(_pocoGen.MetaData.SchemaNamespace); // return h.ToString(); // }; private string GetHeader() { var comment = @"//------------------------------------------------------------------------------ // <auto-generated> // This code was generated using OData2Poco Class library. // Service Url: {0} // MetaData Version: {1} // Generated On: {2} // </auto-generated> //------------------------------------------------------------------------------ "; //Use a user supplied namespace prefix combined with the schema namepace or just the schema namespace var namespc = _pocoGen.MetaData.SchemaNamespace; if (!string.IsNullOrWhiteSpace(PocoSetting.NamespacePrefix)) { namespc = (PocoSetting.NamespacePrefix + "." + _pocoGen.MetaData.SchemaNamespace).Replace("..", "."); namespc = namespc.TrimEnd('.'); } //Ensure the <auto-generated> tag is at the start of the file, and enclose all usings in a namespace var h = new FluentCsTextTemplate(); h.WriteLine(comment, _pocoGen.MetaData.ServiceUrl, _pocoGen.MetaData.MetaDataVersion, DateTimeOffset.Now.ToString("s")) .StartNamespace(namespc) .UsingNamespace("System") .UsingNamespace("System.Collections.Generic"); //Add required namespace for [Key] attributes if (PocoSetting.AddKeyAttribute) { h.UsingNamespace("System.ComponentModel.DataAnnotations"); } //Add required namespace for [Table] attributes if (PocoSetting.AddTableAttribute) { h.UsingNamespace("System.ComponentModel.DataAnnotations.Schema"); } //Add remaining namespaces and comments h.UsingNamespace("System.IO") // .UsingNamespace("System.Spatial") .WriteLineComment("uncomment for spatial data type and run Install-Package System.Spatial ") .WriteLineComment("using System.Spatial;"); return(h.ToString()); }
/// <summary> /// Constructor /// </summary> /// <param name="pocoGen"></param> /// <param name="setting"></param> public PocoClassGeneratorCs(IPocoGenerator pocoGen, PocoSetting setting = null) { PocoSetting = setting ?? new PocoSetting(); _pocoGen = pocoGen; _classDictionary = new Dictionary <string, ClassTemplate>(); Template = new FluentCsTextTemplate(); var list = _pocoGen.GeneratePocoList(); //generate all classes from model if (list != null) { foreach (var item in list) { _classDictionary[item.Name] = item; } } //Console.WriteLine("PocoClassGeneratorCs constructor key: {0}", PocoSetting.AddKeyAttribute); CodeText = null; }
public void GenerateDummyClassTest() { var code = @"using System; using System.Text; namespace Northwind.Data { public class Northwind { public int age {get;set;} virtual public double Price {get;set;} //Price of product [key] [Required] public string StartDate {get;set;} } } "; var template = new FluentCsTextTemplate(); var name = "TestClass"; var text = ""; var result = template //using namespace .UsingNamespace("System") .UsingNamespace("System.Text") .NewLine() //namespace .StartNamespace("Northwind.Data") //class name .StartClass("Northwind") .WriteLineProperty("int", "age") .WriteLineProperty("double", "Price", isVirtual: true, comment: "Price of product") .WriteLineAttribute("key") .WriteLineAttribute("Required") .WriteLineProperty("string", "StartDate") .EndClass() .EndNamespace() .ToString(); //Console.WriteLine(result); var compRegex = new StringCompIgnoreWhiteSpaceRegex(); var flag = compRegex.Equals(code, result); // StringAssert.Contains(code.Trim(), result.Trim()); Assert.IsTrue(flag); }
public void GenerateDummyClassTest() { var code = @"using System; using System.Text; namespace Northwind.Data { public class Northwind { public int age {get;set;} virtual public double Price {get;set;} //Price of product [key] [Required] public string StartDate {get;set;} } } "; var template = new FluentCsTextTemplate(); var name = "TestClass"; var text = ""; var result = template //using namespace .UsingNamespace("System") .UsingNamespace("System.Text") .NewLine() //namespace .StartNamespace("Northwind.Data") //class name .StartClass("Northwind") .WriteLineProperty("int", "age") .WriteLineProperty("double", "Price", isVirtual: true, comment: "Price of product") .WriteLineAttribute("key") .WriteLineAttribute("Required") .WriteLineProperty("string", "StartDate") .EndClass() .EndNamespace() .ToString(); //Console.WriteLine(result); var compRegex = new StringCompIgnoreWhiteSpaceRegex(); var flag= compRegex.Equals(code, result); // StringAssert.Contains(code.Trim(), result.Trim()); Assert.IsTrue(flag); }
private string GetHeader() { var comment = @"//------------------------------------------------------------------------------ // <auto-generated> // This code was generated using OData2Poco System. // Service Url: {0} // MetaData Version: {1} // Generated On: {2} // </auto-generated> //------------------------------------------------------------------------------ //"; //The <auto-generated> tag at the start of the file var h = new FluentCsTextTemplate(); h.WriteLine(comment, _pocoGen.MetaData.ServiceUrl, _pocoGen.MetaData.MetaDataVersion, DateTimeOffset.Now.ToString("s")); return(h.ToString()); }
private string UsingAssembly(List <string> nameSpaces) { var h = new FluentCsTextTemplate(); var assemblyManager = new AssemplyManager(PocoSetting, ClassList); var asemplyList = assemblyManager.AssemplyReference; foreach (var entry in asemplyList) { h.UsingNamespace(entry); } //add also namespaces of the built-in schema namespaces if (nameSpaces.Count > 1) { nameSpaces.ForEach(x => { var namespc = PrefixNamespace(x); h.UsingNamespace(namespc); }); } return(h.ToString()); }
private string GetHeader() { var comment = @"//------------------------------------------------------------------------------ // <auto-generated> // This code was generated using OData2Poco Class library. // Service Url: {0} // MetaData Version: {1} // Generated On: {2} // </auto-generated> //------------------------------------------------------------------------------ "; //Use a user supplied namespace prefix combined with the schema namepace or just the schema namespace var namespc = _pocoGen.MetaData.SchemaNamespace; if (!string.IsNullOrWhiteSpace(PocoSetting.NamespacePrefix)) { namespc = (PocoSetting.NamespacePrefix + "." + _pocoGen.MetaData.SchemaNamespace).Replace("..", "."); namespc = namespc.TrimEnd('.'); } //Ensure the <auto-generated> tag is at the start of the file, and enclose all usings in a namespace var h = new FluentCsTextTemplate(); h.WriteLine(comment, _pocoGen.MetaData.ServiceUrl, _pocoGen.MetaData.MetaDataVersion, DateTimeOffset.Now.ToString("s")) .StartNamespace(namespc); //.UsingNamespace("System") //.UsingNamespace("System.Collections.Generic"); var assemplyManager = new AssemplyManager(PocoSetting, PocoModel); var asemplyList = assemplyManager.AssemplyReference; foreach (var entry in asemplyList) { h.UsingNamespace(entry); } return(h.ToString()); }
/// <summary> /// Constructor /// </summary> /// <param name="pocoGen"></param> /// <param name="setting"></param> public PocoClassGeneratorCs(IPocoGenerator pocoGen, PocoSetting setting = null) { PocoSetting = setting ?? new PocoSetting(); //initialize AttributeFactory to use pocosetting.Attributes AttributeFactory.Default.Init(PocoSetting); _pocoGen = pocoGen; PocoModel = new Dictionary <string, ClassTemplate>(); Template = new FluentCsTextTemplate(); var list = _pocoGen.GeneratePocoList(); //generate all classes from model //check reserved keywords ModelManager.RenameClasses(list); if (list != null) { foreach (var item in list) { PocoModel[item.Name] = item; } } CodeText = null; }
/// <summary> /// Generte C# code for a given Entity using FluentCsTextTemplate /// </summary> /// <param name="ent"> Class to generate code</param> /// <param name="includeNamespace"></param> /// <returns></returns> internal string ClassToString(ClassTemplate ent, bool includeNamespace = false) { var csTemplate = new FluentCsTextTemplate(); if (includeNamespace) { csTemplate.WriteLine(GetHeader()); } ////for enum if (ent.IsEnum) { var elements = string.Join(", ", ent.EnumElements.ToArray()); var enumString = string.Format("public enum {0} {{ {1} }}", ent.Name, elements); return(enumString); } //v 2.2 foreach (var item in ent.GetAttributes(PocoSetting)) { csTemplate.PushIndent("\t").WriteLine(item).PopIndent(); } csTemplate.StartClass(ent.Name, PocoSetting.Inherit); // csTemplate.StartClass(ent.Name, PocoSetting.Inherit, partial:true); //delayed to a future release to avoid change of most test cases foreach (var p in ent.Properties) { var pp = new PropertyGenerator(p, PocoSetting); //@@@ v1.0.0-rc3 // navigation properties //v1.4 skip //if (p.IsNavigate) continue; //v1.5 if (p.IsNavigate) { //Console.WriteLine("navigation entity {0} prop: {1}",ent.Name, p.PropName); if (!PocoSetting.AddNavigation && !PocoSetting.AddEager) { continue; } } foreach (var item in pp.GetAllAttributes()) { csTemplate.WriteLine(item); } csTemplate.WriteLine(pp.Declaration); //Console.WriteLine(pp.ToString()); } csTemplate.EndClass(); if (includeNamespace) { csTemplate.EndNamespace(); //"}" for namespace } CodeText = csTemplate.ToString(); return(CodeText); }
////v1.4 ////TODO: find other implementation for multifile to avoid change break in API ///// <summary> ///// Generate entry for every class to be written in separate file ///// </summary> ///// <returns></returns> //private Dictionary<string, string> GeneratePocoMultiFile() //{ // Dictionary<string, string> codes = new Dictionary<string, string>(); // foreach (var item in _classDictionary) // { // var template = new FluentCsTextTemplate(); // template.WriteLine(_header()); //header of the file (using xxx;....) // template.WriteLine(ClassToString(item.Value)); //c# code of the class // template.EndNamespace(); //"}" for namespace // codes[item.Key] = template.ToString(); // } // return codes; //} /// <summary> /// Generte C# code for a given Entity using FluentCsTextTemplate /// </summary> /// <param name="ent"> Class to generate code</param> /// <param name="includeNamespace"></param> /// <returns></returns> internal string ClassToString(ClassTemplate ent, bool includeNamespace = false) { var csTemplate = new FluentCsTextTemplate(); if (includeNamespace) csTemplate.WriteLine(_header()); ////for enum if (ent.IsEnum) { var elements = string.Join(", ", ent.EnumElements.ToArray()); var enumString = string.Format("public enum {0} {{ {1} }}", ent.Name, elements); return enumString; } //v1.4 //add TableAttribute if (PocoSetting.AddTableAttribute) { if (ent.EntitySetName != "") { var tableAtt = string.Format("Table(\"{0}\")", ent.EntitySetName); csTemplate.PushIndent("\t").WriteLineAttribute(tableAtt).PopIndent(); } } csTemplate.StartClass(ent.Name); foreach (var p in ent.Properties) { //@@@ v1.0.0-rc3 // navigation properties //v1.4 skip //if (p.IsNavigate) continue; //v1.5 if (p.IsNavigate) { //Console.WriteLine("navigation entity {0} prop: {1}",ent.Name, p.PropName); if (!PocoSetting.AddNavigation) continue; } //v1.4 //add key attributes if (PocoSetting.AddKeyAttribute) { if (p.IsKey) csTemplate.WriteLineAttribute("Key"); } if (PocoSetting.AddRequiredAttribute) { if (!p.IsNullable) csTemplate.WriteLineAttribute("Required"); } //set nullable data types var nulType = PocoSetting.AddNullableDataType && p.IsNullable ? Helper.GetNullable(p.PropType) : ""; // if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) var virtualprop = (p.IsNavigate && PocoSetting.AddNavigation); csTemplate.WriteLineProperty(p.PropType + nulType, p.PropName, comment: p.PropComment, isVirtual: virtualprop); } csTemplate.EndClass(); if (includeNamespace) csTemplate.EndNamespace(); //"}" for namespace CodeText = csTemplate.ToString(); return CodeText; }
////v1.4 ////TODO: find other implementation for multifile to avoid change break in API ///// <summary> ///// Generate entry for every class to be written in separate file ///// </summary> ///// <returns></returns> //private Dictionary<string, string> GeneratePocoMultiFile() //{ // Dictionary<string, string> codes = new Dictionary<string, string>(); // foreach (var item in _classDictionary) // { // var template = new FluentCsTextTemplate(); // template.WriteLine(_header()); //header of the file (using xxx;....) // template.WriteLine(ClassToString(item.Value)); //c# code of the class // template.EndNamespace(); //"}" for namespace // codes[item.Key] = template.ToString(); // } // return codes; //} /// <summary> /// Generte C# code for a given Entity using FluentCsTextTemplate /// </summary> /// <param name="ent"> Class to generate code</param> /// <param name="includeNamespace"></param> /// <returns></returns> internal string ClassToString(ClassTemplate ent, bool includeNamespace = false) { var csTemplate = new FluentCsTextTemplate(); if (includeNamespace) { csTemplate.WriteLine(GetHeader()); } ////for enum if (ent.IsEnum) { var elements = string.Join(", ", ent.EnumElements.ToArray()); var enumString = string.Format("public enum {0} {{ {1} }}", ent.Name, elements); return(enumString); } //v1.4 //add TableAttribute if (PocoSetting.AddTableAttribute) { if (ent.EntitySetName != "") { var tableAtt = string.Format("Table(\"{0}\")", ent.EntitySetName); csTemplate.PushIndent("\t").WriteLineAttribute(tableAtt).PopIndent(); } } csTemplate.StartClass(ent.Name, PocoSetting.Inherit); foreach (var p in ent.Properties) { //@@@ v1.0.0-rc3 // navigation properties //v1.4 skip //if (p.IsNavigate) continue; //v1.5 if (p.IsNavigate) { //Console.WriteLine("navigation entity {0} prop: {1}",ent.Name, p.PropName); if (!PocoSetting.AddNavigation && !PocoSetting.AddEager) { continue; } } //v1.4 //add key attributes if (PocoSetting.AddKeyAttribute) { if (p.IsKey) { csTemplate.WriteLineAttribute("Key"); } } if (PocoSetting.AddRequiredAttribute) { if (!p.IsNullable) { csTemplate.WriteLineAttribute("Required"); } } //set nullable data types var nulType = PocoSetting.AddNullableDataType && p.IsNullable ? Helper.GetNullable(p.PropType) : ""; // if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) var virtualprop = (PocoSetting.AddNavigation && !PocoSetting.AddEager); //var virtualprop = (PocoSetting.AddNavigation); csTemplate.WriteLineProperty(p.PropType + nulType, p.PropName, comment: p.PropComment, isVirtual: virtualprop); } csTemplate.EndClass(); if (includeNamespace) { csTemplate.EndNamespace(); //"}" for namespace } CodeText = csTemplate.ToString(); return(CodeText); }