Exemple #1
0
        private DataType ConvertDataType(System.ComponentModel.DataAnnotations.DataType sourceDataType)
        {
            switch (sourceDataType)
            {
            case System.ComponentModel.DataAnnotations.DataType.DateTime:
            case System.ComponentModel.DataAnnotations.DataType.Date:
            case System.ComponentModel.DataAnnotations.DataType.Time:
            case System.ComponentModel.DataAnnotations.DataType.Duration:
                return(DataType.DateTime);

            case System.ComponentModel.DataAnnotations.DataType.Url:
            case System.ComponentModel.DataAnnotations.DataType.ImageUrl:
            case System.ComponentModel.DataAnnotations.DataType.Upload:
                return(DataType.File);

            case System.ComponentModel.DataAnnotations.DataType.Currency:
                return(DataType.Numeric);

            case System.ComponentModel.DataAnnotations.DataType.Password:
                return(DataType.Password);

            default:
            case System.ComponentModel.DataAnnotations.DataType.Custom:
            case System.ComponentModel.DataAnnotations.DataType.PhoneNumber:
            case System.ComponentModel.DataAnnotations.DataType.Text:
            case System.ComponentModel.DataAnnotations.DataType.Html:
            case System.ComponentModel.DataAnnotations.DataType.MultilineText:
            case System.ComponentModel.DataAnnotations.DataType.EmailAddress:
            case System.ComponentModel.DataAnnotations.DataType.CreditCard:
            case System.ComponentModel.DataAnnotations.DataType.PostalCode:
                return(DataType.Text);
            }
        }
