在最佳编程实践之后,Asp.Net Mvc应用程序所需/推荐的所有层的细分是什么?

时间:2021-08-11 04:11:51

The more I read into Asp.Net MVC the more layers and components I find out are required in order to make my application follow all of the standards and best programming practices.

我读入Asp.Net MVC的次数越多,我发现的层和组件就越多,以使我的应用程序遵循所有标准和最佳编程实践。

It's starting to get a bit confusing because some of the new layers don't seem to fit in as easily as the others I learnt. So I just wanted someone to go over all of the required/recommended layers for an Asp.Net MVC application- what purpose they serve and how they interact with the other layers.

它开始变得有点混乱,因为有些新层似乎不像我学到的其他层那么容易。所以我只是想让某人查看Asp.Net MVC应用程序的所有必需/推荐层 - 它们服务的目的是什么以及它们如何与其他层交互。

Here's a few of the layers I've found and how they link up: (Some of them may be wrong)

以下是我发现的一些层次以及它们如何连接起来:(其中一些可能是错误的)

View/UI --> Model Binder --> Controller --> Service Layer --> Repository --> Entity Framework/LINQ to SQL --> DB

Could someone go over ones I may be missing, how they all link up, and what each of their purposes are?

有人可能会遗漏我可能会失踪的人,他们如何联系起来,以及他们的目的是什么?

Thanks,
Matt

谢谢,马特

2 个解决方案

#1


2  

Good question, I think you covered all the layers I have seen: Modal binder and service layer are optional.

好问题,我认为你涵盖了我所看到的所有层:模态绑定器和服务层是可选的。

Maybe, you can add another Error Handling layer such as elmah.

也许,你可以添加另一个错误处理层,如elmah。

  • View/UI --> You put your html markup / Javascript code.
  • 查看/ UI - >你把你的HTML标记/ Javascript代码。
  • Model Binder --> You perform the magic to bind your input to the action parameters, normally, you will use the default binder, so you don't need worry about it. However, you can override this with your own binder, and do validation in this layer. Here is a good example on this.
  • 模型Binder - >您可以执行魔术将输入绑定到动作参数,通常,您将使用默认的活页夹,因此您不必担心它。但是,您可以使用自己的绑定器覆盖它,并在此层中进行验证。这是一个很好的例子。
  • Controller --> Enough documentation online.
  • 控制器 - >在线有足够的文档。
  • Service Layer --> A lot of people do validation and other business logic processing here before sending it to repository. Asp.net mvc contact manger example has a good example here. This is also the layer to actually work with your modal.
  • 服务层 - >在将数据发送到存储库之前,很多人都在这里进行验证和其他业务逻辑处理。 Asp.net mvc联系manger示例在这里有一个很好的例子。这也是实际使用模态的图层。
  • Repository --> Simple read/write operation.
  • 存储库 - >简单的读/写操作。
  • Entity Framework/LINQ to SQL --> DB - Actually writing to database. Nhibernate is another good candidate here.
  • 实体框架/ LINQ to SQL - > DB - 实际写入数据库。 Nhibernate是另一个很好的候选人。

#2


0  

First of all, I think software and patterns have a tendency to overcomplicate things. As the ASP name implies the main idea of the framework is Model-View-Controller (MVC). You could be able to put a lot of things between these components, including DBs, Services, APIs, etc. However, the main concept of the Model-View-Controller pattern is pretty simple: Separate those functionalities into modules so the project could be easier to mantain.

首先,我认为软件和模式倾向于使事情过于复杂。正如ASP名称所暗示的那样,框架的主要思想是模型 - 视图 - 控制器(MVC)。您可以在这些组件之间放置很多东西,包括DB,服务,API等。但是,模型 - 视图 - 控制器模式的主要概念非常简单:将这些功能分成模块,以便项目可以更容易保持。

MVC could be applied to ANY programming or scripting you do. Even for a shell script MVC could be helpfull. Here are some examples of each one:

