예제 #1
0
        public void RenderProperty(ConsoleStringBuilderInterlaced sb, bool generateConfigMethod,
                                   bool notifyPropertysChanged)
        {
            var memberName = "_" + Char.ToLowerInvariant(Name[0]) + Name.Substring(1);

            if (notifyPropertysChanged)
            {
                sb.AppendInterlacedLine(
                    $"private {Type.GetTypeName()} {memberName};");
            }

            if (!generateConfigMethod)
            {
                foreach (var attributeInfo in Attributes)
                {
                    attributeInfo.Render(sb);
                }
            }
            else
            {
                foreach (var attributeInfo in Attributes.Where(e => e.DoesNotSupportDbConfigApi))
                {
                    attributeInfo.Render(sb);
                }
            }

            sb.AppendInterlaced("public ");
            if (ForeignKey != null)
            {
                sb.Append("virtual ");
            }

            sb.Append($"{Type.GetTypeName()}");
            sb.Append($" {Name} ");
            if (!notifyPropertysChanged)
            {
                sb.AppendLine($" {{ get; set; }}");
            }
            else
            {
                sb.AppendLine($"");
                sb.AppendInterlacedLine("{")
                .Up()
                .AppendInterlacedLine($"get {{ return {memberName}; }}")
                .AppendInterlacedLine("set")
                .AppendInterlacedLine("{")
                .Up()
                .AppendInterlacedLine($"{memberName} = value;")
                .AppendInterlacedLine("SendPropertyChanged();")
                .Down()
                .AppendInterlacedLine("}")
                .Down()
                .AppendInterlacedLine("}");
            }
        }
예제 #2
0
        public void Render(ConsoleStringBuilderInterlaced sb)
        {
            sb.AppendInterlaced("[")
            .Append($"{Name}")
            .Append("(");

            var result = RenderAssignments();

            sb.Append(result).AppendLine(")]");
        }