Exemple #2
0
        public IPropertyCustomizer Type(SystemDataType dataType, string errorMessage = null)
        {
            propertyCustomizerHolder.SourceDataType = dataType;
            Validator(new DataTypeAttribute(dataType)
                      .SetErrorMessage(errorMessage));

            return(Type(DataTypeConverter.Convert(dataType)));
        }
 //-------------------------------------------------------------------
 public void SetStringDataType()
 {
     if (_DotNetType == typeof(String))
     {
         if (MaxLength >= 200)
         {
             _DataTypeAttr = System.ComponentModel.DataAnnotations.DataType.MultilineText;
         }
         else if (Globals.IsMatchIdentifier(Globals.Identifier_Url, this))
         {
             _DataTypeAttr = System.ComponentModel.DataAnnotations.DataType.Url;
         }
         else if (Globals.IsMatchIdentifier(Globals.Identifier_Email, this))
         {
             _DataTypeAttr = System.ComponentModel.DataAnnotations.DataType.EmailAddress;
         }
         else if (Globals.IsMatchIdentifier(Globals.Identifier_File, this))
         {
             _DataTypeAttr = System.ComponentModel.DataAnnotations.DataType.Upload;
         }
         else if (Globals.IsMatchIdentifier(Globals.Identifier_Password, this))
         {
             _DataTypeAttr = System.ComponentModel.DataAnnotations.DataType.Password;
         }
         else if (Globals.IsMatchIdentifier(Globals.Identifier_Phone, this))
         {
             _DataTypeAttr = System.ComponentModel.DataAnnotations.DataType.PhoneNumber;
         }
         else if (Globals.IsMatchIdentifier(Globals.Identifier_PostalCode, this))
         {
             _DataTypeAttr = System.ComponentModel.DataAnnotations.DataType.PostalCode;
         }
         else if (Globals.IsMatchIdentifier(Globals.Identifier_CreditCard, this))
         {
             _DataTypeAttr = System.ComponentModel.DataAnnotations.DataType.CreditCard;
         }
         else
         {
             _DataTypeAttr = System.ComponentModel.DataAnnotations.DataType.Text;
         }
     }
 }
        public void SetDataTypes()
        {
            switch (SmoDataType.SqlDataType)
            {
            case SqlDataType.BigInt:
                _SqlDbType       = SqlDbType.BigInt;
                _DotNetType      = typeof(Int64);
                _DotNetTypeAlias = "long";
                break;

            case SqlDataType.Binary:
                _SqlDbType       = SqlDbType.Binary;
                _DotNetType      = typeof(Byte[]);
                _DotNetTypeAlias = "byte[]";
                break;

            case SqlDataType.Bit:
                _SqlDbType       = SqlDbType.Bit;
                _DotNetType      = typeof(Boolean);
                _DotNetTypeAlias = "bool";
                break;

            case SqlDataType.Char:
                _SqlDbType       = SqlDbType.Char;
                _DotNetType      = typeof(String);
                _DotNetTypeAlias = "string";
                SetStringDataType();
                break;

            case SqlDataType.Date:
                _SqlDbType       = SqlDbType.Date;
                _DotNetType      = typeof(DateTime);
                _DotNetTypeAlias = "DateTime";
                _DataTypeAttr    = System.ComponentModel.DataAnnotations.DataType.Date;
                break;

            case SqlDataType.DateTime:
                _SqlDbType       = SqlDbType.DateTime;
                _DotNetType      = typeof(DateTime);
                _DotNetTypeAlias = "DateTime";
                _DataTypeAttr    = System.ComponentModel.DataAnnotations.DataType.DateTime;

                break;

            case SqlDataType.DateTime2:
                _SqlDbType       = SqlDbType.DateTime2;
                _DotNetType      = typeof(DateTime);
                _DotNetTypeAlias = "DateTime";
                _DataTypeAttr    = System.ComponentModel.DataAnnotations.DataType.DateTime;
                break;

            case SqlDataType.DateTimeOffset:
                _SqlDbType       = SqlDbType.DateTimeOffset;
                _DotNetType      = typeof(DateTimeOffset);
                _DotNetTypeAlias = "DateTimeOffset";
                break;

            case SqlDataType.Decimal:
                _SqlDbType       = SqlDbType.Decimal;
                _DotNetType      = typeof(Decimal);
                _DotNetTypeAlias = "decimal";

                break;

            case SqlDataType.Float:
                _SqlDbType       = SqlDbType.Float;
                _DotNetType      = typeof(Double);
                _DotNetTypeAlias = "double";
                break;

            case SqlDataType.Image:
                _SqlDbType       = SqlDbType.Image;
                _DotNetType      = typeof(Byte[]);
                _DotNetTypeAlias = "byte[]";
                break;

            case SqlDataType.Int:
                _SqlDbType       = SqlDbType.Int;
                _DotNetType      = typeof(Int32);
                _DotNetTypeAlias = "int";
                break;

            case SqlDataType.Money:
                _SqlDbType       = SqlDbType.Money;
                _DotNetType      = typeof(Decimal);
                _DotNetTypeAlias = "decimal";
                _DataTypeAttr    = System.ComponentModel.DataAnnotations.DataType.Currency;

                break;

            case SqlDataType.NChar:
                _SqlDbType       = SqlDbType.NChar;
                _DotNetType      = typeof(String);
                _DotNetTypeAlias = "string";
                SetStringDataType();

                break;

            case SqlDataType.NText:
                _SqlDbType       = SqlDbType.NText;
                _DotNetType      = typeof(String);
                _DotNetTypeAlias = "string";
                _DataTypeAttr    = System.ComponentModel.DataAnnotations.DataType.MultilineText;

                break;

            case SqlDataType.NVarChar:
                _SqlDbType       = SqlDbType.NVarChar;
                _DotNetType      = typeof(String);
                _DotNetTypeAlias = "string";
                SetStringDataType();

                break;

            case SqlDataType.Real:
                _SqlDbType       = SqlDbType.Real;
                _DotNetType      = typeof(Single);
                _DotNetTypeAlias = "Single";
                break;

            case SqlDataType.SmallDateTime:
                _SqlDbType       = SqlDbType.SmallDateTime;
                _DotNetType      = typeof(DateTime);
                _DotNetTypeAlias = "DateTime";
                _DataTypeAttr    = System.ComponentModel.DataAnnotations.DataType.DateTime;
                break;

            case SqlDataType.SmallInt:
                _SqlDbType       = SqlDbType.SmallInt;
                _DotNetType      = typeof(Int16);
                _DotNetTypeAlias = "short";
                break;

            case SqlDataType.SmallMoney:
                _SqlDbType       = SqlDbType.SmallMoney;
                _DotNetType      = typeof(Decimal);
                _DotNetTypeAlias = "decimal";
                _DataTypeAttr    = System.ComponentModel.DataAnnotations.DataType.Currency;
                break;

            case SqlDataType.Text:
                _SqlDbType       = SqlDbType.Text;
                _DotNetType      = typeof(String);
                _DotNetTypeAlias = "string";
                _DataTypeAttr    = System.ComponentModel.DataAnnotations.DataType.MultilineText;

                break;

            case SqlDataType.Time:
                _SqlDbType       = SqlDbType.Time;
                _DotNetType      = typeof(TimeSpan);
                _DotNetTypeAlias = "TimeSpan";
                _DataTypeAttr    = System.ComponentModel.DataAnnotations.DataType.Time;
                break;

            case SqlDataType.Timestamp:
                _SqlDbType       = SqlDbType.Timestamp;
                _DotNetType      = typeof(Byte[]);
                _DotNetTypeAlias = "byte[]";
                break;

            case SqlDataType.TinyInt:
                _SqlDbType       = SqlDbType.TinyInt;
                _DotNetType      = typeof(Byte);
                _DotNetTypeAlias = "byte";
                break;

            case SqlDataType.UserDefinedDataType:
                _SqlDbType       = SqlDbType.UniqueIdentifier;
                _DotNetType      = typeof(Object);
                _DotNetTypeAlias = "object";
                break;

            case SqlDataType.UniqueIdentifier:
                _DotNetType      = typeof(Guid);
                _DotNetTypeAlias = "Guid";
                break;

            case SqlDataType.VarBinary:
                _SqlDbType       = SqlDbType.VarBinary;
                _DotNetType      = typeof(Byte[]);
                _DotNetTypeAlias = "byte[]";
                break;

            case SqlDataType.VarChar:
                _SqlDbType       = SqlDbType.VarChar;
                _DotNetType      = typeof(String);
                _DotNetTypeAlias = "string";
                SetStringDataType();

                break;

            case SqlDataType.Variant:
                _SqlDbType       = SqlDbType.Variant;
                _DotNetType      = typeof(Object);
                _DotNetTypeAlias = "object";
                break;

            case SqlDataType.Xml:
                _SqlDbType       = SqlDbType.Xml;
                _DotNetType      = typeof(System.Xml.XmlDocument);
                _DotNetTypeAlias = "XmlDocument";
                break;

            case SqlDataType.Geography:
                //    _SqlDbType = SqlDbType.Geography;
                _DotNetType      = typeof(Object);
                _DotNetTypeAlias = "object";
                break;

            case SqlDataType.Geometry:
                //    _SqlDbType = SqlDbType.Geometry;
                _DotNetType      = typeof(Object);
                _DotNetTypeAlias = "object";
                break;

            case SqlDataType.HierarchyId:
                //    _SqlDbType = SqlDbType.HierarchyId;
                _DotNetType      = typeof(Object);
                _DotNetTypeAlias = "object";
                break;

            case SqlDataType.None:
                //    _SqlDbType = SqlDbType.None;
                _DotNetType      = typeof(Object);
                _DotNetTypeAlias = "object";
                break;

            case SqlDataType.Numeric:
                //    _SqlDbType = SqlDbType.Numeric;
                _DotNetType      = typeof(Decimal);
                _DotNetTypeAlias = "decimal";
                break;

            case SqlDataType.NVarCharMax:
                _SqlDbType       = SqlDbType.NVarChar;
                _DotNetType      = typeof(String);
                _DotNetTypeAlias = "string";
                _DataTypeAttr    = System.ComponentModel.DataAnnotations.DataType.MultilineText;

                break;

            case SqlDataType.SysName:
                //    _SqlDbType = SqlDbType.SysName;
                _DotNetType      = typeof(Object);
                _DotNetTypeAlias = "object";
                break;

            case SqlDataType.UserDefinedTableType:
                _SqlDbType       = SqlDbType.Udt;
                _DotNetType      = typeof(Object);
                _DotNetTypeAlias = "object";
                break;

            case SqlDataType.UserDefinedType:
                _SqlDbType       = SqlDbType.Udt;
                _DotNetType      = typeof(Object);
                _DotNetTypeAlias = "object";
                break;

            case SqlDataType.VarBinaryMax:
                _SqlDbType       = SqlDbType.VarBinary;
                _DotNetType      = typeof(Byte[]);
                _DotNetTypeAlias = "byte[]";
                break;

            case SqlDataType.VarCharMax:
                _SqlDbType       = SqlDbType.VarChar;
                _DotNetType      = typeof(String);
                _DotNetTypeAlias = "string";
                _DataTypeAttr    = System.ComponentModel.DataAnnotations.DataType.MultilineText;

                break;

            default:
                _DotNetType      = typeof(String);
                _DotNetTypeAlias = "string";
                break;
            }
        }
