Exemplo n.º 1
0
        /// <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());
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Generate C# code for all POCO classes in the model
 /// </summary>
 /// <returns></returns>
 public string GeneratePoco()
 {
     //   ClassList = _pocoGen.GeneratePocoList(); //generate all classes from model
     _template.WriteLine(_header()); //header of the file (using xxx;....)
     Console.WriteLine("wwwwwww before loop GeneratePoco call ClassToString key: {0}", PocoSetting.AddKeyAttribute);
     foreach (var item in _classDictionary)
     {
         Console.WriteLine("yyyyyy GeneratePoco call ClassToString key: {0}", PocoSetting.AddKeyAttribute);
         _template.WriteLine(ClassToString(item.Value)); //c# code of the class
     }
     _template.EndNamespace();                           //"}" for namespace
     return(_template.ToString());
 }
//        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());
        }
Exemplo n.º 5
0
        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());
        }
Exemplo n.º 6
0
        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>
        ///     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);
        }
Exemplo n.º 9
0
        ////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;
        }
Exemplo n.º 10
0
        ////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);
        }