人们都经常提到三层开发 单我只理会了其中的两层,就是实体类,和数据层操作类 连中间的接口类都不是很明白是搞什么用的
比如 先定义了 public class HotelImgInfo
{
private int _picid;
private int _hotelid;
private string _picname;
private string _picpath;
private int _isfront;
private int _order;
public HotelImgInfo()
{
}
/// <summary>
/// 图片id
/// </summary>
public int PicId
{
get {return this._picid;}
set {this._picid = value;}
}
/// <summary>
/// 酒店id
/// </summary>
public int HotelId
{
get {return this._hotelid;}
set {this._hotelid = value;}
}
/// <summary>
/// 图片名称
/// </summary>
public string PicName
{
get {return this._picname;}
set {this._picname = value;}
}
/// <summary>
/// 图片路径
/// </summary>
public string PicPath
{
get {return this._picpath;}
set {this._picpath = value;}
}
/// <summary>
/// 是否显示在酒店信息首叶
/// </summary>
public int IsFront
{
get {return this._isfront;}
set {this._isfront = value;}
}
public int Order
{
get {return this._order;}
set {this._order=value;}
}
}这个类, 接着引用这个类的集合
中间再写一个 集合类
public class HotelImgCollection:CollectionBase
{
public HotelImgCollection()
{
}
/// <summary>
/// 索引器
/// </summary>
public HotelImgInfo this [ int Index ]
{
get { return (Model.HotelImgInfo)List[ Index ] ; }
set { List[Index] = value ; }
}
/// <summary>
/// 索引添加方法
/// </summary>
/// <param name="Info"></param>
/// <returns></returns>
public int Add ( Model.HotelImgInfo Info )
{
return List.Add( Info );
}
public void Remove ( HotelImgInfo Info)
{
List.Remove(Info);
}
}
然后 就开始写实现问题的接口 (这个过程 我很是费解,我直接用数据操作层不行么?)
然后写 数据操作层
public Model.Collections.HotelImgCollection GetHotelImgList(int HotelId,int top)
{
string Sql;
if (top == 0)
{
Sql = "Select * From H_PicList Where H_PIC_HotelId = @HotelId Order By H_Pic_Order Asc";
}
else
{
Sql = "Select top "+top+" * From H_PicList Where H_PIC_HotelId = @HotelId Order By H_Pic_Order Asc";
}
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@HotelId",SqlDbType.Int)
};
param[0].Value = HotelId;
Model.Collections.HotelImgCollection Co = new HotelImgCollection();
using (SqlConnection Con = ICon.Conn)
{
SqlDataReader dr = SqlHelper.ExecuteReader(Con,CommandType.Text,Sql,param);
while (dr.Read())
{
Co.Add(FillHotelImgInfo(dr));
}
}
return Co;
}