Tuesday, July 3, 2012

.NET N-Tier Architecture


1- Business Logic Layer 
    it is a class 
    we have table in database called Branch with columns(pk_branch_id,mobile,branch_name,address,notes)
    so for each column or field we create property to set and get values


public class BusinessLogicLayer
{
    #region Variables

    private int pk_branch_id, mobile;
    private string branch_name, address, notes;

    #endregion

    #region Properties

    public int Branch_ID
    {
        get { return pk_branch_id; }
        set { pk_branch_id = value; }
    }

    public string Branch_Name
    {
        get { return branch_name; }
        set { branch_name = value; }
    }

    public string Branch_Address
    {
        get { return address; }
        set { address = value; }
    }
    public int Branch_Mobile
    {
        get { return mobile; }
        set { mobile = value; }
    }

    public string Branch_Notes
    {
        get { return notes; }
        set { notes = value; }
    }

    #endregion


2 - Data Access Layer
      it is a class
      which is responsible for creating connection , open, close it, create command and execute it

public class DataAccessLayer
{
    SqlConnection conn;
    SqlCommand cmd;
    public DataAccessLayer() // Constructor
    {
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ADSL_DBConnectionString"].ConnectionString);
        conn.Open();
    }

    public bool Insert_Branch(BusinessLogicLayer bll_branch_INS)
    {
        using (cmd = new SqlCommand("PRC_INSERT_BRANCH", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            
            cmd.Parameters.AddWithValue("@Branch_Name", bll_branch_INS.Branch_Name);
            cmd.Parameters.AddWithValue("@Mobile", bll_branch_INS.Branch_Mobile);
            cmd.Parameters.AddWithValue("@Address", bll_branch_INS.Branch_Address);
            cmd.Parameters.AddWithValue("@Notes", bll_branch_INS.Branch_Notes);
            int nn = cmd.ExecuteNonQuery();
            if (nn > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public bool Update_Branch( BusinessLogicLayer  bll_branch_UPD)
    {
        using (cmd = new SqlCommand("PRC_UPDATE_BRANCH", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Pk_Branch_Id", bll_branch_UPD.Branch_ID);

            cmd.Parameters.AddWithValue("@Branch_Name", bll_branch_UPD.Branch_Name);
            cmd.Parameters.AddWithValue("@Mobile", bll_branch_UPD.Branch_Mobile);
            cmd.Parameters.AddWithValue("@Address", bll_branch_UPD.Branch_Address);
            cmd.Parameters.AddWithValue("@Notes", bll_branch_UPD.Branch_Notes);
            
            if (cmd.ExecuteNonQuery() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public Boolean Delete_Branch(int id)
    {
        using (cmd = new SqlCommand("PRC_DELETE_BRANCH", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Pk_Branch_Id", id);

            if (cmd.ExecuteNonQuery() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public void Select_Branch_BYID(int id)
    {
        using (cmd = new SqlCommand("PRC_SELECTBRANCH_BYID", conn))
        {
            BusinessLogicLayer  bll_branch = new BusinessLogicLayer();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Pk_Branch_Id", id);
            SqlDataReader rdr;
            rdr = cmd.ExecuteReader();
            if (rdr.Read())
            {
            bll_branch.Branch_Name = rdr["Branch_Name"].ToString();
            bll_branch.Branch_Mobile = Convert.ToInt32(rdr["Mobile"]);
            bll_branch.Branch_Address = rdr["Address"].ToString();
            bll_branch.Branch_Notes = rdr["Notes"].ToString();
            }
        }
    }

    public void Fill_GridView(GridView GV)
    {
        using (cmd = new SqlCommand("PRC_FILL_GV", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            SqlDataAdapter adpr = new SqlDataAdapter(cmd);
            adpr.Fill(dt);
            GV.DataSource = dt;
            GV.DataBind();
        }
    }

    public void Fill_DropDownList(DropDownList DDL, string datatext, string datavalue)
    {
        using (cmd = new SqlCommand("PRC_FILL_DDR", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader rdr;
            rdr = cmd.ExecuteReader();
            DDL.DataSource = rdr;
            DDL.DataTextField = datatext;
            DDL.DataValueField = datavalue;
            DDL.DataBind();
        }
    }

}

No comments:

Post a Comment