如何将一个类传递给另一个类到一个代码隐藏?
当我调试并检查Default.aspx页面中的myCategoryObj时,我可以看到该对象在调试中。我究竟做错了什么?
我知道我可以在Default.aspx中创建对象,但我不应该这样做我应该能够调用业务逻辑层并要求返回一个对象,然后填充该对象并将其传递回业务逻辑要保存的图层(插入或更新)。
我希望这是有道理的。
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(){
}
}
}
答案 0 :(得分:2)
变化
Object myCategoryObj = myCategoryBLL.CategoryNew()
到
Category myCategoryObj = myCategoryBLL.CategoryNew()
以及
public Object CategoryNew() { return myCategoryObject; }
到
公共类别类别新() { return myCategoryObject; }