Пример #1
0
        private static void CreateInsertQuery(IDataEntity Record, StringBuilder Query, List <PropertyInfo> properties, Dictionary <string, string> FieldsNames)
        {
            List <string> FieldsHolders = new List <string>();

            foreach (PropertyInfo property in properties)
            {
                //first we dfo not want to add in the insert statement the Key
                var KeyName   = (from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.EntityKey) select attr).FirstOrDefault();
                var NotInsert = (from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.NotInsert) select attr).FirstOrDefault();
                if (NotInsert == null)
                {
                    if (KeyName == null)
                    {
                        //check if the property has mapped field attribute
                        //if the attribute is missing we use the property name as field
                        DataInterface.Attribbutes.Field FieldAttribute = (DataInterface.Attribbutes.Field)(from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.Field) select attr).FirstOrDefault();
                        if (FieldAttribute == null)
                        {
                            FieldAttribute = new DataInterface.Attribbutes.Field(property.Name);
                        }
                        FieldsNames.Add(property.Name, FieldAttribute.Name);
                        FieldsHolders.Add($"@{FieldAttribute.Name}");
                    }
                }
            }
            Query.Append($"{String.Join(",", FieldsNames.Values.ToArray()) }) values ({String.Join(",", FieldsHolders.ToArray())});Select Scope_Identity()");
        }
Пример #2
0
        /// <summary>
        /// Updates the record.
        ///   If there is a property marked as EntityKey the update will consider that field as Unique Key
        ///  If there is not any property marked as EntityKey the function will throw an exception
        /// </summary>
        /// <param name="record">The record.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">
        /// There is not any property marked as EntityKey
        /// or
        /// A Class marked with attribute NotInsert cannot perform write opperations
        /// </exception>
        public bool UpdateRecord(IDataEntity record)
        {
            List <PropertyInfo>         properties  = record.GetType().GetProperties().ToList();
            Dictionary <string, string> FieldsNames = new Dictionary <string, string>();

            var customAttributes = record.GetType().GetCustomAttributes();

            OnBeforeInsert(null);
            //find if there is an EntityKey property. If there is not we abort the operation

            Boolean KeyExists = false;

            foreach (PropertyInfo property in properties)
            {
                DataInterface.Attribbutes.EntityKey KeyProperty = (DataInterface.Attribbutes.EntityKey)(from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.EntityKey) select attr).FirstOrDefault();
                if (KeyProperty != null)
                {
                    KeyExists = true;
                    break;
                }
            }

            if (KeyExists == false)
            {
                throw new Exception("There is not any property marked as EntityKey");
            }


            DataInterface.Attribbutes.NotInsert NotInsertAttribute = (DataInterface.Attribbutes.NotInsert)(from attr in record.GetType().GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.NotInsert) select attr).FirstOrDefault();

            if (NotInsertAttribute != null)
            {
                throw new Exception("A Class marked with attribute NotInsert cannot perform write opperations");
            }


            //first find the table attribute to obtain the maped db table
            DataInterface.Attribbutes.Table TableAttribute = (DataInterface.Attribbutes.Table)(from attr in record.GetType().GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.Table) select attr).FirstOrDefault();

            using (SqlConnection connection = new SqlConnection(this.Source))
            {
                connection.Open();
                try
                {
                    StringBuilder Query = new StringBuilder();
                    if (TableAttribute != null)
                    {
                        Query.Append($"Update {TableAttribute.Name} set ");
                    }
                    else
                    {
                        Query.Append($"Update  {record.GetType().Name} set");
                    }

                    //get the properties of the entity

                    // create the insert query

                    CreateUpdateQuery(record, Query, properties, FieldsNames);

                    var com = new SqlCommand(Query.ToString(), connection);
                    //Create the sql parameters
                    if (_Encryptor != null)
                    {
                        if (record is IEncryptableClass)
                        {
                            _Encryptor.EncryptProperties((IEncryptableClass)record);
                        }
                    }
                    SetInsetParameters(record, properties, FieldsNames, com);

                    foreach (PropertyInfo property in properties)
                    {
                        DataInterface.Attribbutes.EntityKey KeyProperty = (DataInterface.Attribbutes.EntityKey)(from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.EntityKey) select attr).FirstOrDefault();
                        if (KeyProperty != null)
                        {
                            DataInterface.Attribbutes.Field FieldProperty = (DataInterface.Attribbutes.Field)(from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.Field) select attr).FirstOrDefault();
                            if (FieldProperty != null)
                            {
                                com.Parameters.AddWithValue($"@{FieldProperty.Name}", property.GetValue(record));
                            }
                            else
                            {
                                com.Parameters.AddWithValue($"@{property.Name}", property.GetValue(record));
                            }
                        }
                    }

                    com.ExecuteNonQuery();
                    //find the Key property of the entity

                    if (_Encryptor != null)
                    {
                        if (record is IEncryptableClass)
                        {
                            _Encryptor.Decryptproperties((IEncryptableClass)record);
                        }
                    }
                    connection.Close();
                }
                catch (Exception ex)
                {
                    if (connection.State == System.Data.ConnectionState.Open)
                    {
                        connection.Close();
                    }
                    throw;
                }
            }
            return(true);
        }
Пример #3
0
        private void FillresultsNew <T>(Type currentType, EntityCollection <T> records, SqlDataReader result) where T : class, IDataEntity
        {
            var CurrentTypeProperties = currentType.GetProperties().ToList();

            while (result.Read())
            {
                //now iterate through the properties
                //if the property has a maped field with Field attribute defined
                // we set the property value from that field
                //otherwise we se the property value from the field with name same as property's name
                T newEntity = (T)Activator.CreateInstance(typeof(T));

                for (int i = 0; i < CurrentTypeProperties.Count; i++)
                {
                    var property = CurrentTypeProperties[i];

                    DataInterface.Attribbutes.Field FieldAttribute = (DataInterface.Attribbutes.Field)(from attr in property.GetCustomAttributes(true) where attr.GetType() == typeof(DataInterface.Attribbutes.Field) select attr).FirstOrDefault();
                    string _fieldName;
                    if (FieldAttribute != null)
                    {
                        _fieldName = FieldAttribute.Name;
                    }
                    else
                    {
                        _fieldName = property.Name;
                    }


                    try
                    {
                        if (result[_fieldName] != DBNull.Value)
                        {
                            if (result[_fieldName].GetType() == typeof(Int64))
                            {
                                if (property.PropertyType == typeof(string))
                                {
                                    property.SetValue(newEntity, result[_fieldName].ToString());
                                }
                                else
                                {
                                    property.SetValue(newEntity, Convert.ToInt32(result[_fieldName]));
                                }
                            }
                            else
                            {
                                if (result[_fieldName].GetType() == typeof(Int32))
                                {
                                    if (property.PropertyType == typeof(string))
                                    {
                                        property.SetValue(newEntity, result[_fieldName].ToString());
                                    }
                                    else
                                    {
                                        property.SetValue(newEntity, result[_fieldName]);
                                    }
                                }
                                else
                                {
                                    property.SetValue(newEntity, result[_fieldName]);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        var aa = ex;
                    }
                }

                if (_Encryptor != null)
                {
                    if (newEntity is IEncryptableClass)
                    {
                        _Encryptor.Decryptproperties((IEncryptableClass)newEntity);
                    }
                }
                records.Add(newEntity);
            }
        }