views:

110

answers:

1

How to pass a class into another class to a codebehind?

When I debug and check the myCategoryObj in the Default.aspx page, I can see the object is in the debug. What am I doing wrong?

I know I could create the object in the Default.aspx but I should not have to I should be able to call the Business Logic Layer and ask for an object back and then fill the object and pass it back to the Business Logic Layer to be saved (insert or update).

I hope this makes sense.

Default.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SC1.Models.OBJ;
using SC1.Models.BLL;
using SC1.Models.DAL;

namespace SC1
{
  public partial class Default : System.Web.UI.Page
  {
    protected void Page_Load(Object sender, EventArgs e)
    {
      // I know I could do this but I don't want to unless I have too.
      //Category categoryObj = new Category();

      CategoryBLL myCategoryBLL = new CategoryBLL();

      Object myCategoryObj = myCategoryBLL.CategoryNew();

      // How do I make the code below work or what am I doing wrong.  
      myCategoryObj.Name = "test";

      string test = "";

    }
  }
}

CategoryBLL.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using SC1.Models.DAL;
using SC1.Models.OBJ;

namespace SC1.Models.BLL
{
  public class CategoryBLL
  {
    // Create a page object
    Category myCategoryObject = new Category();

    // Create a Data Acces Layer Object
    CategoryDAL myCategoryDAL = new CategoryDAL();


    public CategoryBLL()
    {
    }

    public DataSet Select()
    {
      return (myCategoryDAL.Select());
    }

    public Object CategoryNew()
    {
      return myCategoryObject;
    }



  }
}

CategoryDAL.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace SC1.Models.DAL
{
  public class CategoryDAL
  {
    public CategoryDAL()
    {
    }
    string connStr = ConfigurationManager.ConnectionStrings["staceys_cakesConnectionString"].ConnectionString;

    // select all
    public DataSet Select()
    {
      SqlConnection sqlConnection1 = new SqlConnection();
      string SqlString = "select * from Categories";
      SqlDataAdapter da = new SqlDataAdapter(SqlString, connStr);
      DataSet ds = new DataSet();
      da.Fill(ds, "Categories");
      return (ds);
    }
    // save
    // insert
    // update
    // delete

  }
}

Category.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SC1.Models.OBJ
{
  public class Category
  {
    public int CategoryID { get; set; }
    public int ParentID { get; set; }
    public string Name { get; set; }
    public int DisplayOrder { get; set; }
    public bool Active { get; set; }

    public Category(){
    }

  }
}
+1  A: 

change

Object myCategoryObj = myCategoryBLL.CategoryNew()

to

Category myCategoryObj = myCategoryBLL.CategoryNew()

and also

public Object CategoryNew() { return myCategoryObject; }

to

public Category CategoryNew() { return myCategoryObject; }

gbogumil
Thanks, that was where I was going wrong. I was actually told at work by another coder to move the properities to the BLL. This I knew had to be wrong. So is this a good way to do OO or should I be reading more else where? @gbogumil?
Nathan Stanford
I would change the DAL select to return an IEnumerable<Category> or a List<Category> or write another simple class to represent a list of caterory objects. DAL converts BL to DL and DL to BL. BL should not have to deal with DataSets/Tables/Rows. It is more work up front, but makes for cleaner code in the end.
gbogumil
@gbogumi sounds good. I am still new to DotNet so I will look into IEnumerable<Category> or a List<Category>.How can I change the below line of code so it will not be in each DAL?string connStr = ConfigurationManager.ConnectionStrings["staceys_cakesConnectionString"].ConnectionString;
Nathan Stanford
"DAL converts BL to DL and DL to BL. BL should not have to deal with DataSets/Tables/Rows." Should I create the object in the Code Behind and pass it to the BLL and if returns with no errors then pass it to the DAL? I thought you were not supposed to connect from the Code Behind to the DAL. What do you mean BL to DL and DL to BL? I am looking for an example of IEnumerable<> and or List<> that I can understand and use in my simple example code above.
Nathan Stanford
gbogumil
I had plans to make a object(class) then pass the class to the DL using Save(Category myCategoryObject); from the BL then return a true false to the BL to let it know if it succeeded. However when I do a categoryList and pass it through the BL to the DL doesn't the BL have to know what is returning? or not? and if not how do I do that? example the select only calls the DL to get the result back even if it is a List<Category> it has to know what is coming back in the BL or am I wrong?
Nathan Stanford
Yes, the BL will certainly know the return type of Select is List<Category>. That way in the BL you don't have to deal with any conversions. That's why you use the DL.
gbogumil