Пример #1
0
        private void CreateData(XmlClassItem data, GeneratorSettings settings, bool isSerializableItem)
        {
            var code = new CodeFormatter();
            var name = data.name + Constants.DataClassSuffix;

            code.UseNamespace("System", false);
            code.UseNamespace("System.Collections.Generic", false);
            code.UseNamespace("System.Linq", false);
            code.UseNamespace(Constants.TypesNamespace);
            code.UseNamespace(Constants.UtilsNamespace);
            code.UseNamespace(settings.SerializableNamespace);
            code.UseNamespace(settings.EnumsNamespace);
            code.NewLine();
            code.Namespace(settings.ClassesNamespace);
            code.Class(name);

            CommonCodeGenerator.WriteObjectDeserializer(data.name, settings, code);
            code.Add("return new ", name, "(serializable, database);");
            code.CloseBraces();
            code.NewLine();

            CommonCodeGenerator.WriteObjectConsturctor(data.name, settings, code);
            if (isSerializableItem)
            {
                code.Add("ItemId = new ItemId<", name, ">(serializable.Id, serializable.FileName);");
            }
            foreach (var item in data.members)
            {
                CommonCodeGenerator.WriteDeserializationCode(item, code, settings);
            }
            code.CloseBraces();
            code.NewLine();

            CommonCodeGenerator.WriteObjectSerializer(data.name, settings, code);
            code.Add("var serializable = new ", data.name, Constants.SerializableClassSuffix, "();");
            SerializeMembers(data.members, data.typeid, code, isSerializableItem);
            code.Add("return serializable;");
            code.CloseBraces();
            code.NewLine();

            if (isSerializableItem)
            {
                code.Add("public readonly ItemId<", name, "> ItemId;");
            }

            foreach (var item in data.members)
            {
                CommonCodeGenerator.WriteDataMember(item, code, _schema);
            }

            _codeWriter.Write(settings.ClassesNamespace, name, code.ToString());
        }
Пример #2
0
        private void AddContentClasses(string objectName, Dictionary <string, List <XmlClassMember> > members, GeneratorSettings settings, CodeFormatter code)
        {
            var serializableName = CommonCodeGenerator.SerializableClass(objectName);

            code.Add("private interface ", ContentInterfaceName, " { ", serializableName, " ", settings.SerializeMethod, "(); }");
            code.NewLine();

            code.Class(EmptyContentClassName, false, ContentInterfaceName);
            CommonCodeGenerator.WriteObjectSerializer(objectName, settings, code);
            code.Add("return new ", serializableName, "();");
            code.CloseBraces();
            code.CloseBraces();

            foreach (var item in members)
            {
                var className = GetContentClassName(item.Key);
                code.Class(className, false, ContentInterfaceName);

                code.Add("public ", className, "(", serializableName, " serializable, ", settings.DatabaseClass, " database)");
                code.OpenBraces();
                code.Add("if (serializable == null || database == null) return;");
                foreach (var member in item.Value)
                {
                    CommonCodeGenerator.WriteDeserializationCode(member, code, settings);
                }
                code.CloseBraces();
                code.NewLine();

                CommonCodeGenerator.WriteObjectSerializer(objectName, settings, code);
                code.Add("var serializable = new ", serializableName, "();");
                foreach (var member in item.Value)
                {
                    CommonCodeGenerator.WriteSerializationCode(member, code);
                }
                code.Add("return serializable;");
                code.CloseBraces();
                code.NewLine();

                foreach (var member in item.Value)
                {
                    CommonCodeGenerator.WriteDataMember(member, code, _schema);
                }

                code.CloseBraces();
                code.NewLine();
            }
        }
Пример #3
0
        private void CreateMutableData(XmlClassItem data, GeneratorSettings settings, bool isSerializableItem)
        {
            SortMembers(data, out var baseMembers, out var extraMembers);

            var code           = new CodeFormatter();
            var name           = data.name + Constants.DataClassSuffix;
            var switchEnumType = baseMembers.First(item => item.name.Equals(data.switchEnum, StringComparison.Ordinal)).typeid;

            code.UseNamespace("System", false);
            code.UseNamespace("System.Collections.Generic", false);
            code.UseNamespace("System.Linq", false);
            code.UseNamespace(Constants.TypesNamespace);
            code.UseNamespace(Constants.UtilsNamespace);
            code.UseNamespace(settings.SerializableNamespace);
            code.UseNamespace(settings.EnumsNamespace);
            code.NewLine();
            code.Namespace(settings.ClassesNamespace);
            code.Class(name, true, "IDataAdapter");

            CommonCodeGenerator.WriteObjectDeserializer(data.name, settings, code);
            code.Add("var data = new ", name, "(serializable, database); ");
            code.Add("data.CreateContent(serializable, database);");
            code.Add("return data;");
            code.CloseBraces();
            code.NewLine();

            CommonCodeGenerator.WriteObjectConsturctor(data.name, settings, code);
            if (isSerializableItem)
            {
                code.Add("ItemId = new ItemId<", name, ">(serializable.Id, serializable.FileName);");
            }
            foreach (var item in baseMembers)
            {
                CommonCodeGenerator.WriteDeserializationCode(item, code, settings);
            }
            code.CloseBraces();
            code.NewLine();

            code.Add("private void ", CreateContentMethodName, "(", CommonCodeGenerator.SerializableClass(data.name), " serializable, ", settings.DatabaseClass, " database)");
            code.OpenBraces();
            var first = true;

            foreach (var item in extraMembers)
            {
                code.Add(first ? "if (" : "else if (", "serializable.", data.switchEnum, " == ", switchEnumType, ".", item.Key, ")");
                code.Add(Constants.Indent, ContentPropertyName, " = new ", GetContentClassName(item.Key), "(serializable, database);");
                first = false;
            }
            if (!first)
            {
                code.Add("else");
            }
            code.Add(first ? string.Empty : Constants.Indent, ContentPropertyName, " = new ", EmptyContentClassName, "();");
            code.CloseBraces();
            code.NewLine();

            CommonCodeGenerator.WriteObjectSerializer(data.name, settings, code);
            code.Add("var serializable = Content.Serialize();");
            SerializeMembers(baseMembers, data.typeid, code, isSerializableItem);
            code.Add("return serializable;");
            code.CloseBraces();
            code.NewLine();

            AddDataAdapterImplementation(baseMembers, data.switchEnum, code);

            if (isSerializableItem)
            {
                code.Add("public readonly ItemId<", name, "> ItemId;");
            }
            foreach (var item in baseMembers)
            {
                CommonCodeGenerator.WriteDataMember(item, code, _schema);
            }
            code.NewLine();

            AddContentClasses(data.name, extraMembers, settings, code);

            _codeWriter.Write(settings.ClassesNamespace, name, code.ToString());
        }