public SqlCommand GetDeleteCommand()
        {
            SqlCommand _return = new SqlCommand();
            string     tabela  = typeof(T).Name;
            string     chave   = "";

            using (SqlConnection cn = new SqlConnection(CaminhoBD))
            {
                try
                {
                    cn.Open();
                }
                catch (Exception)
                {
                    throw;
                }

                _return.CommandText = "Delete From {0} Where {1}";
                _return.Connection  = cn;

                PropertyInfo pro = typeof(T).GetProperties().ToList().FirstOrDefault(
                    p => p.GetCustomAttribute(typeof(DataObjectFieldAttribute)) != null);

                DataObjectFieldAttribute att = (DataObjectFieldAttribute)pro.GetCustomAttribute(typeof(DataObjectFieldAttribute));
                if (att.PrimaryKey)
                {
                    chave = pro.Name + "=@" + pro.Name;
                    _return.Parameters.AddWithValue("@" + pro.Name, pro.GetValue(this));
                }

                _return.CommandText = string.Format(_return.CommandText, tabela, chave);
            }
            return(_return);
        }
예제 #2
0
        public SqlCommand GetUpdateCommand()
        {
            SqlCommand _return = new SqlCommand();

            _return.CommandText = "UPDATE {0} SET {1} WHERE {2}";

            string tabela = typeof(T).Name;
            string campos = "";
            string chave  = "";

            foreach (PropertyInfo pro in typeof(T).GetProperties().ToList().Where(
                         p => p.GetCustomAttribute(typeof(DataObjectFieldAttribute)) != null))
            {
                DataObjectFieldAttribute att = (DataObjectFieldAttribute)pro.GetCustomAttribute(typeof(DataObjectFieldAttribute));

                if (att.PrimaryKey)
                {
                    chave = pro.Name + "=@" + pro.Name;
                    _return.Parameters.AddWithValue("@" + pro.Name, pro.GetValue(this));
                }
                else
                {
                    campos += pro.Name + "=@" + pro.Name + ",";
                    _return.Parameters.AddWithValue("@" + pro.Name, pro.GetValue(this));
                }
            }

            campos = campos.Substring(0, campos.Length - 1);

            _return.CommandText = string.Format(_return.CommandText, tabela, campos, chave);

            return(_return);
        }
예제 #3
0
        private static bool FilterDataObjectProperty(MemberInfo propertyInfo, object unused)
        {
            DataObjectFieldAttribute dataObjectFieldAttribute =
                Attribute.GetCustomAttribute(propertyInfo, typeof(DataObjectFieldAttribute)) as DataObjectFieldAttribute;

            return(dataObjectFieldAttribute != null);
        }
        public void Ctor_PrimaryKey_IsIdentity(bool primaryKey, bool isIdentity)
        {
            var attribute = new DataObjectFieldAttribute(primaryKey, isIdentity);

            Assert.Equal(primaryKey, attribute.PrimaryKey);
            Assert.Equal(isIdentity, attribute.IsIdentity);
            Assert.False(attribute.IsNullable);
            Assert.Equal(-1, attribute.Length);
        }
        public void Ctor_PrimaryKey_IsIdentity_IsNullable_Length(bool primaryKey, bool isIdentity, bool isNullable, int length)
        {
            var attribute = new DataObjectFieldAttribute(primaryKey, isIdentity, isNullable, length);

            Assert.Equal(primaryKey, attribute.PrimaryKey);
            Assert.Equal(isIdentity, attribute.IsIdentity);
            Assert.Equal(isIdentity, attribute.IsNullable);
            Assert.Equal(length, attribute.Length);
        }
예제 #6
0
파일: FieldItem.cs 프로젝트: rebider/soa
        /// <summary>构造函数</summary>
        /// <param name="table"></param>
        /// <param name="property"></param>
        public FieldItem(TableItem table, PropertyInfo property)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            _Table = table;

            _Property        = property;
            _Column          = BindColumnAttribute.GetCustomAttribute(_Property);
            _DataObjectField = DataObjectAttribute.GetCustomAttribute(_Property, typeof(DataObjectFieldAttribute)) as DataObjectFieldAttribute;
            _Description     = DescriptionAttribute.GetCustomAttribute(_Property, typeof(DescriptionAttribute)) as DescriptionAttribute;
        }
예제 #7
0
        private void GetDataObjectAttributes()
        {
            DataObjectFieldAttribute attribute =
                (DataObjectFieldAttribute)
                _field.Attributes[typeof(DataObjectFieldAttribute)];

            if (attribute != null)
            {
                _primaryKey = attribute.PrimaryKey;
                _isIdentity = attribute.IsIdentity;
                _isNullable = attribute.IsNullable;
                _length     = attribute.Length;
            }
        }
