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

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;
}
}
}
}



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

...全文
186 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
roydu 2006-04-07
  • 打赏
  • 举报
回复
如果相对每个数据表,都写一个实体层,和一个操作层,不麻烦吗?这样有什么好处?
wangyiban 2006-04-07
  • 打赏
  • 举报
回复
完全可以把问题进一部的抽象解决,关键要看你做程序的目的性,至于怎样分层和你的目标是有关系的
iuhxq 2006-04-07
  • 打赏
  • 举报
回复
不觉得麻烦,确实可以这样写
roydu 2006-04-07
  • 打赏
  • 举报
回复
每个表都写一个实体层,那操作层是不是也该相应写一个?
课程介绍:          Informatica 9.6 实战课程,将从数据仓库体系架构介绍,服务器搭建配置,实例组件操作,丰富的案例,及项目实战开发过程 来讲述ETL工具Informatica的实际应用,真正完成数据仓库全流程开发应用,ODS,EDW,DM,学完该课程具备ETL软件开发工程师能力水平,能独立完成项目开发工作,从入门到高级项目开发应用,每个实例都载图,课件,教程,数据等,能达到中高级ETL开发能力,本课程的学习要求,一定跟着实例动手实战,操作才能掌握实例的开发步骤,及开发注意事项。每个组件都用具体案例讲解,保证学以致用。掌握ETL的开发技能,搭建数据仓库体系架构。应对各环境操作。具体数据实操与应用。 你将收获:ETL软件开发工程师能力与水平数据仓库体系架构案例综合应用实战ETL实战项目开发数据采集与数据处理数据清洗与数据仓库指标统计学会使用Informatica完成项目开发掌握ETL开发流程掌握数据质量管理 适用人群 数据仓库初学者 数据仓库开发工程师 数据开发工程师 BI开发工程师 数据可视化工程师大数据开发与应用 数据仓库管理 数据管理 数据析师 业务数据析 零基础入门ETL ETL大数据处理 数据仓库体系架构图: 课程大纲:01_Informatica9.6课程导学02_数据仓库体系介绍03_Oracle_安装04_Oracle_配置.wmv05_PLSQL工具汉化破解06_Informatica9.6安装.wmv07_Informatica9.6服务器配置.wmv08_客户端的安装配置连接.wmv09_客户端工具介绍10_源数据介绍11_入门实例EMP_0112_入门实例EMP_0213_入门实例DEPT表14_表达式Fx组件实例115_表达式Fx组件实例216_聚合组件实例17_查找Lookup组件实例18_过滤Filter组件实例19_Informatica9.6 排序sort组件实例详解20_Informatica9.6 发器Router组件实例21_Informatica9.6 sql连接查询复习22_Informatica9.6 连接查询同构SQ23_Informatica9.6 连接查询异构JOIN24_Informatica9.6 等级函数Rank实例25_Informatica9.6 数据合并Union组件实例26_Informatica9.6 存储过程Px组件实例27_1行转列案例sql28_2行转列案例ETL过程29_工资大于该职位下平均工资案例30_增量抽取案例31_缓慢变化维案例32_对文件数据的读取写入33_项目1薪酬统计项目介绍34_Informatica 项目1薪酬统计项目接口入库ODS35_Informatica 项目1薪酬统计项目EDW明细表临时表1sql36_Informatica 项目1薪酬统计项目EDW明细表临时表2ETL37_Informatica 项目1薪酬统计EDW明细表138_Informatica 项目1薪酬统计EDW明细表239_Informatica 项目1薪酬统计EDW明细表340_Informatica 项目1薪酬统计DM月考勤统计报表41_Informatica 项目1开发DM统计报表142_Informatica 项目1开发DM统计报表243_Informatica 项目1开发DM统计报表344_Informatica 项目1开发DM薪资报表145_Informatica 项目1开发DM薪资报表246_Informatica 项目1开发DM薪资报表347_Informatica 项目2计费系统项目介绍48_Informatica 项目2计算系统项目ODS49_Informatica 项目2计费系统EDW150_Informatica 项目2计费系统EDW251_Informatica 项目2计费系统EDW352_Informatica 项目2计费系统DM 

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