MVC可以应用于任何编程或脚本。即使对于shell脚本,MVC也可能有所帮助。以下是每个例子:

  • View - Here is how the user interacts. It could be a web page, and Windows Form, or a command line interface.
  • 查看 - 以下是用户交互的方式。它可以是网页,Windows窗体或命令行界面。
  • Controller - The brains of the program, it should be aware of everything, but should be pretty simple. It basically gets messages or events from the view and/or model and decides on what to do. Good controllers are basically a dispatcher of events. Depending on events, it call view or model methods. In ASP MVC, the controller is the one providing the ActionResults for the View and interacts with the Model.
  • 控制器 - 程序的大脑,应该知道一切,但应该很简单。它基本上从视图和/或模型中获取消息或事件,并决定要做什么。好的控制器基本上是事件的调度员。根据事件,它调用视图或模型方法。在ASP MVC中,控制器是为View提供ActionResults并与Model交互的控制器。
  • Model - This is basically where the data is. This could be a DB, file system, a Web Session, or memory.
  • 模型 - 这基本上是数据的位置。这可以是数据库,文件系统,Web会话或内存。

Now the good part. The Controller does not care how the View is managing the interaction with the user. It could be a command line interface or a web form. The Controller does not know how the data is stored, it does not matters if it is a DB or a file. It just request data and pass it to the View. It not of its business to know how the view is getting the inputs, or the model the data.

现在好的一部分。 Controller不关心View如何管理与用户的交互。它可以是命令行界面或Web表单。 Controller不知道数据是如何存储的,如果是数据库或文件则无关紧要。它只是请求数据并将其传递给View。知道视图如何获取输入或模型数据并不是它的业务。

Then the question, Why the hell do we want to overcomplicate things with this pattern? Well, Imagine that you have an MVC application using a MySQL DB and know you want to use SQL Server. What module you should change? Obviously the Model is the one affected. The Controller and the View should not have any major impacts. Now, imagine that you have another MVC application using Windows Forms, and now you want to change it to Web Forms? Well basically the View is the one that will be affected (and some parts of the controller), but your Model should be the same.

那么问题,为什么我们想用这种模式过度复杂化?好吧,想象一下,你有一个使用MySQL数据库的MVC应用程序,并且知道你想使用SQL Server。你应该改变什么模块?显然模型是受影响的模型。 Controller和View不应产生任何重大影响。现在,假设您有另一个使用Windows窗体的MVC应用程序,现在您想将其更改为Web窗体?基本上View是受影响的(和控制器的某些部分),但你的模型应该是相同的。

In summary, MVC is a great pattern, and it should be used more. However, I think there are some projects which are not suitable for MVC due its simplicity. It will be like building a laser to kill flies. Of course you will kill them, but the effort is not worthy on all cases.

总之,MVC是一个很好的模式,它应该被更多地使用。但是,我认为有些项目由于其简单性而不适合MVC。这就像制造激光来杀死苍蝇一样。当然你会杀了他们,但是在所有情况下这种努力都不值得。

#1


2  

Good question, I think you covered all the layers I have seen: Modal binder and service layer are optional.

好问题,我认为你涵盖了我所看到的所有层:模态绑定器和服务层是可选的。

Maybe, you can add another Error Handling layer such as elmah.

也许,你可以添加另一个错误处理层,如elmah。

  • View/UI --> You put your html markup / Javascript code.
  • 查看/ UI - >你把你的HTML标记/ Javascript代码。
  • Model Binder --> You perform the magic to bind your input to the action parameters, normally, you will use the default binder, so you don't need worry about it. However, you can override this with your own binder, and do validation in this layer. Here is a good example on this.
  • 模型Binder - >您可以执行魔术将输入绑定到动作参数,通常,您将使用默认的活页夹,因此您不必担心它。但是,您可以使用自己的绑定器覆盖它,并在此层中进行验证。这是一个很好的例子。
  • Controller --> Enough documentation online.
  • 控制器 - >在线有足够的文档。
  • Service Layer --> A lot of people do validation and other business logic processing here before sending it to repository. Asp.net mvc contact manger example has a good example here. This is also the layer to actually work with your modal.
  • 服务层 - >在将数据发送到存储库之前,很多人都在这里进行验证和其他业务逻辑处理。 Asp.net mvc联系manger示例在这里有一个很好的例子。这也是实际使用模态的图层。
  • Repository --> Simple read/write operation.
  • 存储库 - >简单的读/写操作。
  • Entity Framework/LINQ to SQL --> DB - Actually writing to database. Nhibernate is another good candidate here.
  • 实体框架/ LINQ to SQL - > DB - 实际写入数据库。 Nhibernate是另一个很好的候选人。

