【问题标题】:What is the proper sequence of method calls when using a multi layered architecture?使用多层架构时,方法调用的正确顺序是什么?
【发布时间】:2012-04-25 13:22:00
【问题描述】:

我使用只有一层 (MVC) 的 MVC 3 构建了一个简单的调查工具。我现在很后悔。我所有的数据库访问和映射都在控制器和其他一些映射类中处理。

我想改用三层:

演示(MVC)
业务逻辑
数据/持久性 (EF)

我正在使用实体框架来处理数据库的所有内容。实体框架创建它自己的领域类。 MVC 使用的模型和 EF 创建的模型之间的映射应该放在哪里?

如果映射在业务层,MVC项目中是否需要Models文件夹?

调查问题由问题本身、行和列组成。这些是我使用的模型:

public class Question {

        public int Question_ID { get; set; }

        public Boolean Condition_Fullfilled;

        [Required(ErrorMessage = "Dette felt er påkrævet")]
        public String Question_Wording { get; set; }

        public String Question_Type { get; set; }

        [Required(ErrorMessage = "Dette felt er påkrævet")]
        public String Question_Order { get; set; }

        public String Left_scale { get; set; }
        public String Right_scale { get; set; }
        public int Scale_Length { get; set; }
        public String Left_Scale_HelpText { get; set; }
        public String Right_Scale_HelpText { get; set; }

        public Boolean Visible { get; set; }
        public Boolean IsAnswered { get; set; }
        public String Question_HelpText { get; set; }
        public int Category_ID { get; set; }

}
public class MatrixColumns
    {
        public int Column_ID { get; set; }
        public int Column_Number { get; set; }
        public String Column_Description { get; set; }
        public Boolean IsAnswer { get; set; }
        public int? Procent { get; set; }
        public bool Delete { get; set; }
        public bool Visible { get; set; }
        public int? Numbers { get; set; }
        public String Help_Text { get; set; }

    }

    public class MatrixRows
    {
        public bool Delete { get; set; }
        public bool Visible { get; set; }
        public int Row_Id { get; set; }
        public String Row_Number { get; set; }
        public String Row_Description { get; set; }
        public String Special_Row_CSS { get; set; }
        public String Help_Text { get; set; }
        // Dette er summen af procenterne af alle kolonner i rækken
        public int RowSum { get; set; }
    }

这些模型的所有数据都在控制器中检索,基于 QuestionID,并映射到如下所示的 ViewModel:

 public class ShowMatrixQuestionViewModel : Question
    {
        public Dictionary<MatrixRows, List<MatrixColumns>> columnrow { get; set; }
        public List<MatrixColumns> columns { get; set; }
        public List<MatrixRows> rows { get; set; }

        public ShowMatrixQuestionViewModel()
        {
            columns = new List<MatrixColumns>();
            rows = new List<MatrixRows>();
            columnrow = new Dictionary<MatrixRows, List<MatrixColumns>>();
        }
    }

所以当我想从我的控制器向视图发送ShowMatrixQuestionViewModel 时,我应该采取什么路线?

这是我的建议:

-> 控制器调用业务层中的一个方法叫

public ShowMatrixViewModel GetQuestion(int QuestionID) {}

-> GetQuestion 在数据层调用以下方法:

public Question GetQuestion(int QuestionId) {}
public MatrixRows GetRows(int QuestionId) {}
public MatrixColumns GetColumns(int id) {}

-> 实体框架返回“纯”对象,我想将其映射到我在上面发布的对象
-> GetQuestion 调用方法将 EF 模型映射到我自己的模型
-> Last GetQuestion 调用一个映射问题、行和列的方法:

    ShowMatrixQuestionViewModel model = MapShowMatrixQuestionViewModel(Question, MatrixRows, MatrixColumns)
return model;

这对吗?

提前致谢

【问题讨论】:

    标签: asp.net-mvc-3 design-patterns separation-of-concerns


    【解决方案1】:

    回答你问题的第一部分:

    “MVC 使用的模型和 EF 创建的模型之间的映射应该去哪里?”

    答案是 MVC 使用的模型由 EF 创建的模型。 ASP.NET MVC 项目中的 EF 工具是 Linq to SQL 类或 ADO.NET 实体框架模型。您应该在项目的 Models 文件夹中创建这些,它们提供您的数据/持久性 (EF)。

    【讨论】:

    • 调查中的一个问题包含来自 3 个不同 EF 模型的数据。我将这些模型中的数据映射到一个 ViewModel,该 ViewModel 包含所有必要的信息以显示一个问题。我的想法是,所有这些都可能在业务层中。 MVC 可以引用业务层,从而引用模型。
    • 听起来不错。如果您从三个单独的 EF 源获取数据,那么我将创建自己的类(即业务逻辑层),其中包含我需要来自三个 EF 源中的每一个的数据字段,以及我需要的任何额外数据和任何操作去做。 MVC 通过模型和您自己的自定义类将数据和业务逻辑分开。
    猜你喜欢
    • 2021-02-20
    • 2012-02-18
    • 2020-10-19
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    相关资源
    最近更新 更多