internal void Generate() { IParserLogic2 parserLogic2 = new ParserLogic2(); _classDataList = new List<ClassData>(); _database.Tables .Where(t => t.Include) .ToList() .ForEach(table => { // Set data for the very [class]. var classData = new ClassData { Name = table.Name, IsPartial = _pocoSettings.MakePartial, Properties = new List<PropertyData>(), Methods = new List<MethodData>() }; // # Create the properties. table.Columns .ForEach(column => { classData.Properties.Add( new PropertyData { Name = column.Name, Scope = Common.VisibilityScope.Public, SystemType = parserLogic2.ConvertDatabaseTypeToDotnetType(column.DatabaseTypeName), Comment = column.IsInPrimaryKey ? new CommentData("This property is part of the primary key.") : null }); }); // # Create the constructors. if (_pocoSettings.CreateDefaultConstructor) { classData.Methods.Add(new MethodData { IsConstructor = true, Name = classData.Name, Comment = new CommentData("Default constructor needed for instance for de/serialising.") }); } if (_pocoSettings.CreateAllPropertiesConstructor) { classData.Methods.Add( new MethodData { IsConstructor = true, Name = classData.Name, Comment = new CommentData("This constructor takes all properties as parameters."), Parameters = table.Columns.Select(p => new ParameterData { Name = p.Name, SystemTypeString = parserLogic2.ConvertDatabaseTypeToDotnetTypeString( p.DatabaseTypeName) }).ToList() }); } if (_pocoSettings.CreateAllPropertiesSansPrimaryKeyConstructor) { classData.Methods.Add(new MethodData { IsConstructor = true, Name = classData.Name, Comment = new CommentData("This constructor takes all properties but primary keys as parameters."), Parameters = table.Columns .Where(p=>false == p.IsInPrimaryKey) .Select(p => new ParameterData { Name = p.Name, SystemTypeString = parserLogic2.ConvertDatabaseTypeToDotnetTypeString( p.DatabaseTypeName) }).ToList() }); } if(_pocoSettings.CreateCopyConstructor) { classData.Methods.Add(new MethodData { IsConstructor = true, Name = classData.Name, Comment = new CommentData("This is the copy constructor."), Parameters = new List<ParameterData> { new ParameterData { Name = classData.Name, SystemTypeString = classData.Name } }, Body = new BodyData { Lines = table.Columns .Select(p => string.Format("this.{0} = {1}.{2};", p.Name, Common.Safe(Common.ToCamelCase(table.Name)), p.Name)) .ToList() } }); } // # Create the methods. if (_pocoSettings.CreateMethodEquals && Regex.IsMatch(classData.Name, _pocoSettings.CreateMethodEqualsRegex)) { classData.Methods.Add( CreateEqualsMethod(table, classData, "o")); classData.Methods.Add( CreateHashMethod(table, classData)); } _classDataList.Add(classData); }); _log.Add("Included classes are:"); _log.Add(_classDataList.ToInfo()); }
private IList<string> CreateBodyForGetHashCodeMethod(TableData table) { IParserLogic2 parserLogic2 = new ParserLogic2(); var bodyLines = new List<string> { "int hash = 13;" }; bodyLines.AddRange(table.Columns.Select(c => "hash = (hash*7) + " + ( DefaultValueIsNull( parserLogic2.ConvertDatabaseTypeToDotnetType(c.DatabaseTypeName)) ? // It is a parameter that cannot be null. string.Format("( null == {0} ? 0 : this.{0}.GetHashCode() );", c.Name) : // It is a value that can be null. string.Format("this.{0}.GetHashCode();", c.Name) ))); bodyLines.Add("return hash;"); return bodyLines; }