简体   繁体   English

为什么我们应该将模型引用单独添加到MVC前端,而不是从服务引用中使用它?

[英]Why should we add Model reference separately to MVC front end instead of using it from service reference?

In older web projects that I worked on, we used to create models in DAL, add reference of DAL in Business Logic Layer (and reuse models from DAL as they would be available with reference of DAL), Add reference of BL in Service (again reuse models). 在我从事的较旧的Web项目中,我们曾经在DAL中创建模型,在Business Logic Layer中添加了DAL的引用(并重用了DAL中的模型,因为它们可以在DAL的引用中使用),在服务中添加BL的引用(同样)重用模型)。 Entities were available transitively in all successive layers. 实体在所有连续层中都可传递。

In a MVC project with multiple layers, Models are often added in a separate class library project and referenced across all layers like DAL, Business Logic, Service, FrontEnd etc; 在具有多层的MVC项目中,通常将模型添加到单独的类库项目中,并跨DAL,业务逻辑,服务,前端等所有层进行引用; Even though they are transitively available. 即使它们是可传递的。

Is there any specific reason to do this? 有什么具体原因吗? Why shouldn't we bind Models available through service in frontend like below 我们为什么不应该像下面这样绑定前端可通过服务获得的模型

@model List<TestSolution.TestServiceRef.Employee>

instead of 代替

@model List<TestSolution.Models.Employee>

What is the advantage of referring models separately in all layers over using it from the reference of another/previous layer? 与从另一个/上一个层的引用中使用模型相比,在所有层中分别引用模型的优点是什么?

I don't have a lot of MVC experience (as far as ASP.Net MVC), but as I understand it the Model in MVC is just a representation of the data structures as the coded understands it (ie at runtime) - and isn't necessarily the underlying data itself (ie database). 我没有很多MVC经验(就ASP.Net MVC而言),但是据我了解,MVC中的模型只是代码结构所理解的数据结构的一种表示(即在运行时),并且不一定是基础数据本身(即数据库)。

If you have a concept that you want to represent in the UI, then obviously the UI needs to know what that concept is - hence referencing it at that level (and other such as the business logic, etc). 如果您有要在UI中表示的概念,则显然UI需要知道该概念是什么-因此在该级别(以及其他诸如业务逻辑等)引用它。 Pre MVC there was an approach / architecture I followed where the "model" was just a bunch of POCO's (plain old class objects - .e. really simple dumb classes or structs). 在MVC之前,我遵循了一种方法/体系结构,其中的“模型”只是一堆POCO(普通的旧类对象,即非常简单的哑类或结构)。

These POCO's could go into a an assemply/project like MyApp.Common from where you could safely reference them in any other project / layer of the architecture (UI, Logic, DAL, etc). 这些POCO的可以去到一个assemply /像项目MyApp.Common从那里,你可以放心地引用它们的架构(UI,逻辑,DAL等)的任何其他项目/层。 This allows all layers of the application to "talk the same language", so to speak. 可以这样说,应用程序的所有层都可以“说相同的语言”。

I did a proper write up of this architectural style (which is not MVC, but shares some concepts), here: https://morphological.wordpress.com/2011/08/29/5-layer-architecture/ 我对这种架构风格(不是MVC,但共享一些概念)做了适当的写法,在这里: https : //morphological.wordpress.com/2011/08/29/5-layer-architecture/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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