主题:探讨.net 三层架构中的问题
下边的程序为加学生或者老师,有2中写法,但是我不知道改用哪个?
Class 表
ClassID
ClassName班级名称
People表
PeopleID
Flag 标志,1,学生;2,老师
Name 姓名
Sex 性别
V_Student 视图
Name
ClassName
Sex
V_Teacher 视图
Name
ClassName
Sex
第一种写法
C# code
Web层
addStudent.aspx.cs
protected void btnAdd_Click(object sender, EventArgs e)
{
StudentInfo info = new StudentInfo();
info.Name = txtName.Text;
......
if (StudentBLL.AddStudent(info))
{
JScript.AlertAndRedirect("添加成功!", "ManageStudent.aspx");
}
}
addTeacher.aspx.cs
protected void btnAdd_Click(object sender, EventArgs e)
{
TeacherInfo info = new TeacherInfo();
info.Name = txtName.Text;
......
if (TeacherBLL.AddTeacher(info))
{
JScript.AlertAndRedirect("添加成功!", "ManageTeacher.aspx");
}
}
BLL层
(StudentBLL.cs)
public static bool AddStudent(StudentInfo info)
{
if (instance.AddStudent(info))
return true;
else
return false;
}
(TeacherBLL.cs)
public static bool AddTeacher(TeacherInfo info)
{
if (instance.AddTeacher(info))
return true;
else
return false;
}
DAL层
(StudentDAL.cs)
public bool AddStudent(StudentInfo info)
{
StringBuilder buffer = new StringBuilder();
buffer.Append("insert into People(Flag,Name,Sex) Values (@Flag,@Name,@Sex)");
SqlParameter[] parameters = {
new SqlParameter("@Flag",SqlDbType.Int,4)...};
parameters[0].Value = 1;
...
SqlDbHelper db = new SqlDbHelper();
return db.ExecuteNonQuery(buffer.ToString(), CommandType.Text, parameters) > 0;
}
(TeacherDAL.cs)
public bool AddTeacher(TeacherInfo info)
{
StringBuilder buffer = new StringBuilder();
buffer.Append("insert into People(Flag,Name,Sex) Values (@Flag,@Name,@Sex)");
SqlParameter[] parameters = {
new SqlParameter("@Flag",SqlDbType.Int,4)...};
parameters[0].Value = 2;
...
SqlDbHelper db = new SqlDbHelper();
return db.ExecuteNonQuery(buffer.ToString(), CommandType.Text, parameters) > 0;
}
DBUtility层
StudentInfo.cs
...
TeacherInfo.cs
...
第二种写法
Web层
addStudent.aspx.cs
protected void btnAdd_Click(object sender, EventArgs e)
{
PeopleInfo info = new PeopleInfo();
info.Name = txtName.Text;
info.Flag = 1;
......
if (PeopleBLL.AddPeople(info))
{
JScript.AlertAndRedirect("添加成功!", "ManageStudent.aspx");
}
}
addTeacher.aspx.cs
protected void btnAdd_Click(object sender, EventArgs e)
{
PeopleInfo info = new PeopleInfo();
info.Name = txtName.Text;
info.Flag = 2;
......
if (PeopleBLL.AddPeople(info))
{
JScript.AlertAndRedirect("添加成功!", "ManageStudent.aspx");
}
}
BLL层
(PeopleBLL.cs)
public static bool AddPeople(PeopleInfo info)
{
if (instance.AddPeople(info))
return true;
else
return false;
}
DAL层
(PeopleDAL.cs)
public bool AddPeope(PeopleInfo info)
{
StringBuilder buffer = new StringBuilder();
buffer.Append("insert into People(Flag,Name,Sex) Values (@Flag,@Name,@Sex)");
SqlParameter[] parameters = {
new SqlParameter("@Flag",SqlDbType.Int,4)...};
parameters[0].Value = info.Flag;
...
SqlDbHelper db = new SqlDbHelper();
return db.ExecuteNonQuery(buffer.ToString(), CommandType.Text, parameters) > 0;
}
DBUtility层
PeopleInfo.cs
...
2种写法都行,第一种看上去明了一些,第二种简单,但是意思没有前一种明显。单独一个表好操作,对于视图,就有很多种情况,不知道怎么处理了
Class 表
ClassID
ClassName班级名称
People表
PeopleID
Flag 标志,1,学生;2,老师
Name 姓名
Sex 性别
V_Student 视图
Name
ClassName
Sex
V_Teacher 视图
Name
ClassName
Sex
第一种写法
C# code
Web层
addStudent.aspx.cs
protected void btnAdd_Click(object sender, EventArgs e)
{
StudentInfo info = new StudentInfo();
info.Name = txtName.Text;
......
if (StudentBLL.AddStudent(info))
{
JScript.AlertAndRedirect("添加成功!", "ManageStudent.aspx");
}
}
addTeacher.aspx.cs
protected void btnAdd_Click(object sender, EventArgs e)
{
TeacherInfo info = new TeacherInfo();
info.Name = txtName.Text;
......
if (TeacherBLL.AddTeacher(info))
{
JScript.AlertAndRedirect("添加成功!", "ManageTeacher.aspx");
}
}
BLL层
(StudentBLL.cs)
public static bool AddStudent(StudentInfo info)
{
if (instance.AddStudent(info))
return true;
else
return false;
}
(TeacherBLL.cs)
public static bool AddTeacher(TeacherInfo info)
{
if (instance.AddTeacher(info))
return true;
else
return false;
}
DAL层
(StudentDAL.cs)
public bool AddStudent(StudentInfo info)
{
StringBuilder buffer = new StringBuilder();
buffer.Append("insert into People(Flag,Name,Sex) Values (@Flag,@Name,@Sex)");
SqlParameter[] parameters = {
new SqlParameter("@Flag",SqlDbType.Int,4)...};
parameters[0].Value = 1;
...
SqlDbHelper db = new SqlDbHelper();
return db.ExecuteNonQuery(buffer.ToString(), CommandType.Text, parameters) > 0;
}
(TeacherDAL.cs)
public bool AddTeacher(TeacherInfo info)
{
StringBuilder buffer = new StringBuilder();
buffer.Append("insert into People(Flag,Name,Sex) Values (@Flag,@Name,@Sex)");
SqlParameter[] parameters = {
new SqlParameter("@Flag",SqlDbType.Int,4)...};
parameters[0].Value = 2;
...
SqlDbHelper db = new SqlDbHelper();
return db.ExecuteNonQuery(buffer.ToString(), CommandType.Text, parameters) > 0;
}
DBUtility层
StudentInfo.cs
...
TeacherInfo.cs
...
第二种写法
Web层
addStudent.aspx.cs
protected void btnAdd_Click(object sender, EventArgs e)
{
PeopleInfo info = new PeopleInfo();
info.Name = txtName.Text;
info.Flag = 1;
......
if (PeopleBLL.AddPeople(info))
{
JScript.AlertAndRedirect("添加成功!", "ManageStudent.aspx");
}
}
addTeacher.aspx.cs
protected void btnAdd_Click(object sender, EventArgs e)
{
PeopleInfo info = new PeopleInfo();
info.Name = txtName.Text;
info.Flag = 2;
......
if (PeopleBLL.AddPeople(info))
{
JScript.AlertAndRedirect("添加成功!", "ManageStudent.aspx");
}
}
BLL层
(PeopleBLL.cs)
public static bool AddPeople(PeopleInfo info)
{
if (instance.AddPeople(info))
return true;
else
return false;
}
DAL层
(PeopleDAL.cs)
public bool AddPeope(PeopleInfo info)
{
StringBuilder buffer = new StringBuilder();
buffer.Append("insert into People(Flag,Name,Sex) Values (@Flag,@Name,@Sex)");
SqlParameter[] parameters = {
new SqlParameter("@Flag",SqlDbType.Int,4)...};
parameters[0].Value = info.Flag;
...
SqlDbHelper db = new SqlDbHelper();
return db.ExecuteNonQuery(buffer.ToString(), CommandType.Text, parameters) > 0;
}
DBUtility层
PeopleInfo.cs
...
2种写法都行,第一种看上去明了一些,第二种简单,但是意思没有前一种明显。单独一个表好操作,对于视图,就有很多种情况,不知道怎么处理了