Exemple #5
0
        /// <summary>
        /// Add the properties to the class.
        /// </summary>
        private void AddProperties()
        {
            // For each column found in the table
            // iterate through the list and create
            // each property.
            for (int i = 0; i < _data.PropertyName.Count(); i++)
            {
                // Create a new property member
                // and the accessor type.
                CodeMemberProperty valueProperty = new CodeMemberProperty();
                valueProperty.Attributes = MemberAttributes.Public | MemberAttributes.Final;

                // Assign the name and get and set indictors.
                valueProperty.Name   = _data.PropertyName[i];
                valueProperty.HasGet = true;
                valueProperty.HasSet = true;

                // Add the comments to the property.
                valueProperty.Comments.Add(new CodeCommentStatement("<summary>", true));
                valueProperty.Comments.Add(new CodeCommentStatement("Gets sets, the " + _data.PropertyName[i].ToLower() + " property for the object.", true));
                valueProperty.Comments.Add(new CodeCommentStatement("</summary>", true));

                bool isNullable = false;
                if (_data.PropertyIsNullable != null)
                {
                    isNullable = _data.PropertyIsNullable[i];
                }

                bool dataTypeNull = true;
                System.ComponentModel.DataAnnotations.DataType dataType = System.ComponentModel.DataAnnotations.DataType.Text;
                if (_data.DataType != null)
                {
                    if (_data.DataType[i] != null)
                    {
                        dataTypeNull = false;
                        dataType     = (System.ComponentModel.DataAnnotations.DataType)_data.DataType[i];
                    }
                }

                // If the table column is nullable and
                // the data type is not a reference type
                // then apply the nullable generic base.
                if (isNullable && ((!Nequeo.DataType.GetSystemType(_data.PropertyType[i]).IsArray) &&
                                   !Nequeo.DataType.GetSystemType(_data.PropertyType[i]).IsClass))
                {
                    // Assign the data type for the property if
                    // the data type is not a reference type
                    // then create a nullable type property.
                    valueProperty.Type = new CodeTypeReference(
                        "System.Nullable<" + Nequeo.DataType.GetSystemType(_data.PropertyType[i]).ToString() + ">");
                }
                else
                {
                    // Assign the property type for the property.
                    // Get the data type of the property from
                    // the sql data type.
                    valueProperty.Type = new CodeTypeReference(_data.PropertyType[i]);
                }

                // Add the code to the
                // get section of the property.
                valueProperty.GetStatements.Add(new CodeMethodReturnStatement(
                                                    new CodeFieldReferenceExpression(
                                                        new CodeThisReferenceExpression(), "_" + _data.PropertyName[i])));

                // Create a new code condition statement.
                CodeConditionStatement conditionalStatement = new CodeConditionStatement(
                    new CodeVariableReferenceExpression("(this." + "_" + _data.PropertyName[i] + " != value)"),
                    new CodeStatement[] { new CodeExpressionStatement(new CodeSnippetExpression("this." + "_" + _data.PropertyName[i] + " = value")) });

                // Add the code to the
                // set section of the property.
                valueProperty.SetStatements.Add(conditionalStatement);

                // Add the attributes to the property.
                if (!isNullable)
                {
                    valueProperty.CustomAttributes.Add(new CodeAttributeDeclaration("RequiredAttribute"));
                }

                // If attributes are to be included.
                if (_data.Attributes != null)
                {
                    Attribute[] attrs = _data.Attributes[i];
                    if (attrs != null)
                    {
                        foreach (Attribute attr in attrs)
                        {
                            valueProperty.CustomAttributes.Add(new CodeAttributeDeclaration(attr.GetType().FullName));
                        }
                    }
                }

                if (!dataTypeNull)
                {
                    valueProperty.CustomAttributes.Add(new CodeAttributeDeclaration("DataTypeAttribute",
                                                                                    new CodeAttributeArgument(new CodeSnippetExpression("DataType." + dataType.ToString()))));
                }

                // Add the attributes to the property.
                valueProperty.CustomAttributes.Add(new CodeAttributeDeclaration("DisplayNameAttribute",
                                                                                new CodeAttributeArgument(new CodeSnippetExpression("\"" + _data.LabelText[i] + "\""))));

                // Add the attributes to the property.
                valueProperty.CustomAttributes.Add(new CodeAttributeDeclaration("CategoryAttribute",
                                                                                new CodeAttributeArgument(new CodeSnippetExpression("\"Column\""))));

                // Add the attributes to the property.
                valueProperty.CustomAttributes.Add(new CodeAttributeDeclaration("DescriptionAttribute",
                                                                                new CodeAttributeArgument(new CodeSnippetExpression("\"Gets sets, the " + _data.PropertyName[i].ToLower() + " property for the object.\""))));

                // Add the attributes to the property.
                valueProperty.CustomAttributes.Add(new CodeAttributeDeclaration("DataMemberAttribute",
                                                                                new CodeAttributeArgument(new CodeSnippetExpression("Name = \"" + _data.PropertyName[i] + "\""))));

                // If the type is an array the add the
                // array attribute else add the element attribute.
                if ((Nequeo.DataType.GetSystemType(_data.PropertyType[i]).IsArray) ||
                    (_data.PropertyType[i].EndsWith("[]")))
                {
                    valueProperty.CustomAttributes.Add(new CodeAttributeDeclaration("XmlArrayAttribute",
                                                                                    new CodeAttributeArgument(new CodeSnippetExpression("ElementName = \"" + _data.PropertyName[i] + "\", IsNullable = " + isNullable.ToString().ToLower()))));
                }
                else
                {
                    valueProperty.CustomAttributes.Add(new CodeAttributeDeclaration("XmlElementAttribute",
                                                                                    new CodeAttributeArgument(new CodeSnippetExpression("ElementName = \"" + _data.PropertyName[i] + "\", IsNullable = " + isNullable.ToString().ToLower()))));
                }

                // Add the property to the class.
                _targetClass.Members.Add(valueProperty);
            }
        }
        public IPropertyCustomizer Type(SystemDataType dataType, string errorMessage = null)
        {
            propertyCustomizerHolder.SourceDataType = dataType;
            Validator(new DataTypeAttribute(dataType)
                .SetErrorMessage(errorMessage));

            return Type(DataTypeConverter.Convert(dataType));
        }