#2


0  

First of all, I think software and patterns have a tendency to overcomplicate things. As the ASP name implies the main idea of the framework is Model-View-Controller (MVC). You could be able to put a lot of things between these components, including DBs, Services, APIs, etc. However, the main concept of the Model-View-Controller pattern is pretty simple: Separate those functionalities into modules so the project could be easier to mantain.

首先,我认为软件和模式倾向于使事情过于复杂。正如ASP名称所暗示的那样,框架的主要思想是模型 - 视图 - 控制器(MVC)。您可以在这些组件之间放置很多东西,包括DB,服务,API等。但是,模型 - 视图 - 控制器模式的主要概念非常简单:将这些功能分成模块,以便项目可以更容易保持。

MVC could be applied to ANY programming or scripting you do. Even for a shell script MVC could be helpfull. Here are some examples of each one:

MVC可以应用于任何编程或脚本。即使对于shell脚本,MVC也可能有所帮助。以下是每个例子:

  • View - Here is how the user interacts. It could be a web page, and Windows Form, or a command line interface.
  • 查看 - 以下是用户交互的方式。它可以是网页,Windows窗体或命令行界面。
  • Controller - The brains of the program, it should be aware of everything, but should be pretty simple. It basically gets messages or events from the view and/or model and decides on what to do. Good controllers are basically a dispatcher of events. Depending on events, it call view or model methods. In ASP MVC, the controller is the one providing the ActionResults for the View and interacts with the Model.
  • 控制器 - 程序的大脑,应该知道一切,但应该很简单。它基本上从视图和/或模型中获取消息或事件,并决定要做什么。好的控制器基本上是事件的调度员。根据事件,它调用视图或模型方法。在ASP MVC中,控制器是为View提供ActionResults并与Model交互的控制器。
  • Model - This is basically where the data is. This could be a DB, file system, a Web Session, or memory.
  • 模型 - 这基本上是数据的位置。这可以是数据库,文件系统,Web会话或内存。

Now the good part. The Controller does not care how the View is managing the interaction with the user. It could be a command line interface or a web form. The Controller does not know how the data is stored, it does not matters if it is a DB or a file. It just request data and pass it to the View. It not of its business to know how the view is getting the inputs, or the model the data.

现在好的一部分。 Controller不关心View如何管理与用户的交互。它可以是命令行界面或Web表单。 Controller不知道数据是如何存储的,如果是数据库或文件则无关紧要。它只是请求数据并将其传递给View。知道视图如何获取输入或模型数据并不是它的业务。

Then the question, Why the hell do we want to overcomplicate things with this pattern? Well, Imagine that you have an MVC application using a MySQL DB and know you want to use SQL Server. What module you should change? Obviously the Model is the one affected. The Controller and the View should not have any major impacts. Now, imagine that you have another MVC application using Windows Forms, and now you want to change it to Web Forms? Well basically the View is the one that will be affected (and some parts of the controller), but your Model should be the same.

那么问题,为什么我们想用这种模式过度复杂化?好吧,想象一下,你有一个使用MySQL数据库的MVC应用程序,并且知道你想使用SQL Server。你应该改变什么模块?显然模型是受影响的模型。 Controller和View不应产生任何重大影响。现在,假设您有另一个使用Windows窗体的MVC应用程序,现在您想将其更改为Web窗体?基本上View是受影响的(和控制器的某些部分),但你的模型应该是相同的。

In summary, MVC is a great pattern, and it should be used more. However, I think there are some projects which are not suitable for MVC due its simplicity. It will be like building a laser to kill flies. Of course you will kill them, but the effort is not worthy on all cases.

总之,MVC是一个很好的模式,它应该被更多地使用。但是,我认为有些项目由于其简单性而不适合MVC。这就像制造激光来杀死苍蝇一样。当然你会杀了他们,但是在所有情况下这种努力都不值得。