예제 #8
0
        /// <summary>构造函数</summary>
        /// <param name="table"></param>
        /// <param name="property">属性</param>
        public FieldItem(TableItem table, PropertyInfo property)
        {
            Table = table;

            if (property != null)
            {
                _Property = property;
                var dc = _Column = BindColumnAttribute.GetCustomAttribute(property);
                var df = _DataObjectField = property.GetCustomAttribute<DataObjectFieldAttribute>();
                var ds = _Description = property.GetCustomAttribute<DescriptionAttribute>();
                var di = _DisplayName = property.GetCustomAttribute<DisplayNameAttribute>();
                Map = property.GetCustomAttribute<MapAttribute>();
                Name = property.Name;
                Type = property.PropertyType;
                DeclaringType = property.DeclaringType;

                if (df != null)
                {
                    IsIdentity = df.IsIdentity;
                    PrimaryKey = df.PrimaryKey;
                    IsNullable = df.IsNullable;
                    Length = df.Length;

                    IsDataObjectField = true;
                }

                if (dc != null)
                {
                    Master = dc.Master;
                }

                if (dc != null && !dc.Name.IsNullOrWhiteSpace())
                    ColumnName = dc.Name;
                else
                    ColumnName = Name;

                if (ds != null && !String.IsNullOrEmpty(ds.Description))
                    Description = ds.Description;
                else if (dc != null && !String.IsNullOrEmpty(dc.Description))
                    Description = dc.Description;
                if (di != null && !di.DisplayName.IsNullOrEmpty())
                    DisplayName = di.DisplayName;

                var map = Map;
                if (map == null || map.Provider == null) ReadOnly = !property.CanWrite;
                var ra = property.GetCustomAttribute<ReadOnlyAttribute>();
                if (ra != null) ReadOnly = ra.IsReadOnly;
            }
        }
예제 #9
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="propertyDescriptor">the property descriptor.</param>
        public ObjectFieldSchema(PropertyDescriptor propertyDescriptor)
        {
            _propertyDescriptor = propertyDescriptor;

            DataObjectFieldAttribute attr =
                (DataObjectFieldAttribute)_propertyDescriptor.Attributes[typeof(DataObjectFieldAttribute)];

            if (attr != null)
            {
                _length     = attr.Length;
                _primaryKey = attr.PrimaryKey;
                _isIdentity = attr.IsIdentity;
                _isNullable = attr.IsNullable;
            }
        }
예제 #10
0
 private void EnsureMetaData()
 {
     if (!this._retrievedMetaData)
     {
         DataObjectFieldAttribute attribute = (DataObjectFieldAttribute)this._fieldDescriptor.Attributes[typeof(DataObjectFieldAttribute)];
         if (attribute != null)
         {
             this._primaryKey = attribute.PrimaryKey;
             this._isIdentity = attribute.IsIdentity;
             this._isNullable = attribute.IsNullable;
             this._length     = attribute.Length;
         }
         this._retrievedMetaData = true;
     }
 }
예제 #11
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static PropertyDescriptor FindPrimaryProperty(Type type)
 {
     if (IsDataObjectSource(type))
     {
         DataObjectFieldAttribute attribute = null;
         foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(type))
         {
             attribute = (DataObjectFieldAttribute)
                         property.Attributes[typeof(DataObjectFieldAttribute)];
             if (attribute != null && attribute.PrimaryKey)
             {
                 return(property);
             }
         }
         throw PrimaryNotExists(type);
     }
     throw IsNotDataObject(type);
 }
        public static IEnumerable <object[]> Equals_TestData()
        {
            var attribute = new DataObjectFieldAttribute(true, true, true, 10);

            yield return(new object[] { attribute, attribute, true });

            yield return(new object[] { attribute, new DataObjectFieldAttribute(true, true, true, 10), true });

            yield return(new object[] { attribute, new DataObjectFieldAttribute(false, true, true, 10), false });

            yield return(new object[] { attribute, new DataObjectFieldAttribute(true, false, true, 10), false });

            yield return(new object[] { attribute, new DataObjectFieldAttribute(true, true, false, 10), false });

            yield return(new object[] { attribute, new DataObjectFieldAttribute(true, true, true, 9), false });

            yield return(new object[] { attribute, new object(), false });

            yield return(new object[] { attribute, null, false });
        }
예제 #13
0
        public SqlCommand GetDeleteCommand()
        {
            SqlCommand _return = new SqlCommand();

            _return.CommandText = "DELETE FROM {0} WHERE {1}";
            string tabela = typeof(T).Name;
            string chave  = "";

            foreach (PropertyInfo pro in typeof(T).GetProperties().ToList().Where(
                         p => p.GetCustomAttribute(typeof(DataObjectFieldAttribute)) != null))
            {
                DataObjectFieldAttribute att = (DataObjectFieldAttribute)pro.GetCustomAttribute(typeof(DataObjectFieldAttribute));
                if (att.PrimaryKey)
                {
                    chave = pro.Name + "=@" + pro.Name;
                }
                _return.Parameters.AddWithValue("@" + pro.Name, pro.GetValue(this));
            }
            _return.CommandText = string.Format(_return.CommandText, tabela, chave);
            return(_return);
        }
