我下载了很多三层结构的代码,发现它们都大量使用了泛型,
数据层:public IList<ItemInfo> GetItemByNew()
{ IList<ItemInfo> item = new List<ItemInfo>();
using (SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.txtConnecttionString, CommandType.StoredProcedure, "SelectItemByNew", null))
{
while (dr.Read())
{ ItemInfo itemInfo = new ItemInfo(dr.GetInt32(0), dr.GetString(1), dr.GetDecimal(2), dr.GetDecimal(3), dr.GetDecimal(4), dr.GetString(5));
item.Add(itemInfo);
}
}
return item; }
业务层:
public IList<ItemInfo> GetItemByNew()
{ return dal.GetItemByNew(); }
表示层:
IList<ItemInfo> item = bll.GetItemByNew();
为什么要使用IList接口呢?直接用它的类不是更简单吗?
谁有这方面的资料请推荐一下,谢谢!
数据层:public IList<ItemInfo> GetItemByNew()
{ IList<ItemInfo> item = new List<ItemInfo>();
using (SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.txtConnecttionString, CommandType.StoredProcedure, "SelectItemByNew", null))
{
while (dr.Read())
{ ItemInfo itemInfo = new ItemInfo(dr.GetInt32(0), dr.GetString(1), dr.GetDecimal(2), dr.GetDecimal(3), dr.GetDecimal(4), dr.GetString(5));
item.Add(itemInfo);
}
}
return item; }
业务层:
public IList<ItemInfo> GetItemByNew()
{ return dal.GetItemByNew(); }
表示层:
IList<ItemInfo> item = bll.GetItemByNew();
为什么要使用IList接口呢?直接用它的类不是更简单吗?
谁有这方面的资料请推荐一下,谢谢!
解决方案 »
- DataList绑定数据的问题
- 熟悉iis7的进来帮忙下
- 如何实现这个方法?
- 怎么样输入网站就出来 网页 ,但是不出现网页名称
- 如何使验证控件只对某个按钮起作用
- 紧急求救!!!关于改变WEB页面中iframe的src值....
- 我用xmlhttp做一个简单试验,获得的ResponseText总是undefined。特此求教原因。谢谢!
- 登陆问题
- 在一个DataList的ItemTemplate中放置一个Label控件和一个Panel,为什么Panel控件会自动跑到第二行,如何控制Panel和另一个Label在同一行?
- Server.htmlencode的用法,求助
- 在vs2005中如何用客户端控件来实现图片上传
- 远程服务器返回错误: (404) 未找到。
执行效果好并且是类型安全的
用泛类型主要是返回一个集合!
IList<ItemInfo> 表示返回的是ItemInfo对象的集合,
在编程的时候使用起来更加方便!
是为了方便扩展吗?但在表示层获得的集合只是完成一个特定的功能(将数据赋给html标识字符串),这样的话扩不扩展意义不大吧?
目前看起来,这个方法的消费者只需要返回一个实例,但如果将来需求有变化,需要你返回一个IList,那么你就需要再去改一次代码了。
实际上,是否一定要返回一个collection,直接的原因是你的需求变更的可能性。
其实刚才大家都回答你了,主要是编程方便和好管理了://比如我们要对用户信息进行管理//可以先定义一个用户信息类,只加一个字段
public class UserInfo
{
private string _name;
public int Name{get{return _name;} set {this._name = value;}}
public UserInfo(){}
public UserInfo(string name){this.Name = name;}
}
//再定义一个逻辑操作类
public class User
{
//如果我要取得用户信息我们不必返回数据库里的dataset或datareader
//使用泛类型
public IList<UserInfo> GetUsers()
{
IList<UserInfo> user = new List<UserInfo>();
SqlDataReader dr = sqlhelper.GetUsers();//调用数据层
while(dr.Read())
{
UserInfo ui = new UserInfo(dr["UserName"].ToString());
user.Add(ui);
}
return user;
}
}
骑驴找驴了吧。“实体类”这个不生活化的概念念得走火入魔了。实际上,ILIst<T>泛型中的类型参数T在应用中不正是返回的数据个体的类型么?
{................ 接下来手工实现ILIst的各个方法。
这也可以。不论你是返回一个自定义集合类型,还是返回List<ItemInfo>,使用IList<ItemInfo>作为返回的接口定义都是“通吃”的。当然,其实使用List<ItemInfo>也没有什么,除非你一定要兼容自定义集合类型。
我怀疑你可能刚刚接手你们的软件(以前是别人写的)。你的问题中的程序明明是返回一个 UserInfo 类型对象的集合,你怎么能用 DeleteUser(UserInfo ui) 这个跟集合无关的方法来说明你的问题中的程序和呢?
http://hi.baidu.com/ucfar/blog/item/5138aa314309f593a9018e5e.html