public CodeCompileUnit GetCompileUnit(string className) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnitWithInheritanceAndInterface(nameSpace, className, appPrefs.InheritenceAndInterfaces); var mapper = new DataTypeMapper(); var newType = compileUnit.Namespaces[0].Types[0]; newType.IsPartial = appPrefs.GeneratePartialClasses; CreateProperties(codeGenerationHelper, mapper, newType); var constructorStatements = new CodeStatementCollection(); foreach (var hasMany in Table.HasManyRelationships) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(appPrefs.ForeignEntityCollectionType + "<" + appPrefs.ClassNamePrefix + Formatter.FormatSingular(hasMany.Reference) + ">", "Lista" + Formatter.FormatSingular(hasMany.Reference), appPrefs.UseLazy)); //constructorStatements.Add(new CodeSnippetStatement(string.Format(TABS + "{0} = new {1}<{2}{3}>();", Formatter.FormatPlural(hasMany.Reference), codeGenerationHelper.InstatiationObject(appPrefs.ForeignEntityCollectionType), appPrefs.ClassNamePrefix, Formatter.FormatSingular(hasMany.Reference)))); } var constructor = new CodeConstructor { Attributes = MemberAttributes.Public }; constructor.Statements.AddRange(constructorStatements); newType.Members.Add(constructor); return(compileUnit); }
public CodeCompileUnit GetCompileUnit(string className) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnitWithInheritanceAndInterface(nameSpace, className, appPrefs.InheritenceAndInterfaces); var mapper = new DataTypeMapper(); var newType = compileUnit.Namespaces[0].Types[0]; newType.IsPartial = appPrefs.GeneratePartialClasses; CreateProperties(codeGenerationHelper, mapper, newType); // Generate GetHashCode() and Equals() methods. if (Table.PrimaryKey != null && Table.PrimaryKey.Columns.Count != 0 && Table.PrimaryKey.Type == PrimaryKeyType.CompositeKey) { var pkColsList = Table.PrimaryKey.Columns.Select(s => Formatter.FormatText(s.Name)).ToList(); var equalsCode = CreateCompositeKeyEqualsMethod(pkColsList); var getHashKeyCode = CreateCompositeKeyGetHashKeyMethod(pkColsList); equalsCode.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "NHibernate Composite Key Requirements")); newType.Members.Add(equalsCode); newType.Members.Add(getHashKeyCode); getHashKeyCode.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty)); } // Dont create a constructor if there are no relationships. if (Table.HasManyRelationships.Count == 0) { return(compileUnit); } var pascalCaseTextFormatter = new PascalCaseTextFormatter { PrefixRemovalList = appPrefs.FieldPrefixRemovalList }; var constructorStatements = new CodeStatementCollection(); foreach (var hasMany in Table.HasManyRelationships) { if (appPrefs.Language == Language.CSharp) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(string.Format("{0}<{1}{2}>", appPrefs.ForeignEntityCollectionType, appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)), Formatter.FormatPlural(hasMany.Reference), appPrefs.UseLazy)); constructorStatements.Add(new CodeSnippetStatement(string.Format(TABS + "{0} = new {1}<{2}{3}>();", Formatter.FormatPlural(hasMany.Reference), codeGenerationHelper.InstatiationObject(appPrefs.ForeignEntityCollectionType), appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)))); } else if (appPrefs.Language == Language.VB) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(string.Format("{0}(Of {1}{2})", appPrefs.ForeignEntityCollectionType, appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)), Formatter.FormatPlural(hasMany.Reference), appPrefs.UseLazy)); constructorStatements.Add(new CodeSnippetStatement(string.Format(TABS + "{0} = New {1}(Of {2}{3})()", Formatter.FormatPlural(hasMany.Reference), codeGenerationHelper.InstatiationObject(appPrefs.ForeignEntityCollectionType), appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)))); } } var constructor = new CodeConstructor { Attributes = MemberAttributes.Public }; constructor.Statements.AddRange(constructorStatements); newType.Members.Add(constructor); return(compileUnit); }
public CodeCompileUnit GetCompileUnit(string className) { var codeGenerationHelper = new CodeGenerationHelper(); var compileUnit = codeGenerationHelper.GetCodeCompileUnitWithInheritanceAndInterface(nameSpace, className, appPrefs.InheritenceAndInterfaces); var mapper = new DataTypeMapper(); var newType = compileUnit.Namespaces[0].Types[0]; newType.IsPartial = appPrefs.GeneratePartialClasses; CreateProperties(codeGenerationHelper, mapper, newType); // Generate GetHashCode() and Equals() methods. if (Table.PrimaryKey != null && Table.PrimaryKey.Columns.Count != 0 && Table.PrimaryKey.Type == PrimaryKeyType.CompositeKey) { var pkColsList = Table.PrimaryKey.Columns.Select(s => Formatter.FormatText(s.Name)).ToList(); var equalsCode = CreateCompositeKeyEqualsMethod(pkColsList); var getHashKeyCode = CreateCompositeKeyGetHashKeyMethod(pkColsList); equalsCode.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "NHibernate Composite Key Requirements")); newType.Members.Add(equalsCode); newType.Members.Add(getHashKeyCode); getHashKeyCode.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty)); } // Dont create a constructor if there are no relationships. if (Table.HasManyRelationships.Count == 0) return compileUnit; var pascalCaseTextFormatter = new PascalCaseTextFormatter { PrefixRemovalList = appPrefs.FieldPrefixRemovalList }; var constructorStatements = new CodeStatementCollection(); foreach (var hasMany in Table.HasManyRelationships) { if (appPrefs.Language == Language.CSharp) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(string.Format("{0}<{1}{2}>", appPrefs.ForeignEntityCollectionType, appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)), Formatter.FormatPlural(hasMany.Reference), appPrefs.UseLazy)); constructorStatements.Add(new CodeSnippetStatement(string.Format(TABS + "{0} = new {1}<{2}{3}>();", Formatter.FormatPlural(hasMany.Reference), codeGenerationHelper.InstatiationObject(appPrefs.ForeignEntityCollectionType), appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)))); } else if (appPrefs.Language == Language.VB) { newType.Members.Add(codeGenerationHelper.CreateAutoProperty(string.Format("{0}(Of {1}{2})", appPrefs.ForeignEntityCollectionType, appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)), Formatter.FormatPlural(hasMany.Reference), appPrefs.UseLazy)); constructorStatements.Add(new CodeSnippetStatement(string.Format(TABS + "{0} = New {1}(Of {2}{3})()", Formatter.FormatPlural(hasMany.Reference), codeGenerationHelper.InstatiationObject(appPrefs.ForeignEntityCollectionType), appPrefs.ClassNamePrefix, pascalCaseTextFormatter.FormatSingular(hasMany.Reference)))); } } var constructor = new CodeConstructor { Attributes = MemberAttributes.Public }; constructor.Statements.AddRange(constructorStatements); newType.Members.Add(constructor); return compileUnit; }