在codeproject看到一个mvc实例.
大致结构如下.
AbstractLayer-baseMember.cs 定义了基类成员
DataLayer-SqlDataAccess.cs 继承IdataAccess,实现接口方法
-IdataAccess.cs 接口
BusinessLayer-Member.cs 继承baseMember.cs 返回方法实现
View
添加用户页面.
将页面数据添加到BusinessLayer 下的Member实例(因为BusinessLayer下的Member继承了baseMember)调用AddNewMember();
{
AddMember();
int retVal = objMember.AddnewMember(objMember);
if(retVal>0)
{
lblMessage.Text = "New record added successfully";
}
else
{
lblMessage.Text = "An error has occured while processing your request";
}
}
public void AddMember()
{
objMember.firstName = txtFirstName.Text.Trim();
objMember.lastName = txtLastName.Text.Trim();
objMember.contactNo = txtContactNo.Text.Trim();
objMember.emailAddress = txtEmailAddress.Text.Trim();
}
BusinessLayer下Member.cs
AddNewMember再返回接口方法的实现
{
IdataAccess MemberSqlDataAccess = new SqlDataAccess(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString());
return MemberSqlDataAccess.AddnewMember(objMember);
}
DataLayer
IdataAccess定义了AddNewMember接口.
SqlDataAccess实现
SqlDataAccess代码:
public int AddnewMember(AbstractLayer.baseMember objMember)
{
SqlTransaction objTrans=null;
SqlConnection myConnection = new SqlConnection(m_Connection_String);
try
{
// Insert Member Personal details only
myConnection.Open();
objTrans= myConnection.BeginTransaction();
SqlParameter [] arrParam=new SqlParameter[4];
arrParam[0]=new SqlParameter("@fName", objMember.firstName);
arrParam[1]=new SqlParameter("@lName",objMember.lastName);
arrParam[2]=new SqlParameter("@coNo",objMember.contactNo);
arrParam[3]=new SqlParameter("@emailAddr",objMember.emailAddress);
//pass connection string, storedprocedure name and parameter array
SqlHelper.ExecuteNonQuery(m_Connection_String,CommandType.StoredProcedure,"usps_proInsMember",arrParam);
}
catch(Exception Ex)
{
objTrans.Rollback();
string sError=Ex.Message.ToString();
return -1;
}
finally
{
myConnection.Close();
}
return 1;
}