【发布时间】:2011-09-01 22:18:35
【问题描述】:
我正在尝试改进我的应用程序的设计,而不是从表示层调用 DataAccess 层。我将尝试在 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 会是什么样子?
编辑: 解释我的要求
我现在将我的代码基于我系统中一个非常重要的对象。
BusinessEntitiesLayer使用业务逻辑层
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; }
}
}
BusinessLogicLayer 使用 DataAccessLayer
实际上这个层是用来验证我的对象然后调用 DAL 方法,但我现在没有实现任何验证,所以我只是用它来调用 DAL 方法。
public static void Save(object cruise)
{
CruisesDAL.Save(cruise);
}
DataAccessLayer 试图引用 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;
}
PresentationLayer 使用 BusinessEntities
输入控件(文本框、下拉列表等)
当点击保存按钮时,我获取所有值,创建一个 Cruise 对象并调用 Cruise.Save();
【问题讨论】:
标签: c# asp.net n-tier-architecture