PetShop中的对象传递
忙里偷闲把petshop4.0看了看.对它的"对象传递"印象比较深刻.以前在做系统的时候总是在表现层暴漏大量的sql语句.现在我想有了对象传递机制.便可以把数据操作完全封装到业务逻辑层其结果可以以对象的形式返回表现层.以下是简化的对象传递代码.
using System;
/**//// <summary>
/// Summary description for Product
/// </summary>
///
namespace web.Test
{
[Serializable]
public class Product
{
private string id;
private string name;
private string description;
private string image;
private string categoryId;
/**//// <summary>
/// Default constructor
/// </summary>
public Product() { }
/**//// <summary>
/// Constructor with specified initial values
/// </summary>
/// <param name="id">Product Id</param>
/// <param name="name">Product Name</param>
/// <param name="description">Product Description</param>
/// <param name="image">Product image</param>
/// <param name="categoryId">Category Id</param>
public Product(string id, string name, string description, string image, string categoryId)
{
this.id = id;
this.name = name;
this.description = description;
this.image = image;
this.categoryId = categoryId;
}
// Properties
public string Id
{
get { return id; }
}
public string Name
{
get { return name; }
}
public string Description
{
get { return description; }
}
public string Image
{
get { return image; }
}
public string CategoryId
{
get { return categoryId; }
}
}
}
using System;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using web.Test;
/**//// <summary>
/// Summary description for productedList
/// </summary>
namespace web.IDAL
{
public class productedList
{
public IList<Product> GetProductsByCategory()
{
IList<Product> PList = new List<Product>();
for(int i=0;i<5;i++)
{
Product pr = new Product(i.ToString(),i.ToString(),i.ToString(),i.ToString(),i.ToString());
PList.Add(pr);
}
return PList;
}
}
}