简体   繁体   中英

What is the proper sequence of method calls when using a multi layered architecture?

I have built a simple survey-tool using MVC 3 with only 1 layer (MVC). I regret this now. All my database access and mapping is handled in the controllers, and some other mapping classes.

I would like to switch over to using three layers:

Presentation (MVC)
Business Logic
Data / Persistence (EF)

I am using the Entity Framework to handle everything with the database. The entity framework creates it's own domain classes. Where should the mapping between the Models that MVC uses and the models that EF creates go?

If the mapping is in the business layer, is there a need for the Models folder in the MVC project?

A survey-question consists of the Question itself, Rows and Columns. Theese are the models that i use:

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; }
    }

All the data for theese models is retrieved in the Controller, based upon a QuestionID, and mapped to a ViewModel that looks like this:

 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>>();
        }
    }

So when i want to send a ShowMatrixQuestionViewModel to a View from my Controller, what is the route i should take?

This is my suggestion:

-> Controller calls a method in the business layer called

public ShowMatrixViewModel GetQuestion(int QuestionID) {}

-> GetQuestion calls the following methods in the data layer:

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

-> Entity framework returns "pure" objects, which i want to map over to the ones i posted above
-> GetQuestion calls methods to map the EF models to my own models
-> Last GetQuestion calls a method that maps the Questions, Rows and Columns:

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

Is this correct?

Thanks in advance

To answer the first part of your question:

"Where should the mapping between the Models that MVC uses and the models that EF creates go?"

The answer is that the models MVC uses are the models created by the EF. Your EF tool in the ASP.NET MVC project is either Linq to SQL Classes or the ADO.NET Entity Framework Model. You should create these inside the Models folder in your project and they provide your data / persistence (EF).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM