Example #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();

            ////for enum
            if (ent.IsEnum)
            {
                var elements      = string.Join($",{_nl}", ent.EnumElements.ToArray());
                var flagAttribute = ent.IsFlags ? "[Flags] " : "";
                var enumString    = $"\t{flagAttribute}public enum {ent.Name}{_nl}\t {{{_nl} {elements} {_nl}\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
                ? ReducedBaseTyp(ent) //ent.BaseType
                : PocoSetting.Inherit;

            csTemplate.StartClass(ent.Name, baseClass, partial: true, abstractClass: ent.IsAbstrct);

            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())
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        csTemplate.WriteLine(item);
                    }
                }
                csTemplate.WriteLine(pp.Declaration);

                if (BlankSpaceBeforeProperties)
                {
                    csTemplate.WriteLine(""); //empty line
                }
            }

            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);
        }