/// <summary> /// Build properties. /// </summary> /// <param name="model"></param> /// <param name="buffer"></param> /// <returns></returns> public void BuildProperty(Model model, PropertyInfo prop, bool usePropType, StringBuilder buffer) { string indent = GetIndent(); string netType = string.Empty; if (usePropType && TypeMap.IsBasicNetType(prop.DataType)) { netType = TypeMap.Get <string>(TypeMap.NetFormatToCSharp, prop.DataType.Name); buffer.Append(indent + "/// <summary>" + Environment.NewLine); buffer.Append(indent + "/// Get/Set " + prop.Name + Environment.NewLine); buffer.Append(indent + "/// </summary>" + Environment.NewLine); buffer.Append(indent + "public " + netType + " " + prop.Name + " { get; set; }" + Environment.NewLine + Environment.NewLine + Environment.NewLine); } else { netType = model.Name; // private Address _address = new Address(); buffer.Append(indent + "private " + netType + " _" + prop.Name + " = new " + netType + "();" + Environment.NewLine); buffer.Append(indent + "/// <summary>" + Environment.NewLine); buffer.Append(indent + "/// Get/Set " + prop.Name + Environment.NewLine); buffer.Append(indent + "/// </summary>" + Environment.NewLine); buffer.Append(indent + "public " + netType + " " + prop.Name + Environment.NewLine + indent + " { " + Environment.NewLine + indent + " get { return _" + prop.Name + "; }" + Environment.NewLine + indent + " set { _" + prop.Name + " = value; }" + Environment.NewLine + indent + " }" + Environment.NewLine + Environment.NewLine + Environment.NewLine); } }
public virtual string BuildColumnInfoForKey(PropertyInfo prop) { // [Id] [bigint] IDENTITY(1,1) NOT NULL string columnInfo = "[{0}] [{1}] {2} {3}"; string sqlType = TypeMap.Get <string>(TypeMap.SqlServer, prop.DataType.Name); string indentity = "IDENTITY(1,1)"; columnInfo = string.Format(columnInfo, prop.ColumnName, sqlType, indentity, "NOT NULL"); return(columnInfo); }
public virtual string BuildColumnInfo(PropertyInfo prop) { string columnInfo = "[{0}] [{1}]{2} {3}"; string sqlType = TypeMap.Get <string>(TypeMap.SqlServer, prop.DataType.Name); string length = prop.DataType == typeof(string) ? "(" + prop.MaxLength + ")" : string.Empty; string nullOption = prop.IsRequired ? "NOT NULL" : "NULL"; columnInfo = string.Format(columnInfo, prop.ColumnName, sqlType, length, nullOption); return(columnInfo); }
/// <summary> /// Builds code for a property. /// </summary> /// <param name="model">Model to use.</param> /// <param name="prop">Property name.</param> /// <param name="usePropType">True to use a default get/set model, /// false to use a private variable with the default get/set model.</param> /// <param name="type">Type to generate.</param> /// <param name="initialize">If using a private variable, setting this to /// true will also generate code to generate a new instance of the variable.</param> /// <returns>Generated string.</returns> public string BuildProperty(Model model, PropInfo prop, bool usePropType, string type, bool initialize) { string indent = GetIndent(); var buffer = new StringBuilder(); string netType = string.Empty; if (usePropType && TypeMap.IsBasicNetType(prop.DataType)) { netType = TypeMap.Get <string>(TypeMap.NetFormatToCSharp, prop.DataType.Name); } else { netType = string.IsNullOrEmpty(type) ? model.Name : type; } if (usePropType) { buffer.Append(indent + "/// <summary>" + Environment.NewLine); buffer.Append(indent + "/// Get/Set " + prop.Name + Environment.NewLine); buffer.Append(indent + "/// </summary>" + Environment.NewLine); buffer.Append(indent + "public " + netType + " " + prop.Name + " { get; set; }" + Environment.NewLine + Environment.NewLine + Environment.NewLine); } else { string name = prop.Name.ToLower()[0] + prop.Name.Substring(1); string privateVar = indent + "private " + netType + " _" + name; // private Address _address = new Address(); if (initialize) { buffer.Append(privateVar + " = new " + netType + "();" + Environment.NewLine); } else { buffer.Append(privateVar + ";" + Environment.NewLine); } buffer.Append(indent + "/// <summary>" + Environment.NewLine); buffer.Append(indent + "/// Get/Set " + prop.Name + Environment.NewLine); buffer.Append(indent + "/// </summary>" + Environment.NewLine); buffer.Append(indent + "public " + netType + " " + prop.Name + Environment.NewLine + indent + " { " + Environment.NewLine + indent + " get { return _" + name + "; }" + Environment.NewLine + indent + " set { _" + name + " = value; }" + Environment.NewLine + indent + " }" + Environment.NewLine + Environment.NewLine + Environment.NewLine); } return(buffer.ToString()); }
private void AddDbParams(Model model, string template, string strTemplate, StringBuilder buffer) { model.Properties.ForEach(prop => { if (CanProcessProp(prop)) { var sqlType = TypeMap.Get <object>(TypeMap.SqlClient, prop.DataType.Name).ToString(); string format = template; if (prop.DataType == typeof(string) || prop.DataType == typeof(StringClob)) { format = strTemplate; } buffer.Append(string.Format(format, prop.Name, sqlType, prop.Name) + Environment.NewLine); } }); }
/// <summary> /// Build column mapping. /// </summary> /// <param name="ctx"></param> /// <param name="current"></param> /// <param name="prop"></param> /// <returns></returns> private string BuildMappingForColumn(ModelContext ctx, Model current, PropertyInfo prop) { // <column name="Title" length="100" sql-type="nvarchar" not-null="false"/> string columnString = GetIndent() + "<column name=\"" + prop.ColumnName + "\""; // Set the length. if (prop.DataType == typeof(string)) { columnString += " length=\"" + prop.MaxLength + "\""; columnString += " sql-type=\"nvarchar\""; } else { string dataType = TypeMap.Get <string>(TypeMap.SqlServer, prop.DataType.Name); columnString += " sql-type=\"" + dataType + "\""; } // Unique. if (prop.IsUnique) { columnString += " unique=\"true\""; } columnString += " not-null=\"" + prop.IsRequired.ToString().ToLower() + "\" />"; return(columnString + Environment.NewLine); }
/// <summary> /// Builds the validation code for all entire object, taking into account /// all the validations for each property. /// </summary> /// <param name="model"></param> /// <param name="prop"></param> /// <param name="buffer"></param> public void BuildValidationForProperty(Model model, PropInfo prop, StringBuilder buffer) { // Validate basic types, int, string, datetime, double. if (TypeMap.IsBasicNetType(prop.DataType)) { string shortTypeName = TypeMap.Get <string>(TypeMap.NetFormatToCSharp, prop.DataType.Name); if (shortTypeName == "string" && !string.IsNullOrEmpty(prop.RegEx)) { BuildValidationForStringRegExProperty(model, prop, buffer); } else if (shortTypeName == "string" && string.IsNullOrEmpty(prop.RegEx)) { BuildValidationForStringProperty(model, prop, buffer); } else if (shortTypeName == "int") { BuildValidationForIntProperty(model, prop, buffer); } else if (shortTypeName == "DateTime") { BuildValidationForDateTimeProperty(model, prop, buffer); } } }