Click here to Skip to main content
15,891,423 members
Articles / Programming Languages / XML

netTierGenerator

Rate me:
Please Sign up or sign in to vote.
4.81/5 (20 votes)
30 Nov 2008CPOL14 min read 67.6K   2.8K   108  
A 3-tier application framework and code generation tool - the way for rapid and effective development.
using System;
using System.Collections.Generic;
using Sample.Common.Exception;
using System.Text;

namespace Sample.Common.Util
{
    public static class DataConvertHelper
    {
        #region Get value from DB
        public static bool GetBoolValue(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (bool)val;
            }
            return false;
        }
        public static bool? GetNullableBoolValue(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (bool?)val;
            }
            return null;
        }
        public static DateTime GetDateTimeValue(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (DateTime)val;
            }
            return DateTime.MinValue;
        }
        public static DateTime? GetNullableDateTimeValue(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (DateTime?)val;
            }
            return null;
        }
        public static float GetFloatValue(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (float)val;
            }
            return float.MinValue;
        }
        public static float? GetNullableFloatValue(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (float?)val;
            }
            return null;
        }
        public static double GetDoubleValue(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (double)val;
            }
            return Double.MinValue;
        }
        public static double? GetNullableDoubleValue(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (double?)val;
            }
            return null;
        }
        public static decimal GetDecimalValue(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (decimal)val;
            }
            return Decimal.MinValue;
        }
        public static decimal? GetNullableDecimalValue(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (decimal?)val;
            }
            return null;
        }
        public static T GetEnumValue<T>(object val) where T : struct
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (T)Enum.Parse(typeof(T), val.ToString());
            }
            return default(T);
        }
        public static Guid GetGuidValue(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (Guid)val;
            }
            return Guid.Empty;
        }
        public static Guid? GetNullableGuidValue(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (Guid?)val;
            }
            return null;
        }
        public static short GetInt16Value(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (short)val;
            }
            return Int16.MinValue;
        }
        public static short? GetNullableInt16Value(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (short?)val;
            }
            return null;
        }
        public static int GetInt32Value(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (int)val;
            }
            return Int32.MinValue;
        }
        public static int? GetNullableInt32Value(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (int?)val;
            }
            return null;
        }
        public static long GetInt64Value(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (long)val;
            }
            return Int64.MinValue;
        }
        public static long? GetNullableInt64Value(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (long?)val;
            }
            return null;
        }
        public static long GetInt64Value(byte[] bytes)
        {
            if (bytes.Length != 8)
            {
                throw new BaseException("Can not convert Byte[] to Int64.");
            }

            long int64 = 0;
            for (int i = 7; i >= 0; i--)
            {
                int64 += (long)bytes[i] << (i * 8);
            }
            return int64;
        }
        public static string GetStringValue(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return Convert.ToString(val);
            }
            return null;
        }
        public static byte[] GetBinaryValue(long int64)
        {
            byte[] bytes = new byte[8];

            bytes[0] = (byte)(int64 & 255L);
            bytes[1] = (byte)((int64 & (255L << 8)) >> 8);
            bytes[2] = (byte)((int64 & (255L << 16)) >> 16);
            bytes[3] = (byte)((int64 & (255L << 24)) >> 24);
            bytes[4] = (byte)((int64 & (255L << 32)) >> 32);
            bytes[5] = (byte)((int64 & (255L << 40)) >> 40);
            bytes[6] = (byte)((int64 & (255L << 48)) >> 48);
            bytes[7] = (byte)((int64 & (255L << 56)) >> 56);

            return bytes;
        }
        public static byte[] GetBinaryValue(object val)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return (byte[])val;
            }
            return new byte[] {};
        }
        public static byte[] GetBinaryValue(object val, bool isGZipped)
        {
            if ((val != null) && (!DBNull.Value.Equals(val)))
            {
                return new byte[] {};
            }

            if (!isGZipped)
            {
                return (byte[])val;
            }

            byte[] result = GZipHelper.Decompress((byte[])val);
            return result;
        }
        #endregion

        #region Get value for DB
        public static object GetDbValue(bool val)
        {
            return val;
        }
        public static object GetDbValue(bool? val)
        {
            if (!val.HasValue)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(string val)
        {
            if (String.IsNullOrEmpty(val))
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(short val)
        {
            if (val == Int16.MinValue)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(short? val)
        {
            if (!val.HasValue)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(int val)
        {
            if (val == Int32.MinValue)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(int? val)
        {
            if (!val.HasValue)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(long val)
        {
            if (val == Int64.MinValue)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(long? val)
        {
            if (!val.HasValue)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static T GetDbValue<T>(T enumVal) where T : struct
        {
            return enumVal;
        }
        public static object GetDbValue(float val)
        {
            if (val == Single.MinValue)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(float? val)
        {
            if (!val.HasValue)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(double val)
        {
            if (val == Double.MinValue)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(double? val)
        {
            if (!val.HasValue)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(Guid val)
        {
            if (val == Guid.Empty)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(Guid? val)
        {
            if (!val.HasValue)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(DateTime val)
        {
            if (val == DateTime.MinValue)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(DateTime? val)
        {
            if (!val.HasValue)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(byte[] val)
        {
            if (val == null || val.Length == 0)
            {
                return DBNull.Value;
            }
            return val;
        }
        public static object GetDbValue(byte[] val, bool isGZipped)
        {
            if (val == null || val.Length == 0)
            {
                return DBNull.Value;
            }

            if (!isGZipped)
            {
                return val;
            }

            byte[] result = GZipHelper.Compress(val);
            return result;
        }
        #endregion

        #region Is NULL value
        public static bool IsNull(object val)
        {
            if (val == null)
            {
                return true;
            }
            Type type = val.GetType();

            if (type == typeof(Boolean))
            {
                return DataConvertHelper.IsNull((Boolean)val);
            }
            else if (type == typeof(String))
            {
                return DataConvertHelper.IsNull((String)val);
            }
            else if (type == typeof(Int32))
            {
                return DataConvertHelper.IsNull((Int32)val);
            }
            else if (type == typeof(Int64))
            {
                return DataConvertHelper.IsNull((Int64)val);
            }
            else if (type.IsEnum)
            {
                return DataConvertHelper.IsNull((Enum)val);
            }
            else if (type == typeof(Single))
            {
                return DataConvertHelper.IsNull((Single)val);
            }
            else if (type == typeof(Guid))
            {
                return DataConvertHelper.IsNull((Guid)val);
            }
            else if (type == typeof(DateTime))
            {
                return DataConvertHelper.IsNull((DateTime)val);
            }
            else if (type == typeof(Byte[]))
            {
                return DataConvertHelper.IsNull((Byte[])val);
            }
            else if (DBNull.Value.Equals(val))
            {
                return true;
            }
            return false;
            //throw new InvalidDataException("Incorrect type");
        }
        public static bool IsNull(bool val)
        {
            return false;
        }
        public static bool IsNull(string val)
        {
            if (String.IsNullOrEmpty(val))
            {
                return true;
            }
            return false;
        }
        public static bool IsNull(int val)
        {
            if (val == Int32.MinValue)
            {
                return true;
            }
            return false;
        }
        public static bool IsNull(long val)
        {
            if (val == Int64.MinValue)
            {
                return true;
            }
            return false;
        }
        public static bool IsNull(Enum val)
        {
            return false;
        }
        public static bool IsNull(float val)
        {
            if (val == Single.MinValue)
            {
                return true;
            }
            return false;
        }
        public static bool IsNull(double val)
        {
            if (val == Double.MinValue)
            {
                return true;
            }
            return false;
        }
        public static bool IsNull(Guid val)
        {
            if (val == Guid.Empty)
            {
                return true;
            }
            return false;
        }
        public static bool IsNull(DateTime val)
        {
            if (val == DateTime.MinValue)
            {
                return true;
            }
            return false;
        }
        public static bool IsNull(byte[] val)
        {
            if (val == null || val.Length == 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region Biwise operations
        public static bool CheckBitwiseValue(int valueToCheck, int valueBit)
        {
            if ((valueToCheck & valueBit) != valueBit)
            {
                return false;
            }
            return true;
        }
        public static bool CheckBitwiseValue(long valueToCheck, long valueBit)
        {
            if ((valueToCheck & valueBit) != valueBit)
            {
                return false;
            }
            return true;
        }
        public static void SetBitwiseValue(int valueToCheck, int valueBit, bool erase)
        {
            if (erase)
            {
                if (CheckBitwiseValue(valueToCheck, valueBit))
                {
                    valueToCheck ^= valueBit;
                }
            }
            else
            {
                valueToCheck |= valueBit;
            }
        }
        public static void SetBitwiseValue(long valueToCheck, long valueBit, bool erase)
        {
            if (erase)
            {
                if (CheckBitwiseValue(valueToCheck, valueBit))
                {
                    valueToCheck ^= valueBit;
                }
            }
            else
            {
                valueToCheck |= valueBit;
            }
        }
        #endregion Biwise operations

        #region Guid Helper
        public static bool TryParseGuid(string stringValue, out Guid guid)
        {
            guid = Guid.Empty;
            if (String.IsNullOrEmpty(stringValue))
            {
                return false;
            }

            try
            {
                guid = new Guid(stringValue);
            }
            catch 
            {
                return false;
            }

            return true;
        }
        #endregion

        #region DAL helper
        public static string JoinList<T>(IList<T> list, string delimeter)
        {
            return DataConvertHelper.JoinList<T>(list, delimeter, String.Empty);
        }
        public static string JoinList<T>(IList<T> list, string delimeter, string appender)
        {
            if (list == null)
            {
                throw new ArgumentNullException("list");
            }
            if (list.Count == 0)
            {
                return String.Empty;
            }

            StringBuilder result = new StringBuilder();

            bool isFirst = true;
            bool hasAppender = !String.IsNullOrEmpty(appender);
            foreach (T item in list)
            {
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    result.Append(delimeter);
                }

                if (hasAppender)
                {
                    result.Append(appender);
                }

                result.Append(item);

                if (hasAppender)
                {
                    result.Append(appender);
                }
            }

            return result.ToString();
        }
        #endregion DAL helper

        #region Validation

        public static void ValidateForNullArgument(string name, object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(name);
            }
        }

        #endregion Validation
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions