如何將一個類傳遞給另一個類到一個代碼隱藏?如何將一個類傳遞給另一個類到一個代碼隱藏?
當我調試並檢查Default.aspx頁面中的myCategoryObj時,我可以看到該對象在調試中。我究竟做錯了什麼?
我知道我可以在Default.aspx中創建對象,但我不應該必須能夠調用業務邏輯層並請求返回對象,然後填充該對象並將其傳回給Business要保存的邏輯層(插入或更新)。
我希望這是有道理的。
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(){
}
}
}
不,它沒有任何意義。你想要將一個類傳遞給一個類,或者將某個對象傳遞給一個類,並且該對象位於下拉列表中。這就是我所知道的.. 告訴我們*發生了什麼,以及你想要發生什麼,而不是發生什麼。 – 2010-08-03 16:01:06
我試圖做的是有背後調用BLL的代碼,然後讓它從DAL或Object獲得對象,然後當我設置合適的時候,我可以簡單地有一個函數說Save(object _CategoryObject),它會看到一個ID或沒有ID,如果該ID存在,它會插入一個新的記錄,如果該ID已經存在,那麼它會更新該記錄。 另外我在想,既然對象是具有合適的東西,那麼我不應該在其他地方設置本質。 – 2010-08-03 16:14:02
我想我一定是錯的......所以不用擔心我正在努力把它弄好。 一個汽車類有其特性,但業務邏輯層調用數據訪問層保存不應該是什麼性質?還是我在OO世界中迷失了? – 2010-08-03 16:15:01