1

我正在做的项目是“大学管理系统”,这是一个很大的项目。现在,我正在实施运行良好的学生注册部分(项目的一小部分)。我在 ASP.NET MVC 模板中使用了“三层架构”“ORM - EF” 。在项目中,我需要根据学生的年级、部门等对注册学生进行一些验证。所以有 DAL、BLL、最后是控制器和视图等部分。我已经在控制器中完成了验证,并从 BLL 获取数据,该数据再次从 DAL 检索数据(这是“三层架构”的简单条件)。所以我的问题是:

1)可以在控制器中进行验证吗?

2)如果不是并且需要在 BLL 中执行,会不会很好,为什么或者我可以继续在控制器中执行它?

注意:对我来说,在控制器或 BLL 中进行验证似乎没问题,而且是一样的。它有什么作用吗?

现在,我做了以下事情:

达尔:

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**/
4

2 回答 2

1

1)可以在控制器中进行验证吗?

Data Annotation Validator Attributes完全不,使用, 并在模型类中进行验证会更好。

第二件事,你在你的控制器中做一些 DAL 的东西,比如

List<Department> departments = aDepartmentManager.GetAllDepartments();
List<DepartmentViewModel> departmentsViewModel = aDepartmentManager.GetAllDepartmentViewModel();

var query = (from c in studentList
             where c.Department == department && c.Year == year
             select c).ToList();

这些所有查询都应该在 DAL 中,这正是使用 DAL 与数据库交互,并保持控制器清洁。

第三件事,

如果传递Student给控制器​​,则不需要使用Request.Form.

希望这是有道理的!

于 2016-11-16T07:31:49.187 回答
1

根据上下文和层的含义,我将在不同层中提供多个验证步骤。

首先,在客户端和服务器端都提供验证是最佳实践。

对于客户端,您应该为必填字段和其他简单验证提供字段检查。如果您使用 MVC ,则可以使用数据注释

应该在控制器中复制相同的验证。在这里,您应该无法快速将某种契约应用于已传递的参数。一种好的做法是使用代码契约,这些契约提供了在您的执行管道中继续进行所需满足的先决条件。

在业务层提供业务逻辑中需要做的检查。

最后在数据访问层提供持久化数据所需的所有检查。如果您使用的是 EF,一个好的做法是为您的实体类实现IValidatableObject在 Scott Gu 的博客中,您可以找到解释这种技术的帖子。

尽管这种方法看起来会引入重复,但它会提供数据的一致性并在层之间分离关注点。

于 2016-11-16T07:35:37.153 回答