Exemple #1
0
        public DBField(DBFieldAttribute dbFieldattribute, PropertyInfo propInfo)
        {
            this.dbFieldattribute = dbFieldattribute;
            this.propInfo         = propInfo;

            if (propInfo.PropertyType == typeof(DateTime) && string.IsNullOrEmpty(dbFieldattribute.format))
            {
                //  Set default DateTime format to "yyyy-MM-dd"
                dbFieldattribute.format = "yyyy-MM-dd";
            }

            this.validationAttributeList = new List <ValidationAttribute>();

            bool hasDataTypeValidationAttribute = false;


            foreach (Attribute a in propInfo.GetCustomAttributes(false))
            {
                if (a is DBFieldTranscoder)
                {
                    if (m_transcoder == null)
                    {
                        m_transcoder = (DBFieldTranscoder)a;
                    }
                    else
                    {
                        throw new Exception("Only 1 transcoder attribute is allowed");
                    }
                }
                if (a is RequiredAttribute)
                {
                    m_required = true;
                }
                if (a is ValidationAttribute)
                {
                    validationAttributeList.Add((ValidationAttribute)a);
                    if (a is DoubleAttribute && propInfo.PropertyType == typeof(double))
                    {
                        hasDataTypeValidationAttribute = true;
                    }
                    if (a is IntAttribute && propInfo.PropertyType == typeof(int))
                    {
                        hasDataTypeValidationAttribute = true;
                    }
                }
            }
            if (!hasDataTypeValidationAttribute)
            {
                if (propInfo.PropertyType == typeof(double))
                {
                    validationAttributeList.Add(new DoubleAttribute());
                }
                else if (propInfo.PropertyType == typeof(int))
                {
                    validationAttributeList.Add(new IntAttribute());
                }
            }
        }
Exemple #2
0
        public DBManager(Type type)
        {
            System.Diagnostics.Debug.Write("Initialize DBManager for " + type.Name + ".");
            this.type = type;
            m_dbclass = DBUtils.GetDBClassAttributes(type);
            if (m_dbclass == null)
            {
                m_dbclass = new DBClassAttribute(type.Name);
            }
            System.Diagnostics.Debug.Write(".");


            fieldDictionary = new Dictionary <string, DBField>();
            fieldList       = fieldDictionary.Values;
            keyFieldList    = new List <DBField>();

            PropertyInfo[] propInfoList = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);

            foreach (PropertyInfo propInfo in propInfoList)
            {
                object[] attributeList = propInfo.GetCustomAttributes(typeof(DBFieldAttribute), false);

                foreach (Attribute attribute in attributeList)
                {
                    if (attribute is DBFieldAttribute)
                    {
                        DBFieldAttribute fieldAttribute = (DBFieldAttribute)attribute;
                        DBField          field          = new DBField(fieldAttribute, propInfo);

                        fieldDictionary.Add(fieldAttribute.columnName, field);

                        if (fieldAttribute.isKey)
                        {
                            keyFieldList.Add(field);
                        }

                        //if (!string.IsNullOrEmpty(fieldAttribute.columnName))
                        //{
                        //    object value = SourceRow[fieldAttribute.columnName];
                        //    if (field.transcoder != null)
                        //        value = field.transcoder.fromDB(value);
                        //    propInfo.SetValue(DestinationDBObject, value, null);
                        //}
                    }
                }
            }
            System.Diagnostics.Debug.Write(".");
            prepareSQLStatement();
            System.Diagnostics.Debug.WriteLine(".done");
        }