Click here to Skip to main content
15,895,370 members
Articles / Programming Languages / SQL

Abstract Data Access Layer Design

Rate me:
Please Sign up or sign in to vote.
4.85/5 (16 votes)
5 Sep 2009CPOL4 min read 73.3K   1.2K   68  
The present document tries to describe the architecture of a specific layer of access to data for relational databases. This document tries to present/display a form to automate tasks of access to data.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace XPeriment.DataAccessTasks.SqlServer
{
    public class SqlTaskPerformer:DataTaskPerformer
    {
        private SqlConnection currentconnection = null;

        public override ScriptBuilder ScriptBuilder
        {
            get { return new ScriptBuilder(); }
        }

        public override void Connect()
        {
            if (currentconnection == null)
                currentconnection = new SqlConnection(ConnectionString);
            if (currentconnection.State != ConnectionState.Open)
                currentconnection.Open();
        }

        private DataTable RunQuery(QueryTask query)
        {
            Connect();
            DataTable retval = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(ScriptBuilder.GetScript(query), currentconnection);
            da.Fill(retval);
            return retval;
        }

        private void RunUpdate(UpdateTask task)
        {
            Connect();
            string cmdstring = ScriptBuilder.GetScript(task);
            SqlCommand cmd = null;
            if (task.CurrentTransaction != null)
                cmd = new SqlCommand(cmdstring, currentconnection, (task.CurrentTransaction as SqlPerformerTransaction).SqlTransaction);
            else
                cmd = new SqlCommand(cmdstring, currentconnection);
            cmd.ExecuteNonQuery();
        }

        private void RunDelete(DeleteTask task)
        {
            Connect();
            string cmdstring = ScriptBuilder.GetScript(task);
            SqlCommand cmd = null;
            if (task.CurrentTransaction != null)
                cmd = new SqlCommand(cmdstring, currentconnection, (task.CurrentTransaction as SqlPerformerTransaction).SqlTransaction);
            else
                cmd = new SqlCommand(cmdstring, currentconnection);
            cmd.ExecuteNonQuery();
        }

        private void RunInsert(InsertTask task)
        {
            Connect();
            string cmdstring = ScriptBuilder.GetScript(task) + " SELECT SCOPE_IDENTITY() ";
            SqlCommand cmd = null;
            if (task.CurrentTransaction != null)
                cmd = new SqlCommand(cmdstring, currentconnection, (task.CurrentTransaction as SqlPerformerTransaction).SqlTransaction);
            else
                cmd = new SqlCommand(cmdstring, currentconnection);
            object identity = cmd.ExecuteScalar();
            task.ScopeIdentity = (identity is decimal)?(decimal)identity:0;
        }

        public override PerformerTransaction CreateTransaction()
        {
            Connect();
            return new SqlPerformerTransaction(currentconnection.BeginTransaction());
        }

        public override DataTable Run(BaseTask task)
        {
            if (task is QueryTask)
                return RunQuery(task as QueryTask);
            else if (task is InsertTask)
                RunInsert(task as InsertTask);
            else if (task is UpdateTask)
                RunUpdate(task as UpdateTask);
            else if (task is DeleteTask)
                RunDelete(task as DeleteTask);
            else
                throw new Exception("Data access task type not supported");
            return null;
        }

    }
}

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
Systems Engineer
Colombia Colombia
http://www.construirsoftware.blogspot.com/

Comments and Discussions