Click here to Skip to main content
15,885,757 members
Articles / Programming Languages / C#

Data Accessing Independent From Database And Entity With ADO.Net

Rate me:
Please Sign up or sign in to vote.
4.93/5 (10 votes)
26 Nov 2012CPOL11 min read 26.7K   1K   32  
This tutorial aims to show a basic approach for designing a data access layer independent from databases and entities.
//Copyright 2012, Halil İbrahim Kalyoncu, www.ibrahimkalyoncu.com
//License: BSD
//Contact: halil@ibrahimkalyoncu.com, ibrahimkalyoncu@hotmail.com.tr
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace DataAccess.Helper
{
    public class DbTypeConverter
    {
        #region Private Static Fields
        private static Dictionary<Type,DbType> typeMap = new Dictionary<Type, DbType>();
        #endregion

        #region CTOR
        static DbTypeConverter()
        {
            InitTypes();
        }
        #endregion

        #region Private Static Methods
        private static void InitTypes()
        {
            typeMap[typeof(byte)] = DbType.Byte;
            typeMap[typeof(sbyte)] = DbType.SByte;
            typeMap[typeof(short)] = DbType.Int16;
            typeMap[typeof(ushort)] = DbType.UInt16;
            typeMap[typeof(int)] = DbType.Int32;
            typeMap[typeof(uint)] = DbType.UInt32;
            typeMap[typeof(long)] = DbType.Int64;
            typeMap[typeof(ulong)] = DbType.UInt64;
            typeMap[typeof(float)] = DbType.Single;
            typeMap[typeof(double)] = DbType.Double;
            typeMap[typeof(decimal)] = DbType.Decimal;
            typeMap[typeof(bool)] = DbType.Boolean;
            typeMap[typeof(string)] = DbType.String;
            typeMap[typeof(char)] = DbType.StringFixedLength;
            typeMap[typeof(Guid)] = DbType.Guid;
            typeMap[typeof(DateTime)] = DbType.DateTime;
            typeMap[typeof(DateTimeOffset)] = DbType.DateTimeOffset;
            typeMap[typeof(byte[])] = DbType.Binary;
            typeMap[typeof(byte?)] = DbType.Byte;
            typeMap[typeof(sbyte?)] = DbType.SByte;
            typeMap[typeof(short?)] = DbType.Int16;
            typeMap[typeof(ushort?)] = DbType.UInt16;
            typeMap[typeof(int?)] = DbType.Int32;
            typeMap[typeof(uint?)] = DbType.UInt32;
            typeMap[typeof(long?)] = DbType.Int64;
            typeMap[typeof(ulong?)] = DbType.UInt64;
            typeMap[typeof(float?)] = DbType.Single;
            typeMap[typeof(double?)] = DbType.Double;
            typeMap[typeof(decimal?)] = DbType.Decimal;
            typeMap[typeof(bool?)] = DbType.Boolean;
            typeMap[typeof(char?)] = DbType.StringFixedLength;
            typeMap[typeof(Guid?)] = DbType.Guid;
            typeMap[typeof(DateTime?)] = DbType.DateTime;
            typeMap[typeof(DateTimeOffset?)] = DbType.DateTimeOffset;
        }
        #endregion

        #region Public Static Methods
        public static DbType ConvertFromSystemType(Type systemType)
        {
            if (typeMap.ContainsKey(systemType))
                return typeMap[systemType];
            else
                throw new InvalidCastException("The system type is not convertable.");
        }
        #endregion
    }
}

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 RkSoft GIS Solutions
Turkey Turkey
A recent graduate. Interested in .Net technologies especially on c# language, silverligt and wpf. Also jQuery is one of the interests. Now improwing skills in a development company and learning more everyday.

Comments and Discussions