当前位置: 代码迷 >> ASP.NET >> 为什么要有“linterface.cs”这个文件?该如何处理
  详细解决方案

为什么要有“linterface.cs”这个文件?该如何处理

热度:604   发布时间:2013-02-25 00:00:00.0
为什么要有“linterface.cs”这个文件?
//Default.aspx.cs
  private void bindnew()
  {
  linterface lif = new SqlSelect();
  SqlDataReader sda = lif.SelectData();
  GridView1.DataSource = sda;
  GridView1.DataBind();
  sda.Close();
  }
//SqlSelect.cs

public class SqlSelect : linterface
{
  private static readonly string SlectShitu = "select * from shitu where c_num>60 and z_name='dotnet' order by s_id";
  public SqlDataReader SelectData()
  {
  SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
  SqlCommand cmd = new SqlCommand(SlectShitu, con);
  SqlDataReader sda = null;
  try
  {
  con.Open();
  sda = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  }
  catch(SqlException ex)
  {
  throw new Exception(ex.Message,ex);
  }
  return sda;
  }
}

//linterface.cs
public interface linterface
{
  SqlDataReader SelectData();
}

------解决方案--------------------------------------------------------
也许写程序的人认为他可能要实现多个数据库访问类,比如 SqlSelect、AccessSelect…… 然后自由替换。

不过就你的代码看,更多可能是,这是一个没有必要且蹩脚的设计。
------解决方案--------------------------------------------------------
探讨

这个只是一个接口.让你的代码有层次感