Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of BAL in 3 tier architecture?How to call methods from DAL to BAL

I am a newbie to 3 tier architecture and below is my DAL code

public static int Insert(string firstname, string lastname, DateTime dob, string gender,string email, string password)
    {
        // bool flag = false;
        SqlParameter pid;
        SqlParameter result;

        SqlConnection con = Generic.DBConnection.OpenConnection();

        try
        {

            SqlCommand cmd1 = new SqlCommand("Insertreg", con);
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.AddWithValue("@FirstName", firstname);
            cmd1.Parameters.AddWithValue("@LastName", lastname);
            cmd1.Parameters.AddWithValue("@Dob", dob);
            cmd1.Parameters.AddWithValue("@Gender", gender);
           cmd1.Parameters.AddWithValue("@EmailId", email);
            cmd1.Parameters.AddWithValue("@Password", password);
            result = cmd1.Parameters.Add("@result", System.Data.SqlDbType.Int);
            result.Direction = System.Data.ParameterDirection.Output;
            pid = cmd1.Parameters.Add("@id", System.Data.SqlDbType.Int);
            pid.Direction = System.Data.ParameterDirection.Output;
            return cmd1.ExecuteNonQuery();


            con.Close();

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

this in BAL

 public int insert(string firstname,string lastname,DateTime dob,string gender,string email,string password)
    {
      ProfileMasterDAL dal=new ProfileMasterDAL();
      try
      {
          return ProfileMasterDAL.Insert(firstname, lastname, dob, gender,email, password);
      }
      catch (Exception ex)
      {

          throw ex;
      }
        finally
      {
          dal = null;
      }

    }

I my UI

  ProfileMasterBLL pmBLL = new ProfileMasterBLL();
 pmBLL.insert(firstname, lastname, dob, gender, mobile, country, state, email, password);

Is this the correct way to code in 3 tier??I mean how to call methods from DAL to BAL and into UI?If not suggest me some good way.Thanks.

like image 589
chandra sekhar Avatar asked Aug 31 '12 11:08

chandra sekhar


People also ask

What is dal and Bal?

BAL = Business Application Layer. DAL = Data Access Layer.

What is Bal in C #?

BAL (Business Access Layer) This layer defines a function or class to use to transfer data into Data Access Layer and Presentation Access Layer.

What is Dal in asp net?

This separate layer is referred to as the Data Access Layer, DAL for short, and is typically implemented as a separate Class Library project.


1 Answers

Normally I do the following:

  1. Define a Business Layer (BL, you call it BAL). This contains the definitions of you business entities. It also defines interfaces to retrieve/save/delete data for whatever patterns you use (repository, context, etc).
  2. Define a Data Access Layer (DAL). This contains the actual implementation for the retrieve/save/delete interfaces.
  3. Define a UI layer. This contains UI elements (forms, controls, models, controllers, etc), which can use the BL to load data.

The references are the following:

  1. The BL doesn't know the DAL or the UI.
  2. The DAL knows the BL. The DAL does not know the UI.
  3. THe UI knows the BL. The UI does not know the DAL.

The big question for you probably is, how does the BL retrieve/save/delete data when it doesn't know the DAL, and therefore cannot create an instance of a class in the DAL. Well, this is where a little Dependency Injection comes in handy. All you have to wire up is the injection of the DAL-class to the BL-interface.

Hope this makes sense. I use it as my standard 3-tier implementation, and it works absolutely without problems. Specifically, I use Entity Framework with POCO for entities, and the DI I use is a custom one, but any of the ones out there will do.

UPDATE

The BL does not know the DAL.

  • The BL defines an interface (lets call it IRepository) which it can use to do what it needs to do.
  • The DAL defines a class (Repository) which implements the interface IRepository. So the actual implementation of the repository is in the DAL.
  • Obviously the BL cannot create an instance of the repository directly. This is where dependency injection comes in, this allows the developer to create an instance of a class where it normally cannot be done. A simple crude version of this, is to use reflection.

I hope this makes more sense.

like image 68
Maarten Avatar answered Oct 01 '22 22:10

Maarten