简体   繁体   English

应该放置n层应用程序的Web项目中的视图模型?

[英]Where view models inside Web project of n-tier application should be placed?

Let's assume we have an ASP.NET MVC web application with following tiers: 假设我们有一个包含以下层的ASP.NET MVC Web应用程序:

  • Business logic 商业逻辑
  • Entities (business domain and database POCOs) 实体(业务域和数据库POCO)
  • Common (resources, consts) 共同(资源,争议)
  • Data access (database EF queries, EDMX EF models and so on) 数据访问(数据库EF查询,EDMX EF模型等)
  • Web application (MVC web application) Web应用程序(MVC Web应用程序)

We're using view models approach. 我们正在使用视图模型方法。 Currently view models are placed in Entities layer. 目前,视图模型位于“实体”层中。 Data access queries returns view models (due to efficiency issues, so we avoid using mapper). 数据访问查询返回视图模型(由于效率问题,因此我们避免使用mapper)。

Web layer references all other layers. Web图层引用所有其他图层。 Data access references Common and Entities layers. 数据访问引用Common和Entities层。 Business logic references Entities and Common layers, in the future also Data access layer. 业务逻辑引用实体和公共层,将来也是数据访问层。

There's an idea to move view models to Web layer. 有一个想法将视图模型移动到Web层。 Why? 为什么? Because they're in fact bound with a particular technology (MVC) and UI implementation. 因为它们实际上与特定技术(MVC)和UI实现绑定在一起。 But we're facing a problem here, because in this scenario Data access layer must reference Web and Web references Data access, so we have a circular dependency issue. 但是我们在这里面临一个问题,因为在这种情况下,数据访问层必须引用Web和Web引用数据访问,因此我们有一个循环依赖问题。

Moreover we have scenario when some validation of view model requires reference to Data access layer. 此外,我们有一些场景,当视图模型的某些验证需要引用数据访问层时。 We're going to keep validation method inside view models. 我们将在视图模型中保留验证方法。 Currently we want to implement it by injecting database context class (which is in Data access layer) to view model by constructor. 目前我们希望通过注入数据库上下文类(在数据访问层中)来通过构造函数来查看模型来实现它。

Do you have any idea how can we avoid it? 你知道我们怎么能避免它? Is it good idea to keep view models inside our Web layer? 将视图模型保留在Web层中是否是个好主意?

I'm unsure how you can use ViewModel in web apps. 我不确定如何在Web应用程序中使用ViewModel AFAIK it's harder than in desktop app due to it's data-binding nature. AFAIK由于具有数据绑定特性,因此比桌面应用程序更难。

However, your web layer referenced directly to data layer (or rather, access directly). 但是,您的Web层直接引用数据层(或者更确切地说,直接访问)。 Unless it is used to help simplify the UI and not the business process, it is breaking the purpose of N-tier aswell. 除非它用于帮助简化UI而不是业务流程,否则它将打破N层的目的。

What you need to do to keep the design clean is making 2 different model. 为保持设计清洁,您需要做的是制作2种不同的型号。 One for Entities model (Domain/data/business model) and another for view model . 一个用于实体模型(域/数据/业务模型),另一个用于视图模型 Your view model is placed at web layer and when received the domain model, it is being mapped to web layer. 您的视图模型位于Web层,当收到域模型时,它将映射到Web层。 You cannot avoid mapping here. 你不能避免这里的映射。

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

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