1

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

我想切换到使用三层:

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

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

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

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

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 模型映射到我自己的模型
-> 最后 GetQuestion 调用一个映射问题、行和列:

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

这个对吗?

提前致谢

4

1 回答 1

0

要回答您问题的第一部分:

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

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

于 2012-04-25T16:00:56.293 回答