HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

3 layer design and multiple dataset

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
layerdesigndatasetmultipleand

Problem

I'm new to C# and OO programing.
I have an aspx page with 3 lists of checkboxes and I would like to generate them from the DB.
how should I structure my code?

Here is an example of what I did so far:

-
ASPX page



-
ASPX Code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GenerateCheckBoxList1();
        GenerateCheckBoxList2();
    }      
}

private void GenerateCheckBoxList1()
{
    CBLists cbl = new CBLists();
    CheckBoxList1.DataSource = cbl.GetCheckBoxList1();
    CheckBoxList1.DataTextField = "name";
    CheckBoxList1.DataValueField = "id";
    CheckBoxList1.DataBind();
}

private void GenerateCheckBoxList2()
{
    CBLists cbl = new CBLists();
    CheckBoxList2.DataSource = cbl.GetCheckBoxList2();
    CheckBoxList2.DataTextField = "country";
    CheckBoxList2.DataValueField = "country_id";
    CheckBoxList2.DataBind();
}


-
a class file to handle the connection to the db and return the DataSet:

public class CBLists
{
    private SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CONN"].ConnectionString);
public DataSet GetCheckBoxList1()
{
    try
    {
        DataSet ds = new DataSet();
        string cmdstr = "select * from table1 where id<>999 order by id";
        SqlDataAdapter adp = new SqlDataAdapter(cmdstr, conn);
        adp.Fill(ds);
        return ds;
    }
    catch (Exception ex)
    {
        return null;
    }
    finally
    {
        conn.Close();
    }

}

public DataSet GetCheckBoxList2()
{
    try
    {
        DataSet ds = new DataSet();
        string cmdstr = "select * from table2";
        SqlDataAdapter adp = new SqlDataAdapter(cmdstr, conn);
        adp.Fill(ds);
        return ds;
    }
    catch (Exception ex)
    {
        return null;
    }
    finally
    {
        conn.Close();
    }
}


Is this the "correct" way to do it? multiple methods in a separate file?

Solution

In 3-Layer design, we have below three layers

Presentation Layer: In this case your aspx and code behind files.

Business Layer: It seems this is missing in your project. You may include this for various purposes e.g., filtering items based on different scenarios, logged in user, etc.

Data Layer: This layer interacts with data store, external services, etc. CBLists class in this case.

Also, in OO programming class names are singular so CBLists should be changed to CBList.

Context

StackExchange Code Review Q#37493, answer score: 2

Revisions (0)

No revisions yet.