예제 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public string RenderPocoClass(bool notifyPropertysChanged = false)
        {
            var sb = new ConsoleStringBuilderInterlaced();

            if (GenerateConfigMethod)
            {
                NamespaceImports.Add(typeof(ConfigurationResolver <>).Namespace);
            }

            if (notifyPropertysChanged)
            {
                NamespaceImports.Add(typeof(INotifyPropertyChanged).Namespace);
                NamespaceImports.Add(typeof(CallerMemberNameAttribute).Namespace);
            }

            NamespaceImports.Add(typeof(EagarDataRecord).Namespace);
            NamespaceImports.Add(typeof(GeneratedCodeAttribute).Namespace);
            NamespaceImports.Add(typeof(DbCollection <>).Namespace);

            sb.AppendInterlacedLine("//------------------------------------------------------------------------------");
            sb.AppendInterlacedLine("// <auto-generated>");
            sb.AppendInterlacedLine("// This code was generated by a tool.");
            sb.AppendInterlacedLine("// Runtime Version:2.0.50727.42");
            sb.AppendInterlacedLine("// Changes to this file may cause incorrect behavior and will be lost if");
            sb.AppendInterlacedLine("// the code is regenerated.");
            sb.AppendInterlacedLine("// </auto-generated>");
            sb.AppendInterlacedLine("//------------------------------------------------------------------------------");


            foreach (var namespaceImport in NamespaceImports)
            {
                sb.AppendLine($"using {namespaceImport};");
            }

            sb.AppendLine();
            sb.AppendLine("namespace " + Namespace);
            sb.AppendLine("{");
            sb.Up();
            sb.AppendLine();
            foreach (var headerLine in CompilerHeader)
            {
                sb.AppendInterlacedLine($"//{headerLine}");
            }


            sb.AppendLine();
            if (!GenerateConfigMethod)
            {
                foreach (var attributeInfo in Attributes)
                {
                    attributeInfo.Render(sb);
                }
            }
            if (GeneratedCodeAttribute != null)
            {
                sb.AppendInterlacedLine(
                    $"[{nameof(GeneratedCodeAttribute)}(tool: \"{GeneratedCodeAttribute.Tool}\", version: \"{GeneratedCodeAttribute.Version}\")]");
            }

            sb.AppendInterlaced("public partial class " + ClassName);
            if (!string.IsNullOrWhiteSpace(Inherts) || notifyPropertysChanged)
            {
                sb.Append($": {Inherts}");
                if (notifyPropertysChanged)
                {
                    if (!string.IsNullOrWhiteSpace(Inherts))
                    {
                        sb.Append(", ");
                    }
                    sb.Append(nameof(INotifyPropertyChanged));
                }
            }

            sb.AppendLine();
            sb.AppendInterlacedLine("{");
            sb.Up();
            sb.AppendInterlacedLine($"public {ClassName}() {{}}");

            if (GenerateConstructor && !GenerateFactory)
            {
                var readerName = "reader";
                sb.AppendInterlacedLine($"public {ClassName}({nameof(EagarDataRecord)} {readerName}) ");
                sb.AppendInterlacedLine("{").Up();

                foreach (var propertyInfo in Properties)
                {
                    propertyInfo.RenderAssignment(sb, readerName, "this");
                }

                sb.Down().AppendInterlacedLine("}");
            }

            if (!IsSuperClass)
            {
                foreach (var propertyInfo in Properties)
                {
                    propertyInfo.RenderProperty(sb, GenerateConfigMethod, notifyPropertysChanged);
                }
            }

            if (GenerateFactory)
            {
                var readerName = "reader";
                sb.AppendInterlacedLine($"public static {ClassName} Factory({nameof(EagarDataRecord)} {readerName})");
                sb.AppendInterlacedLine("{").Up();
                var targetName = "super";
                sb.AppendInterlacedLine($"var {targetName} = new {ClassName}();");

                foreach (var propertyInfo in Properties)
                {
                    propertyInfo.RenderAssignment(sb, readerName, targetName);
                }

                sb.AppendInterlacedLine($"return {targetName};");

                sb.Down().AppendInterlacedLine("}");
            }

            if (GenerateConfigMethod)
            {
                var      configArgument = string.Format("ConfigurationResolver<{0}>", ClassName);
                var      configName     = "config";
                string[] eventNames     =
                {
                    "BeforeConfig()", "AfterConfig()", $"BeforeConfig({configArgument} {configName})",
                    $"AfterConfig({configArgument} {configName})"
                };

                foreach (var eventName in eventNames)
                {
                    sb.AppendInterlacedLine($"static partial void {eventName};");
                }

                sb.AppendInterlacedLine($"[{nameof(ConfigMehtodAttribute)}]");
                sb.AppendInterlacedLine($"public static void Configuration({configArgument} {configName})");
                sb.AppendInterlacedLine("{");
                sb.Up();
                sb.AppendInterlacedLine("BeforeConfig();");
                sb.AppendInterlacedLine($"BeforeConfig({configName});");

                if (!GenerateConstructor)
                {
                    sb.AppendInterlacedLine(
                        $"config.{nameof(ConfigurationResolver<object>.SetFactory)}" +
                        "(" +
                        $"{ClassName}.Factory, " +
                        "true" +
                        ");");
                }

                foreach (var propertyInfoAttribute in Attributes)
                {
                    sb.AppendInterlacedLine(
                        $"config.{nameof(ConfigurationResolver<object>.SetClassAttribute)}" +
                        "(" +
                        $"new {propertyInfoAttribute.Name}({propertyInfoAttribute.RenderAssignments()})" +
                        ");");
                }

                foreach (var propertyInfo in Properties)
                {
                    foreach (var propertyInfoAttribute in propertyInfo.Attributes)
                    {
                        sb.AppendInterlacedLine(
                            $"config.{nameof(ConfigurationResolver<object>.SetPropertyAttribute)}" +
                            "(" +
                            $"s => s.{propertyInfo.Name}" +
                            ", " +
                            $"new {propertyInfoAttribute.Name}({propertyInfoAttribute.RenderAssignments()})" +
                            ");");
                    }
                }

                sb.AppendInterlacedLine($"AfterConfig({configName});");
                sb.AppendInterlacedLine("AfterConfig();");

                sb.Down();
                sb.AppendInterlacedLine("}");
            }

            if (notifyPropertysChanged)
            {
                sb.AppendInterlacedLine($"#region {nameof(INotifyPropertyChanged)}");
                sb.AppendInterlacedLine(
                    $"public event {nameof(PropertyChangedEventHandler)} {nameof(INotifyPropertyChanged.PropertyChanged)};");
                sb.AppendInterlacedLine("");
                sb.AppendInterlacedLine("protected virtual void SendPropertyChanged([CallerMemberName] string propertyName = null)");
                sb.AppendInterlacedLine("{").Up();
                sb.AppendInterlacedLine("PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));");
                sb.Down().AppendInterlacedLine("}");
                sb.AppendInterlacedLine("");
                sb.AppendInterlacedLine("#endregion");
            }

            sb.Down();
            sb.AppendInterlacedLine("}");
            sb.Down();
            sb.AppendLine("}");
            using (var writer = new StringWriter())
            {
                sb.WriteToSteam(writer, wrapper => { }, () => { });
                var stringBuilder = writer.GetStringBuilder();
                return(stringBuilder.ToString());
            }
        }