开发者

Singleton Data Access Object (Dao) & SQL Helper Instance, Is here any Performance drawback?

开发者 https://www.devze.com 2023-03-12 05:18 出处:网络
I need comments from Experts. I write my own SQL Helper Class to Communicate with DB. Why I use it as because I try to

I need comments from Experts. I write my own SQL Helper Class to Communicate with DB.

Why I use it as because I try to

  1. Encapsulate the Ado.Net logic.
  2. Try to set a common standard for my developer in terms of DAL coding.
  3. Flexible & Easy to use.
  4. Same kind of code block for SQL Server/ Oracle / Access / Excel / Generic Database code block approach (SQL Server & Oracle) e.t.c.
  5. Plug & Play or Reusable approach.

In terms of code optimization

  1. This helper class or Assembly is CLS Compliant.
  2. It pass Successfully by FxCop / Static Code Analysis.

I give you the sample Code Block (DAO). Please check bellow

                   using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Data;

        #region include SQL Helper Namespace
        using DBManager;
        #endregion

        #region include SQL Helper Namespace
        using DBManager;
        #endregion


namespace ContentManagementSystem.DataAccessLayer
{
/// <summary>
/// 
/// </summary>
public sealed class DaoContentOwner : DataAccessBase
{
    #region CONSTRUCTOR
    /// <summary>
    /// 
    /// </summary>
    private DaoContentOwner()
    {
        GetInstance = new DaoContentOwner();
    }
    #endregion

    //############################################# M E T H O D S ##########################

    #region Retrieve Content Owner
    /// <summary>
    /// Retrieve Content Owner
    /// </summary>
    /// <returns></returns>
    publ开发者_高级运维ic static DataTable RetrieveContentOwner()
    {
        DataTable dt = null;

        try
        {
            using (DBQuery dq = new DBQuery("stpGetContentOwner"))
            {

                dt = dq.ResultSetAsDataTable();

                return dt;
            }
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            if (dt != null)
            {
                dt.Dispose();
            }
        }
    }
    #endregion


    //############################################# P R O P E R T Y ########################

    #region READ ONLY PROPERTY
    /// <summary>
    /// GetInstance of DaoContentOwner Class
    /// </summary>
    public static DaoContentOwner GetInstance { get; private set; }
    #endregion
}

}


Justification:

"DataAccessBase" It is a Abstract class. Implement IDisposable Interface.

"DBQuery" is SQL Helper for SQL Server Only. It is a Sealed class. It is takes T-SQL / SP according to needs. Open an close database connection. No need to handel any thing by developer.

Why I make DAO Singleton Class, Because my all the methods withing DAO is a static method. I order to memory optimization I make it Singleton. It is also a design Principal.

Note: Needs comments. Whether need to change in the design or some thing is wrong that need to correct.


Personally I would go with:

DALFileItem dalF = new DALFileItem(DSN);
List<FileItem> list = dalF.GetAll(""); 
  • Allows accessing multiple databases, without making DSN static member.
  • In multithreaded environment only one thread will have access to the static method.
  • More object oriented: you can add interfaces, base classes and generics easier that way.
0

精彩评论

暂无评论...
验证码 换一张
取 消