三层架构:获取所有数据和验证

时间:2016-11-16 05:08:44

标签: asp.net-mvc entity-framework linq validation three-tier

我正在开展的项目是'大学管理系统',这是一个很大的项目。现在,我正在实施的学生注册部分工作正常(项目的一小部分)。我在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**/

2 个答案:

答案 0 :(得分: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获取每个属性。

希望这有意义!

答案 1 :(得分:1)

我会在不同的层中提供多个验证步骤,具体取决于上下文和层的含义。

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

对于客户端,您应该提供必填字段和其他简单验证的字段检查。如果您正在使用MVC you can use data annotations

应在控制器中复制相同的验证。在这里,您应该快速将某种合同应用于已经传递的参数。一个好的做法是使用Code Contracts来提供在执行流程中继续需要满足的前提条件。

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

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

即使这种方法看起来会引入重复,但它会提供数据的一致性以及各层之间的单独关注。