Exemplo n.º 1
0
        public CodeCompileUnit GetCompileUnit(string className)
        {
            var    codeGenerationHelper = new CodeGenerationHelper();
            string nameSpace            = "Entity";
            var    compileUnit          = codeGenerationHelper.GetCodeCompileUnitWithInheritanceAndInterface(nameSpace, className, "");

            var mapper  = new DataTypeMapper();
            var newType = compileUnit.Namespaces[0].Types[0];

            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 = new List <string>();
                foreach (var pkCol in Table.PrimaryKey.Columns)
                {
                    pkColsList.Add(Formatter.FormatText(pkCol.Name));
                }

                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.

            return(compileUnit);
        }
Exemplo n.º 2
0
        private void CreateAutoProperties(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType)
        {
            if (Table.PrimaryKey != null)
            {
                foreach (var pk in Table.PrimaryKey.Columns)
                {
                    if (pk.IsForeignKey && appPrefs.IncludeForeignKeys)
                    {
                        newType.Members.Add(codeGenerationHelper.CreateAutoProperty(Formatter.FormatSingular(pk.ForeignKeyTableName),
                                                                                    Formatter.FormatSingular(pk.ForeignKeyTableName),
                                                                                    appPrefs.UseLazy));
                    }
                    else
                    {
                        var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, pk.DataType, pk.DataLength,
                                                                 pk.DataPrecision, pk.DataScale);
                        var fieldName   = FixPropertyWithSameClassName(pk.Name, Table.Name);
                        var pkAlsoFkQty = (from fk in Table.ForeignKeys.Where(fk => fk.UniquePropertyName == pk.Name) select fk).Count();
                        if (pkAlsoFkQty > 0)
                        {
                            fieldName = fieldName + "Id";
                        }
                        newType.Members.Add(codeGenerationHelper.CreateAutoProperty(mapFromDbType.ToString(),
                                                                                    Formatter.FormatText(fieldName),
                                                                                    appPrefs.UseLazy));
                    }
                }
            }

            //if (appPrefs.IncludeForeignKeys)
            //{
            //    var pascalCaseTextFormatter = new PascalCaseTextFormatter { PrefixRemovalList = appPrefs.FieldPrefixRemovalList };
            //    // Note that a foreign key referencing a primary within the same table will end up giving you a foreign key property with the same name as the table.
            //    string lastOne = null;
            //    foreach (var fk in Table.Columns.Where(c => c.IsForeignKey && !c.IsPrimaryKey))
            //    {
            //        var typeName = appPrefs.ClassNamePrefix + pascalCaseTextFormatter.FormatSingular(fk.ForeignKeyTableName);
            //        var propertyName = Formatter.FormatSingular(fk.ForeignKeyTableName);
            //        var fieldName = FixPropertyWithSameClassName(propertyName, Table.Name);
            //        if (lastOne != fieldName)
            //            newType.Members.Add(codeGenerationHelper.CreateAutoProperty(typeName, fieldName, appPrefs.UseLazy));
            //        lastOne = fieldName;
            //    }
            //}

            foreach (var column in Table.Columns.Where(x => !x.IsPrimaryKey && (!x.IsForeignKey || !appPrefs.IncludeForeignKeys)))
            {
                var mapFromDbType = mapper.MapFromDBType(this.appPrefs.ServerType, column.DataType, column.DataLength, column.DataPrecision, column.DataScale);

                var fieldName = FixPropertyWithSameClassName(column.Name, Table.Name);
                var property  = codeGenerationHelper.CreateAutoProperty(mapFromDbType, Formatter.FormatText(fieldName), column.IsNullable, appPrefs.UseLazy);
                AttachValidatorAttributes(ref property, column);
                newType.Members.Add(property);
            }
        }
Exemplo n.º 3
0
        public CodeCompileUnit GetCodeCompileUnitWithInheritanceAndInterface(string nameSpace, string className, string inheritanceAndInterface)
        {
            var codeGenerationHelper = new CodeGenerationHelper();
            var codeCompileUnit      = codeGenerationHelper.GetCodeCompileUnit(nameSpace, className);

            if (!string.IsNullOrEmpty(inheritanceAndInterface))
            {
                foreach (CodeNamespace ns in codeCompileUnit.Namespaces)
                {
                    foreach (CodeTypeDeclaration type in ns.Types)
                    {
                        foreach (var classOrInterface in inheritanceAndInterface.Split(','))
                        {
                            type.BaseTypes.Add(new CodeTypeReference(classOrInterface.Replace("<T>", "<" + className + ">").Trim()));
                        }
                    }
                }
            }
            return(codeCompileUnit);
        }
Exemplo n.º 4
0
 private void CreateProperties(CodeGenerationHelper codeGenerationHelper, DataTypeMapper mapper, CodeTypeDeclaration newType)
 {
     CreateAutoProperties(codeGenerationHelper, mapper, newType);
 }