示例#1
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);
        }
        /// <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);
        }