three layered structure in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dotnetnovice
    New Member
    • Aug 2009
    • 21

    #1

    three layered structure in C#

    Hi...I am new to object oriented and also to n-tier.
    I have developed a small program based on 3 tier structure. what i want is some help from professionals that either i worked well or there is a need of improvement.

    here is the Common class which contains members.
    Code:
     public class Common
        {
          public string Id;
          public string Name;
          public string Phone;
          public int Salary;     
        }
    Ist tier i.e. GUI

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                Common Obj1 = new Common();
                Obj1.Id     = textBox1.Text;
                Obj1.Name   = textBox2.Text;
                Obj1.Phone  = textBox3.Text;
                Obj1.Salary = Convert.ToInt16(textBox4.Text);
    
                BPL Obj2 = new BPL();
                Obj2.GetAllFrmPresentation(Obj1);
               
            }
    Business Process Layer

    Code:
    public class BPL
        {
            
            public void GetAllFrmPresentation(Common a)
    
            {
                
                //Suppose Tax rate is 15%
                int tax;
                tax = ((a.Salary * 15) / 100);
                a.Salary = a.Salary - tax;
                
                DAL Obj = new DAL();
                Obj.GetAllFrmBPL(a);           
            }
        }

    and last Data Access Layer

    Code:
    public void GetAllFrmBPL(Common b)
           {
               SqlConnection con = new SqlConnection(ConnectionString);
               SqlCommand objSql = new SqlCommand();
               objSql.Connection = con;
               objSql.CommandType = CommandType.Text;
               objSql.CommandText = "INSERT INTO BasicInfo (_Id, _Name, _Phone, _Salary) VALUES (@Id, @Name, @Phone, @Salary);";
    
               objSql.Connection = con;
    
               objSql.Parameters.Add("@Id", SqlDbType.Int).Value = b.Id;
               objSql.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = b.Name;
               objSql.Parameters.Add("@Phone", SqlDbType.NVarChar, 50).Value = b.Phone;
               objSql.Parameters.Add("@Salary", SqlDbType.Int).Value = b.Salary;
    
               try
               {
                   con.Open();
                   objSql.ExecuteNonQuery();
               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }
               finally
               {
                   con.Close();
               }
              
               
           }

    Thanks in advance
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Your question is too broad a topic for this forum, however, I will point something out to you that should be addressed:

    You aren't using Properties. You're directly setting members, which is a bad idea for maintainability . Set up your properties like this:
    Code:
     public class Common
    {
        public string Id {get; set;}
        public string Name {get; set;}
        public string Phone {get; set;}
        public int Salary {get; set;}
    }
    The idea behind that is the same reason people use getter and setter methods in Java. Because if you one day need to do something with the values before you get or set them, you have to refactor a lot of code to fix the issue if you don't use them. If you do use them, you just change the method/property.

    Also, Id is a string, but later you are using it as an int. And Salary is an int (which is actually an Int32), but you're parsing the textbox as a short (Int16).

    You're using a "magic number" in your tax rate area. Things like that should be set as constants, or even better, in an app.config.

    Comment

    • dotnetnovice
      New Member
      • Aug 2009
      • 21

      #3
      Yes i know there are lots of datatype flaws in my program but here i just want ensurity about three layers in the program either i have done it in correct way like getting data from GUI and passing it ti BPL in BPL i process the data and then pass it to DAL. i just need help about the structure

      Thanks

      Comment

      Working...