public static Dictionary <string, string> ExpressionAttriubtes(this Type entityType)
        {
            var properties = entityType.GetProperties();

            var attributes = new Dictionary <string, string>();

            foreach (var property in properties)
            {
                bool isExcludeProp = false;
                foreach (var attr in property.GetCustomAttributes(true))
                {
                    ExcludeAttribute ex = attr as ExcludeAttribute;
                    if (ex != null)
                    {
                        isExcludeProp = true;
                        break;
                    }
                }

                if (!isExcludeProp)
                {
                    attributes.Add(GetPropertyAttribute(property.Name), property.Name);
                }
            }

            return(attributes);
        }
예제 #2
0
        /// <summary>
        /// Syncs all data in the Todo data model class to the underlying row/record source linked to the model.
        /// </summary>
        public void SyncDataToSource()
        {
            try
            {
                // Get array of class properties.
                var properties = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

                foreach (var p in properties)
                {
                    ExcludeAttribute attr     = p.GetCustomAttribute <ExcludeAttribute>();
                    bool             blBypass = (attr == null) ? false : true;

                    if (!blBypass)
                    {
                        string strFieldName = p.Name;
                        var    value        = p.GetValue(this, null);

                        m_source[strFieldName] = value;
                    } //end if
                }     //next p
            }
            catch (Exception err)
            {
                ErrorHandler.ShowErrorMessage(err, "Error in SyncDataToSource function of TodoDataModel class.");
            }
        }
예제 #3
0
        public IQueryCriteria<T> Select<M>()
        {
            base.SetupRequest<T>();
            var columns = string.Empty;
            var entityColumns = typeof(T).GetProperties().Select(x => x.Name);
            foreach (var prop in typeof(M).GetProperties())
            {
                bool isExcludeProp = false;
                foreach (var attr in prop.GetCustomAttributes(true))
                {
                    ExcludeAttribute ex = attr as ExcludeAttribute;
                    if (ex != null)
                    {
                        isExcludeProp = true;
                        break;
                    }
                }

                if (!isExcludeProp || entityColumns.Any(x => x == prop.Name))
                {
                    columns = string.Concat(columns, prop.Name, ",");
                }
            }

            columns = columns.TrimEnd(',');
            var columnList = columns.Split(',').ToList();

            _queryRequest.AttributesToGet = columnList;

            return this;
        }
예제 #4
0
        public IQueryCriteria <T> Select <M>()
        {
            _columnString = string.Empty;
            var entityColumns = typeof(T).GetProperties().Select(x => x.Name);

            foreach (var prop in typeof(M).GetProperties())
            {
                bool isExcludeProp = false;
                foreach (var attr in prop.GetCustomAttributes(true))
                {
                    ExcludeAttribute ex = attr as ExcludeAttribute;
                    if (ex != null)
                    {
                        isExcludeProp = true;
                        break;
                    }
                }

                if (!isExcludeProp || entityColumns.Any(x => x == prop.Name))
                {
                    _columnString = string.Concat(_columnString, prop.Name, ",");
                }
            }

            _columnString = _columnString.TrimEnd(',');

            return(this);
        }
        private string BuildColumnsAndValues()
        {
            if (_transactionType == SqlTransactionType.Insert)
            {
                var columns = new List <string>();
                var values  = new List <string>();
                foreach (var prop in typeof(T).GetProperties())
                {
                    bool isKeyProperty = false;
                    bool isExcludeProp = false;
                    var  attributes    = prop.GetCustomAttributes(true);
                    foreach (var attr in attributes)
                    {
                        KeyAttribute key = attr as KeyAttribute;
                        if (key != null)
                        {
                            isKeyProperty = true;
                            break;
                        }

                        ExcludeAttribute ex = attr as ExcludeAttribute;
                        if (ex != null)
                        {
                            isExcludeProp = true;
                            break;
                        }
                    }

                    if (!isKeyProperty && !isExcludeProp)
                    {
                        columns.Add(prop.Name);
                        values.Add(string.Concat("'", _entity.GetType().GetProperty(prop.Name).GetValue(_entity, null), "'"));
                    }
                }

                var columnString = string.Join(",", columns.ToArray());
                columnString = string.Concat("(", columnString, ")");

                var valueString = string.Join(",", values.ToArray());
                valueString = string.Concat("VALUES (", valueString, ")");

                var result = string.Concat(columnString, valueString, " SELECT SCOPE_IDENTITY()").Replace("''", "NULL");
                return(result);
            }

            if (_transactionType == SqlTransactionType.Update)
            {
                var    updateDictionary = new Dictionary <string, string>();
                string keyName          = string.Empty;

                foreach (var prop in typeof(T).GetProperties())
                {
                    bool isKeyProperty = false;
                    bool isExcludeProp = false;
                    var  attributes    = prop.GetCustomAttributes(true);
                    foreach (var attr in attributes)
                    {
                        KeyAttribute key = attr as KeyAttribute;
                        if (key != null)
                        {
                            isKeyProperty = true;
                            break;
                        }

                        ExcludeAttribute ex = attr as ExcludeAttribute;
                        if (ex != null)
                        {
                            isExcludeProp = true;
                            break;
                        }
                    }

                    if (!isKeyProperty && !isExcludeProp)
                    {
                        updateDictionary.Add(
                            prop.Name,
                            string.Concat("'", _entity.GetType().GetProperty(prop.Name).GetValue(_entity, null), "'"));
                    }
                    else if (isKeyProperty)
                    {
                        keyName = prop.Name;
                    }
                }

                string setString = string.Empty;
                foreach (var item in updateDictionary)
                {
                    setString = string.Concat(setString,
                                              string.Format(" {0} = {1},", item.Key, item.Value));
                }
                setString = setString.TrimEnd(',');

                return(string.Format(" SET {0} WHERE {1} = {2}", setString, keyName, _entity.GetId()).Replace("''", "NULL"));
            }

            if (_transactionType == SqlTransactionType.Delete)
            {
                string keyName = string.Empty;
                foreach (var prop in typeof(T).GetProperties())
                {
                    bool isKeyProperty = false;
                    var  attributes    = prop.GetCustomAttributes(true);
                    foreach (var attr in attributes)
                    {
                        KeyAttribute key = attr as KeyAttribute;
                        if (key != null)
                        {
                            isKeyProperty = true;
                            break;
                        }
                    }

                    if (isKeyProperty)
                    {
                        keyName = prop.Name;
                    }
                }

                return(string.Format(" WHERE {0} = {1}", keyName, _entity.GetId()));
            }

            return(string.Empty);
        }