Пример #1
0
        /// <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);
            }
        }
        /// <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());
        }
Пример #3
0
        private static void BuildTableColumns(Model model, DataTable table, List <DataColumn> primaryKeyColumns)
        {
            foreach (PropertyInfo prop in model.Properties)
            {
                DataColumn column = new DataColumn();

                // Right now only handle simple datatypes.
                if (TypeMap.IsBasicNetType(prop.DataType))
                {
                    column.ColumnName  = string.IsNullOrEmpty(prop.ColumnName) ? prop.Name : prop.ColumnName;
                    column.DataType    = prop.DataType;
                    column.AllowDBNull = !prop.IsRequired;
                    column.Unique      = prop.IsUnique;
                    if (prop.IsKey)
                    {
                        primaryKeyColumns.Add(column);
                    }
                    table.Columns.Add(column);
                }
            }
        }
 /// <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);
         }
     }
 }