导航
  • 主页
  • ASP
  • .NET Framework
  • Web Services
  • VB
  • VC
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • LINQ
  • 问答

关于分层开发的数据操作层和实体层,解决即给分!

roydu 2006-04-07 01:35:11
请耐心看完,分不够再加!

这里有一个实体层:
using System;
using System.Data;
namespace BWebShippersDemo
{
public class DataEntity:DataSet
{
public const string ShipperTableName="Shippers";
public const string ShipperID="ShipperID";
public const string CompanyName="CompanyName";
public const string Phone="Phone";
public DataEntity()
{
this.BuildDataTable();
}
private void BuildDataTable()
{
DataTable dt=new DataTable(ShipperTableName);
dt.Columns.Add(ShipperID,typeof(int));
dt.Columns.Add(CompanyName,typeof(string));
dt.Columns.Add(Phone,typeof(string));
this.Tables.Add(dt);
}
}
}

然后有相应的数据操作层:
using System;
using System.Data;
using System.Data.SqlClient;
namespace BWebShippersDemo
{
public class DataAccess
{
private SqlCommand selectCommand;
private SqlCommand insertCommand;
private SqlCommand updateCommand;
private SqlCommand deleteCommand;
private SqlDataAdapter dsCommand;

private string connString="server=dotnet;database=northwind;integrated security=sspi";
private string selectString="select * from shippers";
private string insertString="insert into shippers (CompanyName,Phone) values (@name,@phone)";
private string updateString="update shippers set CompanyName =@name,Phone=@phone where ShipperID=@id";
private string deleteString="delete from shippers where shipperid=@id";

public DataAccess()
{
dsCommand=new SqlDataAdapter();
dsCommand.TableMappings.Add("Table",DataEntity.ShipperTableName);
}
private SqlCommand GetSelectCommand()
{
if(selectCommand==null)
{
selectCommand=new SqlCommand(selectString,new SqlConnection(connString));
}
return selectCommand;
}
private SqlCommand GetInsertCommand()
{
if(insertCommand==null)
{
insertCommand=new SqlCommand(insertString,new SqlConnection(connString));
insertCommand.Parameters.Add(new SqlParameter("@name",SqlDbType.NVarChar,40));
insertCommand.Parameters.Add(new SqlParameter("@phone",SqlDbType.NVarChar,24));
insertCommand.Parameters["@name"].SourceColumn=DataEntity.CompanyName;
insertCommand.Parameters["@phone"].SourceColumn=DataEntity.Phone;
}
return insertCommand;
}
private SqlCommand GetUpdateCommand()
{
if(updateCommand==null)
{
updateCommand=new SqlCommand(updateString,new SqlConnection(connString));
updateCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4));
updateCommand.Parameters.Add(new SqlParameter("@name",SqlDbType.NVarChar,40));
updateCommand.Parameters.Add(new SqlParameter("@phone",SqlDbType.NVarChar,24));
updateCommand.Parameters["@id"].SourceColumn=DataEntity.ShipperID;
updateCommand.Parameters["@name"].SourceColumn=DataEntity.CompanyName;
updateCommand.Parameters["@phone"].SourceColumn=DataEntity.Phone;
}
return updateCommand;
}
private SqlCommand GetDeleteCommand()
{
if(deleteCommand==null)
{
deleteCommand=new SqlCommand(deleteString,new SqlConnection(connString));
deleteCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4));
deleteCommand.Parameters["@id"].SourceColumn=DataEntity.ShipperID;
}
return deleteCommand;
}
public DataEntity GetShippersList()
{
DataEntity de=new DataEntity();
if(dsCommand==null)
throw new Exception("Error");
dsCommand.SelectCommand=this.GetSelectCommand();
dsCommand.Fill(de);
return de;
}
public bool InsertShippers(DataEntity de)
{
if(dsCommand==null)
throw new Exception("Error");
dsCommand.InsertCommand=this.GetInsertCommand();
dsCommand.Update(de,DataEntity.ShipperTableName);
if(de.HasErrors)
{
de.Tables[DataEntity.ShipperTableName].GetErrors()[0].ClearErrors();
return false;
}
else
{
de.AcceptChanges();
return true;
}
}
public bool UpdateShippers(DataEntity de)
{
if(dsCommand==null)
throw new Exception("Error");
dsCommand.UpdateCommand=this.GetUpdateCommand();
dsCommand.Update(de,DataEntity.ShipperTableName);
if(de.HasErrors)
{
de.Tables[DataEntity.ShipperTableName].GetErrors()[0].ClearErrors();
return false;
}
else
{
de.AcceptChanges();
return true;
}
}
public bool DeleteShippers(DataEntity de)
{
if(dsCommand==null)
throw new Exception("Error");
dsCommand.DeleteCommand=this.GetDeleteCommand();
dsCommand.Update(de,DataEntity.ShipperTableName);
if(de.HasErrors)
{
de.Tables[DataEntity.ShipperTableName].GetErrors()[0].ClearErrors();
return false;
}
else
{
de.AcceptChanges();
return true;
}
}
}
}



我的问题是这是一个数据表的访问,如果表多了,是不是每个表都得写一个实体层?那么操作层又该怎样?这样写起来是不是搞麻烦了?

...全文
155 点赞 收藏 4
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
roydu 2006-04-07
如果相对每个数据表,都写一个实体层,和一个操作层,不麻烦吗?这样有什么好处?
回复
wangyiban 2006-04-07
完全可以把问题进一部的抽象解决,关键要看你做程序的目的性,至于怎样分层和你的目标是有关系的
回复
iuhxq 2006-04-07
不觉得麻烦,确实可以这样写
回复
roydu 2006-04-07
每个表都写一个实体层,那操作层是不是也该相应写一个?
回复
发动态
发帖子
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
社区公告
暂无公告