Пример #1
0
        private void LoadObject(long id)
        {
            var data = DatabaseProvider.GetAllById(nameTable, GetNamePrimaryKey(), id).GetAll().FirstOrDefault();

            if (data is null || !data.Any())
            {
                throw new Exception($"No result for query by id in '{nameTable}' value id '{id}'.");
            }

            var props = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var prop in props)
            {
                var column = prop.GetCustomAttribute <Column>();
                if (column != null)
                {
                    string name = (column.Name ?? prop.Name).Trim().ToUpper();
                    if (data.ContainsKey(name))
                    {
                        object value = data[name];

                        if (prop.PropertyType == typeof(bool))
                        {
                            prop.SetValue(this, Convert.ToChar(value) == 'S');
                        }
                        else
                        {
                            prop.SetValue(this, value == DBNull.Value ? null : value);
                        }
                    }
                }
            }

            foreach (var prop in props)
            {
                var joinColumn = prop.GetCustomAttribute <JoinColumn>();
                if (joinColumn != null)
                {
                    string name = joinColumn.NameColumnReference.Trim().ToUpper();
                    if (data.ContainsKey(name))
                    {
                        object value = data[name];
                        if (value != DBNull.Value)
                        {
                            var newObjectJoin = Activator.CreateInstance(prop.PropertyType, Convert.ToInt64(value));
                            prop.SetValue(this, newObjectJoin);
                        }
                    }
                }
            }
        }