예제 #14
0
        /// <summary>
        /// Creates an instance of the object.
        /// </summary>
        /// <param name="field">The PropertyInfo object
        /// describing the property.</param>
        public ObjectFieldInfo(PropertyDescriptor field)
        {
            DataObjectFieldAttribute attribute =
                (DataObjectFieldAttribute)
                field.Attributes[typeof(DataObjectFieldAttribute)];

            if (attribute != null)
            {
                _primaryKey = attribute.PrimaryKey;
                _isIdentity = attribute.IsIdentity;
                _isNullable = attribute.IsNullable;
                _length     = attribute.Length;
            }
            _dataType = Utilities.GetPropertyType(
                field.PropertyType);
            _isReadOnly = field.IsReadOnly;
            _name       = field.Name;

            // nullable
            Type t = field.PropertyType;

            if (!t.IsValueType || _isNullable)
            {
                _nullable = true;
            }
            else
            {
                if (t.IsGenericType)
                {
                    _nullable = (t.GetGenericTypeDefinition() == typeof(Nullable <>));
                }
                else
                {
                    _nullable = false;
                }
            }
        }
예제 #15
0
        public SqlCommand GetDeleteCommand()
        {
            SqlCommand _return = new SqlCommand();
            string     tabela  = typeof(T).Name;
            string     chave   = "";

            using (SqlConnection cn = new SqlConnection("Server=.\\sqlexpress;Database=CadastroEmpresa;Trusted_Connection=True;"))
            {
                try
                {
                    cn.Open();
                }
                catch (Exception)
                {
                    throw;
                }
                using (SqlCommand cmd = new SqlCommand())
                {
                    _return.CommandText = @"DELETE FROM {0} WHERE {1}";
                    cmd.Connection      = cn;

                    foreach (PropertyInfo pro in typeof(T).GetProperties().ToList().Where(
                                 p => p.GetCustomAttribute(typeof(DataObjectFieldAttribute)) != null))
                    {
                        DataObjectFieldAttribute att = (DataObjectFieldAttribute)pro.GetCustomAttribute(typeof(DataObjectFieldAttribute));
                        if (att.PrimaryKey)
                        {
                            chave = pro.Name + "=@" + pro.Name;
                            _return.Parameters.AddWithValue("@" + pro.Name, pro.GetValue(this));
                        }
                    }
                    _return.CommandText = string.Format(_return.CommandText, tabela, chave);
                }
            }
            return(_return);
        }
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="pi"></param>
 /// <param name="bc"></param>
 /// <param name="dof"></param>
 public FieldItem(PropertyInfo pi, BindColumnAttribute bc, DataObjectFieldAttribute dof)
 {
     Property        = pi;
     Column          = bc;
     DataObjectField = dof;
 }
예제 #17
0
        /// <summary>构造函数</summary>
        /// <param name="table"></param>
        /// <param name="property">属性</param>
        public FieldItem(TableItem table, PropertyInfo property)
        {
            //if (property == null) throw new ArgumentNullException("property");

            _Table = table;

            if (property != null)
            {
                _Property = property;
                var dc = _Column = BindColumnAttribute.GetCustomAttribute(property);
                var df = _DataObjectField = property.GetCustomAttribute <DataObjectFieldAttribute>();
                var ds = _Description = property.GetCustomAttribute <DescriptionAttribute>();
                var di = _DisplayName = property.GetCustomAttribute <DisplayNameAttribute>();
                Name          = property.Name;
                Type          = property.PropertyType;
                DeclaringType = property.DeclaringType;

                if (df != null)
                {
                    IsIdentity = df.IsIdentity;
                    PrimaryKey = df.PrimaryKey;
                    IsNullable = df.IsNullable;
                    Length     = df.Length;

                    IsDataObjectField = true;
                }

                if (dc != null)
                {
                    _ID    = dc.Order;
                    Master = dc.Master;
                }

                if (dc != null && !dc.Name.IsNullOrWhiteSpace())
                {
                    ColumnName = dc.Name;
                }
                else
                {
                    ColumnName = Name;
                }

                if (ds != null && !String.IsNullOrEmpty(ds.Description))
                {
                    Description = ds.Description;
                }
                else if (dc != null && !String.IsNullOrEmpty(dc.Description))
                {
                    Description = dc.Description;
                }
                if (di != null && !di.DisplayName.IsNullOrEmpty())
                {
                    DisplayName = di.DisplayName;
                }

                _ReadOnly = !property.CanWrite;
                var ra = property.GetCustomAttribute <ReadOnlyAttribute>();
                if (ra != null)
                {
                    _ReadOnly = ra.IsReadOnly;
                }
            }
        }
        public void GetHashCode_InvokeMultipleTimes_ReturnsSame()
        {
            var attribute = new DataObjectFieldAttribute(false);

            Assert.Equal(attribute.GetHashCode(), attribute.GetHashCode());
        }
 public void Equals_Other_ReturnsExpected(DataObjectFieldAttribute attribute, object other, bool expected)
 {
     Assert.Equal(expected, attribute.Equals(other));
 }