Esempio n. 1
0
        private CodeTypeDeclaration GenerateClass(TableDef table)
        {
            var singular = this.GetClassName(table.Name);
            var cls      = new CodeTypeDeclaration(singular);

            //クラスの属性作成
            cls.CustomAttributes.Add(new CodeAttributeDeclaration("Serializable"));
            if (this.UseWCF)
            {
                cls.CustomAttributes.Add(new CodeAttributeDeclaration("DataContract"));
            }

            var attribute = new CodeAttributeDeclaration("DbTable");

            attribute.Arguments.Add(new CodeAttributeArgument("Name", new CodePrimitiveExpression(table.Name)));
            attribute.Arguments.Add(new CodeAttributeArgument("DatabaseType", new CodeSnippetExpression("DatabaseType." + this.db.ToString())));
            attribute.Arguments.Add(new CodeAttributeArgument("ConnectionSettingKeyName", new CodePrimitiveExpression(this.ConnectionSettingKeyName)));
            cls.CustomAttributes.Add(attribute);

            cls.BaseTypes.Add("DataEntity<" + singular + ">");
            cls.IsPartial = true;
            cls.IsClass   = true;

            foreach (ColumnDef column in table.Columns)
            {
                cls.Members.Add(this.GenerateField(column));
                cls.Members.Add(this.GenerateProperty(table, column));
            }

            if (table.Columns.Exists(p => p.IsPrimaryKey))
            {
                cls.Members.Add(this.GenerateGetMethod(table));
                cls.Members.Add(this.GenerateGetAsyncMethod(table));
            }

            // Get Foreign Keys
            var parentForeignKeys = this.ForeignKeys.FindAll(f => f.TableName == table.Name);

            foreach (var parentForeignKey in parentForeignKeys)
            {
                var parentClassName = this.GetClassName(parentForeignKey.ReferencedTableName);
                if (AlreadyAdded(cls.Members, parentClassName))
                {
                    continue;
                }
                cls.Members.Add(this.GenerateParentField(parentClassName));
                cls.Members.Add(this.GenerateParentProperty(parentClassName, parentForeignKey));
            }

            var childrenForeignKeys = this.ForeignKeys.FindAll(f => f.ReferencedTableName == table.Name);

            foreach (var childrenForeignKey in childrenForeignKeys)
            {
                if (AlreadyAdded(cls.Members, childrenForeignKey.TableName))
                {
                    continue;
                }

                cls.Members.Add(this.GenerateChildrenField(childrenForeignKey.TableName));
                cls.Members.Add(this.GenerateChildrenProperty(childrenForeignKey.TableName, childrenForeignKey));
            }

            return(cls);
        }
Esempio n. 2
0
 /// <summary>
 /// クラス名+cs
 /// </summary>
 /// <param name="table"></param>
 /// <returns></returns>
 public string GetFileName(TableDef table)
 {
     return(this.GetClassName(table.Name) + ".cs");
 }