September 2007 - Posts

A generic based DAL

During last period I was working on a little project about PDA with .NET Compact Framework. Working about a DAL I was thinking that almost all operation are very repetitive so I tried to create an architecture for create objects with the help of generics.

Every object implement thi interface for create himself:

using System;

namespace BusinessObjects
{
    public interface IBuildObject 
    {
        void CreateByDataRow(System.Data.IDataRecord dr);
    }
}

Every provider use this base class and inherits members for minimize effort in writing code:

using Interfaces;
using System.Data.SqlServerCe;
using System.Configuration;
using System.Data;
using BusinessObjects;

namespace SQLCEDataProvider
{
    public class SqlCeTransactionalDatabaseProvider<T> where T :  IBuildObject, new()
    {
        protected SqlCeConnection m_Conn;
        private string sub1 = "";
        private string sub2 = "";
       
        public SqlCeTransactionalDatabaseProvider()
        {
            //////this.m_Conn = new SqlCeConnection(@"Data Source =""\Program Files\MobileCE5\Gestionale.sdf""; Password =""pipponet""");
            this.m_Conn = new SqlCeConnection("Data Source ="
                            + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
                            + ("\\Gestionale.sdf;"
                            + ("Password =" + "\"pipponet\";"))));//tonio l'ho modificata io per astrarre dal particolare palmare...          
        }

        public SqlCeConnection Connection
        {
            get
            {    
                return m_Conn;
            }
        }
         
        private System.Data.SqlServerCe.SqlCeTransaction m_Trans;
        public SqlCeTransaction Transaction
        {
            get
            {
                if (m_Trans == null)
                    m_Trans = this.m_Conn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                return m_Trans;
            }
        }


        public IList<T> GetDataObjetList(string selectCommand)
        {
            IList<T> objs = new List<T>();
            System.Data.IDataReader dt;
            System.Data.SqlServerCe.SqlCeCommand da = new SqlCeCommand(selectCommand, this.Connection);

            this.Connection.Open();
            dt = da.ExecuteReader();

                while (dt.Read())
                {
                    T obj = new T();
                    obj.CreateByDataRow2(dt);
                    objs.Add(obj);
                }
 
            this.Connection.Close();
            da.Dispose();
            return objs; 
        }

        public void InsertObject(T obj, string insertCommand,)
        {
            ...  
        }
   }
}

 

This is an example of class that implements IBuildObject interface:

using System;
using System.Data;
using System.Collections;

namespace BusinessObjects
{
    public class Macchina : IBuildObject
    {
        string m_ID;
        public string CodMacch
        {
            get { return m_ID; }
            set { m_ID = value; }
        }

        string m_Località;
        public string Posizion
        {
            get { return m_Località; }
            set { m_Località = value; }
        }

        string m_Citta;
        public string Città
        {
            get { return m_Citta; }
            set { m_Citta = value; }
        }

        string m_BarCode;
        public string BarCode
        {
            get { return m_BarCode; }
            set { m_BarCode = value; }
        }

        string m_TipoMacchina; 
        public string Tipologia 
        {                         
            get { return m_TipoMacchina; }
            set { m_TipoMacchina = value; }
        }
        string m_Cliente;
        public string RagSocCl
        {
            get { return m_Cliente; }
            set { m_Cliente = value; }
        }
        string m_CodCliente;
        public string CodCli
        {
            get { return m_CodCliente; } 
            set { m_CodCliente = value; }
        }

        #region IBuildObject Members
        public void CreateByDataRow(System.Data.IDataRecord dr)
        {
            CodMacch = dr["ID"].ToString();
            BarCode = dr["BarCode"].ToString();
            Posizion = dr["Zona"].ToString();
            Città = dr["Citta"].ToString();
            RagSocCl = dr["ragionesociale"].ToString();
            Tipologia = dr["NomeTipo"].ToString();
            CodCli = dr["Cliente"].ToString();
        }


        #endregion 
    }
}


Base class accepts objects that implement IBuildObject interface and have a constructor. This is only a basic idea, we are imporving code more and more every day...:-)

using System;
using System.Collections.Generic;
using System.Text;
using Interfaces;
using System.Data.SqlServerCe;
using BusinessObjects;
using System.Windows.Forms;

namespace SQLCEDataProvider
{
    public class MacchinaDataProvider : SqlCeTransactionalDatabaseProvider<Macchina>, IMacchinaDataProvider
    {

        public IList<Macchina> GetAll()
        {
            IList<Macchina> objs;
            objs = this.GetDataObjetList("SELECT Macchina.ID, Macchina.BarCode, Posizione.Zona, Macchina.Citta, Clienti.ragionesociale, TipoMacchina.NomeTipo, Macchina.Cliente FROM Clienti INNER JOIN Macchina ON Clienti.ID=Macchina.Cliente INNER JOIN TipoMacchina ON TipoMacchina.ID=Macchina.TipoMacchina INNER JOIN Posizione ON Posizione.ID=Macchina.Posizione Order by Macchina.ID", "");       
            return objs;
        }

        public IList<Macchina> GetIDMacchTipoMByCliente(string cli)
        {
            IList<Macchina> objs = null;
            objs = this.GetDataObjetList("SELECT Macchina.ID, TipoMacchina.NomeTipo FROM Macchina INNER JOIN TipoMacchina ON TipoMacchina.ID=Macchina.TipoMacchina WHERE (Macchina.Cliente = @cli) Order by Macchina.ID", cli);
            return objs;
        }
       #endregion
     

    }
}
 

Every new dataprovider class implements too an other interface with other members.

I hope that you enjoy this basic idea...

Antonio 

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP
  • Breaking News
  • Find a Job
  • Social Bookmarking
    Tidebuy Reviews
    Online Shopping
    asp.net hosting
    UK online local dating