我正在尝试改进应用程序的设计,因此不是从表示层调用数据访问层。我将尝试从 BusinessObjects 层中的对象实现保存方法。但我不确定如何通过图层传递对象或其属性。例如,在我的旧设计中,我只是在表示层中创建对象的实例并分配它的属性,然后调用 DataAccess 方法将此信息保存在数据库中并将对象作为参数传递,如图所示。
DAL
public static void SaveObject(Object obj)
{
int id = obj.id;
string label = obj.label;
}
PL
Object obj = new Object();
obj.id = 1;
obj.label = "test";
DAL.SaveObject(obj);
但我只想在我的PL
Object obj = new Object();
obj.id = 1;
obj.label = "test";
obj.SaveObject();
那可能吗?我的 DAL 会是什么样子?
Edit: 解释我的要求
我现在将把我的代码基于系统中一个非常重要的对象。
业务实体层使用业务逻辑层
namespace BO.Cruises
{
public class Cruise
{
public int ID
{ get; set; }
public string Name
{ get; set; }
public int BrandID
{ get; set; }
public int ClassID
{ get; set; }
public int CountryID
{ get; set; }
public string ProfilePic
{ get; set; }
public bool Hide
{ get; set; }
public string Description
{ get; set; }
public int OfficialRate
{ get; set; }
public string DeckPlanPic
{ get; set; }
public string CabinsLayoutPic
{ get; set; }
public List<Itinerary> Itineraries
{ get; set; }
public List<StatisticFact> Statistics
{ get; set; }
public List<CabinRoomType> RoomTypesQuantities
{ get; set; }
public List<CabinFeature> CabinFeatures
{ get; set; }
public List<CruiseAmenity> Amenities
{ get; set; }
public List<CruiseService> Services
{ get; set; }
public List<CruiseEntertainment> Entertainment
{ get; set; }
public List<CustomerReview> CustomerReviews
{ get; set; }
}
}
业务逻辑层使用数据访问层
实际上,这一层的目的是验证我的对象,然后调用 DAL 方法,但我现在没有实现任何验证,所以我只是用它来调用 DAL 方法。
public static void Save(object cruise)
{
CruisesDAL.Save(cruise);
}
数据访问层尝试引用 BussinessEntities 但它给了我循环依赖错误!
它应该接收该对象并将其转换为 Cruise 实体
public static void Save(object cruise)
{
Cruise c = cruise as Cruise;
//access the object c properties and save them to the database
}
我的项目中的代码示例:
public static List<Cruise> GetCruisesList()
{
string commandText = "SELECT ID, Name + CASE Hide WHEN 1 Then ' (Hidden)' ELSE '' END AS Name FROM Cruises";
List<Cruise> cruises = new List<Cruise>();
Cruise cruise;
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
using (SqlCommand command = new SqlCommand(commandText, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
cruise = new Cruise();
cruise.ID = Convert.ToInt32(reader["ID"]);
cruise.Name = reader["Name"].ToString();
cruises.Add(cruise);
}
}
}
}
return cruises;
}
表示层使用业务实体
输入控件(文本框、DropDownList 等)
单击保存按钮时,我会获取所有值,创建一个 Cruise 对象并调用 Cruise.Save();