【发布时间】:2016-11-16 05:08:44
【问题描述】:
我正在做的项目是'大学管理系统',它是一个很大的项目。现在,我正在实施运行良好的学生注册部分(项目的一小部分)。我在 ASP.NET MVC 模板中使用了 '三层架构' 和 'ORM - EF'。在项目中,我需要根据学生的年级、部门等对注册学生进行一些验证。所以有 DAL、BLL、最后是控制器和视图等部分。我已经在控制器中完成了验证,并从 BLL 中获取数据,该数据再次从 DAL 中检索数据(这是 '三层架构' 的简单条件)。所以我的问题是:
1) 可以在控制器中进行验证吗?
2) 如果不需要并且需要在 BLL 中执行,会不会很好,为什么或者我可以 继续在控制器中执行此操作?
注意:对我来说,在控制器或 BLL 中进行验证似乎没问题,而且是一样的。有效果吗?
现在,我做了以下事情:
DAL:
public List<Student> Add(int studentID, string studentName, string email, DateTime regDate)
{
List<Student> lst = null;
Student aStudent = new Student();
aStudent.StudentID = studentID;
aStudent.StudentName = studentName;
aStudent.Email = email;
aStudent.RegDate = regDate;
try
{
db.Students.Add(aStudent);
db.SaveChanges();
}
catch (Exception ex)
{
ex.ToString();
}
return lst;
}
BLL:
public List<Student> Add(int studentID, string studentName, string email, DateTime regDate)
{
return aStudentGateway.Add(studentID, studentName, email, regDate);
}
控制器:
/**Student Registration - Starts**/
[HttpPost]
public ActionResult AddStudent(Student aStudent)
{
List<Department> departments = aDepartmentManager.GetAllDepartments();
List<DepartmentViewModel> departmentsViewModel = aDepartmentManager.GetAllDepartmentViewModel();
DateTime yearInDateTime = Convert.ToDateTime(Request.Form["RegDate"]);
string extractYear = yearInDateTime.ToString();
var year = DateTime.Parse(extractYear).Year;
int department = Convert.ToInt32(Request.Form["Department"]);
List<Student> studentList = aStudentManager.GetAllStudents();
int count = 1;
var query = (from c in studentList
where c.Department == department && c.Year == year
select c).ToList();
foreach (var c in query)
{
if (query.Count() > 0)
{
int m = Convert.ToInt32(c.StudentID);
count = m + 1; //Incrementing the numbers by one with the table column
}
else
{
int m = 1;
count = m + 1; //Incrementing the numbers by one with the variable assigned one
}
}
Student student = new Student();
student.StudentName = Request.Form["StudentName"];
student.Email = Request.Form["Email"];
student.RegDate = Convert.ToDateTime(Request.Form["RegDate"]);
student.StudentID = count;
if (aStudentManager.ExistEmailAny(student.Email))
{
ViewBag.ErrorMessage = "Email already exists";
}
else
{
aStudentManager.Add(aStudent.StudentID, aStudent.StudentName, aStudent.Email, aStudent.RegDate);
ViewBag.Message = "Registration successful. See below to verify.";
/**This section used to show student details after registration**/
var result = (from c in departments
join d in departmentsViewModel on c.DepartmentID equals d.DepartmentId
where d.DepartmentId == department
select c);
foreach (var items in result)
{
if (count.ToString().Length > 1)
{
ViewBag.StudentID = items.Code + "-" + year + "-" + "0" + count;
}
else
{
ViewBag.StudentID = items.Code + "-" + year + "-" + "00" + count;
}
StudentViewModel.StudentID = student.StudentID;
StudentViewModel.StudentName = student.StudentName;
StudentViewModel.Email = student.Email;
StudentViewModel.RegDate = student.RegDate;
}
/**This section used to show student details after registration**/
}
return View();
}
/**Student Registration - Ends**/
【问题讨论】:
-
感谢您提出这个问题。我正要问同样的问题。我最近一直在从事各种项目,我总是发现我在 BLL 中除了调用 DAL 之外什么都不做。那么 BLL 有什么用呢?我也在 Controller 中编写了所有的业务验证和对象操作。我很想知道做这件事的正确方法。
-
@KrishnanduSarkar:好像你的问题和 OP 的问题不同,如果你有这样的问题,那么你可以问一个新的问题。
-
感谢您的回答。我试图理解你们俩写的东西。在 三层架构 的一些教程中,我看到了控制器中正在执行条件等逻辑部分,但实际上并不确定。但是尽管进行了数据验证,但在我看来,根据项目要求,控制器而不是 DAL 中应该满足某些条件。让我说得更清楚。在 DAL 中,我们插入数据。但在某些情况下,可能需要一些条件才能插入这些数据。所以我猜这些条件可以在控制器中检查。
-
@AT-2016:根据三层架构的考虑,添加/更新/删除数据会有一个通用的方法,这可能是通用的,也可能是不同的。
-
@Div 我猜,您正在写关于存储库模式的文章,其中将有一个通用类和 CRUD 的常用方法。但是在三层架构方面是不是有点不同?基本上两者是不同的。
标签: asp.net-mvc entity-framework linq validation three-tier