其中,鉴于N层应用程序的Web项目内部模型应该放在哪里? [英] Where view models inside Web project of n-tier application should be placed?

查看:127
本文介绍了其中,鉴于N层应用程序的Web项目内部模型应该放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有以下层次的ASP.NET MVC Web应用程序:

Let's assume we have an ASP.NET MVC web application with following tiers:


  • 商业逻辑

  • 实体(业务领域和数据库波苏斯)

  • 通用​​(资源,consts)

  • 数据访问(数据库查询EF,EF EDMX模型等)

  • 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).

网络层引用的所有其他层。
数据访问引用通用和实体层。
业务逻辑引用实体和公共层,将来也数据访问层。

Web layer references all other layers. Data access references Common and Entities layers. Business logic references Entities and Common layers, in the future also Data access layer.

有一个想法到视图模型转移到网络层。为什么?因为他们实际上是与特定技术(MVC)和UI实现的约束。
但是,我们在这里面临的一个问题,因为在这种情况下的数据访问层必须引用网络和网络引用数据访问,所以我们有一个循环依赖的问题。

There's an idea to move view models to Web layer. Why? Because they're in fact bound with a particular technology (MVC) and UI implementation. 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.

此外,我们有方案时,视图模型的一些验证要求引用数据访问层。我们将继续验证方法视图模型中。目前,我们希望通过注入数据库上下文类(这是数据访问层)通过构造函数来查看模型来实现它。

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.

你有什么想法我们怎么能避免呢?这是好主意,让我们的Web层的内部视图模型?

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. AFAIK it's harder than in desktop app due to it's data-binding nature.

然而,你的web层直接(直接或更确切地说,访问)引用的数据层。除非它是用来帮助简化用户界面,而不是业务过程中,打破N层藏汉的目的。

However, your web layer referenced directly to data layer (or rather, access directly). Unless it is used to help simplify the UI and not the business process, it is breaking the purpose of N-tier aswell.

你需要做的,以保持清洁的设计是使2种不同的模式是什么。 一个用于实体模型(域/数据/商业模式),另一个视图模型。视图模型被放置在网层以及当接收到的域模型,它被映射到网络层。你无法避免这里的映射。

What you need to do to keep the design clean is making 2 different model. 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. You cannot avoid mapping here.

这篇关于其中,鉴于N层应用程序的Web项目内部模型应该放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