Richard Baluyut
Richard Baluyut

Reputation: 37

Conversion of 2 Tier Architecture to 3 Tier Architecture

Yes, this is already asked question, and possible to be close. But i want a accurate answers.

I've created a simple system using C#, i used 2 Tier Architecture, now i want is convert to 3 Tier Architecture, I read a lot of documentations, Blogs, and Question to StackOverflow. I want to know if my conversion is correct, or if not please let me know.

Any ideas will be a big help.

This is my code in 2 Tier Architecture :

 dtpfromdate.MaxDate = DateTime.Today;
 dtptodate.MaxDate = DateTime.Today;
 dtpfromdate.Value = DateTime.Today;
 dtptodate.Value = DateTime.Today;
 this.Text = FormName;     

 Db = new DBLayer.DBLayer();
 cmbRD.DataSource = Db.getRSetSQL("exec IMC_DCNE..usp_Get_Shift_Timing", DBLayer.DBLayer.SqlType.SqlQuery);
 cmbRD.DisplayMember = "Shift_Complete_Details";
 cmbRD.ValueMember = "Shift_Name";

This is my code when i convert it to 3 Tier Architecture :

 dtpfromdate.MaxDate = DateTime.Today;
 dtptodate.MaxDate = DateTime.Today;
 dtpfromdate.Value = DateTime.Today;
 dtptodate.Value = DateTime.Today;
 this.Text = FormName;

 Generate_Report_BL obj_Generate_Report = new Generate_Report_BL();
 Shift_Timing shift = new Shift_Timing();
 List<Shift_Timing> ShiftList = obj_Generate_Report.Get_Shift_Timing("Shift_Name", "Shift_Complete_Details");
 cmbRD.DisplayMember = shift.Shift_Name;
 cmbRD.ValueMember = shift.Shift_Complete_Details;

This is my Business Layer :

public class Generate_Report_BL
{
    public List<Shift_Timing> Get_Shift_Timing(string strShift_tName, string str_Shift_Complete_Details)
    {
        List<Shift_Timing> lstUser = new List<Shift_Timing>();
        try
        {
            DataSet ds = Application_Level_Processing_DL.Get_Shift_Timing(strShift_tName,str_Shift_Complete_Details);
            if (ds != null && ds.Tables.Contains("Shift_Timing"))
            { 
                lstUser = new Shift_Timing().getShift_Timing(ds.Tables["Shift_Timing"]);
            }
        }
        catch
        {
            throw;
        }
        return lstUser;
    }
}

This is my code for Data Layer :

 public static DataSet Get_Shift_Timing(string strShift_tName, string str_Shift_Complete_Details)
    {
            DataSet dsReturn = new DataSet();
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetStoredProcCommand("usp_Get_Shift_Timing");
            db.AddInParameter(command, "@Shift_Name", DbType.String, strShift_tName);
            db.AddInParameter(command, "@Shift_Complete_Details", DbType.String, str_Shift_Complete_Details);
            db.ExecuteDataSet(command);
            if (dsReturn != null && dsReturn.Tables.Count > 0)
            {
                dsReturn.Tables[0].TableName = "Shift_Timing";
            }
            return dsReturn;
    }

And this is the class :

public class Shift_Timing
{
    public int Shift_ID { get; set; }
    public string Shift_Complete_Details { get; set; }
    public string Shift_Name { get; set; }
    public string Shift_From { get; set; }
    public string Shift_To { get; set; }
    public string RD_Code { get; set; }
    public string From_Date { get; set; }
    public string To_Date { get; set; }

    public List<Shift_Timing> getShift_Timing(DataTable dtShiftTime)
    {
        List<Shift_Timing> lstEntry = new List<Shift_Timing>();
        try
        {
            if (dtShiftTime.Rows.Count > 0)
            {
                lstEntry = dtShiftTime.AsEnumerable().Select(r => new Shift_Timing
                {
                    Shift_ID = r.Field<int>("Shift_ID"),
                    Shift_Complete_Details = r.Field<string>("Shift_Complete_Details"),
                    Shift_Name = r.Field<string>("Shift_Name"),
                    Shift_From = r.Field<string>("Shift_From"),
                    Shift_To = r.Field<string>("Shift_To"),
                    RD_Code = r.Field<string>("RD_Code"),
                    From_Date = r.Field<string>("From_Date"),
                    To_Date = r.Field<string>("To_Date"),
                }).ToList();
            }
        }
        catch
        {
            throw;
        }
        return lstEntry;
    }
}

Upvotes: 1

Views: 683

Answers (1)

rahulaga-msft
rahulaga-msft

Reputation: 4164

To me overall conversion looks OK with couple of suggestions (IMHO) :

  • There should be Business Object shared between Data layer and Business Layer. Idea is that if you want to change your data access methodology tomorrow, it should not impact your business layer. Basically your core business logic should have no impact on external factors like how you persist data, how you represent data in UI etc.
  • Above point is also applicable when you share contracts between BL and UI (Shift_Timing in your example) Usually this can be achieved via service adapter in between which can take care of converting BO to UI Model and vice versa.
  • You might want to consider injecting dependency of your data layer to your BL and access it via interface. This would make your design extensible.

Upvotes: 1

Related Questions