关于分层开发的数据操作层和实体层,解决即给分!
请耐心看完,分不够再加!
这里有一个实体层:
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;
}
}
}
}
我的问题是这是一个数据表的访问,如果表多了,是不是每个表都得写一个实体层?那么操作层又该怎样?这样写起来是不是搞麻烦了?
这里有一个实体层:
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;
}
}
}
}
我的问题是这是一个数据表的访问,如果表多了,是不是每个表都得写一个实体层?那么操作层又该怎样?这样写起来是不是搞麻烦了?
...全文
相关推荐
Service层和DAO层解析
对于小型项目,两层结构(如UI直接调用DAO)可能已经足够,可以减少开发时间和成本。然而,随着项目的增长,清晰的分层将带来更好的可维护性和可扩展性。
实体层生成工具实体层生成工具
通过理解以上知识点,并熟练使用像nTierGen这样的实体层生成工具,开发者可以更高效地构建和维护大型的、分层的软件系统,降低出错概率,提升项目交付速度。
.NET分层开发的几点心得
这种设计使得业务逻辑层的代码更为清晰,也方便后期的维护和升级。### 结语.NET平台下的分层开发不仅可以提高软件的质量,还能极大地提升开发效率。
一个分层架构设计的例子
本文主要介绍了一个分层架构设计的例子,涉及了常见的四个层次:实体层(Entity)、数据访问层(DAL)、业务逻辑层(BLL)和界面层(UI),以及在数据访问层中加入的接口层(IDAL)。实体层主要
学员成绩管理,C#开发,分层开发
通常,一个分层架构包括表示层(UI)、业务逻辑层(BLL)、数据访问层(DAL)和实体层(Entities)。这种结构的优点在于提高了代码的可读性、可维护性和模块化,使得各个组件可以独立开发和测试。


