如何处理在多层应用的观点 [英] How to handle views in a multilayer-application

查看:132
本文介绍了如何处理在多层应用的观点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作有三个基本层次的​​一个项目:presentation,业务和数据。
每一层是不同的项目,所有的层使用在另一个项目中定义的DTO的。
业务层和数据层查询数据库时返回DTO的或列表的DTO的。

I'm working on a project which has basically three layers: Presentation, business and data. Each layer is on a different project and all layers use DTO's defined in another project. business layer and data layer return DTO's or Lists of DTOs when querying the database.

到目前为止好,但现在我们要查询的观点,当然这些观点不相符的existant DTO。我们所做的到现在为止仅仅是创建一个特殊的DTO,业务和数据层类,因此,他们像正常的实体处理(减号插入,更新等)

So far so good, but now we have to query views and those views of course do not match an existant DTO. What we have done until now is just create a special DTO, business- and data-layer classes so they were treated like normal entities (minus insert, update etc.)

但它似乎并不正确。为什么要进行像正常的实体处理时,他们显然不是。井DTO似乎是必要的,但打造商业逻辑和一个数据层类,每观点似乎相当akward的。所以我想我创建一个通用的业务和持有逻辑/ code所有视图数据层类(我仍然要创建一个DTO为每个不同的看法,也许我可以用匿名类型)

But it does not seem correct. Why should the be treated like normal entities when they are clearly not. Well the DTO seems necessary, but creating "business logic" and a datalayer-class for every view seems rather akward. So I thought I create one generic business- and datalayer class which holds the logic/code for all views (I still would have to create a DTO for every different view, perhaps I could use anonymous types)

你怎么看待我的想法,或者你将如何解决这个问题呢?

What do you think about me idea or how would you solve this issue?

编辑:2011年8月9
对不起,后可能不清楚。
通过意见我是从SQL服务器的次数。

9. August 2011 Sorry, the post may have been unclear. By views I meant Views from a sql-server.

推荐答案

我完全觉得你的痛苦。事实是,在体面的复杂性几乎每一个不平凡的项目,你将得到一个东西,你必须表现出对UI重叠,聚集的用户或只是业务实体的数据子集的地步。我倾向于解决这个问题的方法是接受这个事实,并走得更远 - 从业务逻辑侧查询方从逻辑上和物理上分开。事实是,你需要你的实体只有实际业务操作和保持业务的有效约束,当发生这种情况?只有当有人更改数据。因此,有没有必要甚至建立实体时,显示的数据。
我喜欢结构化解决方案的方式是:

I feel your pain completely. The fact is that in almost every non trivial project with decent complexity you will get to the point where the things you have to show to the users on UI overlap, aggregate or are simply a subset of data of business entities. The way I tend to approach this is to accept this fact and go even further - separate the query side from the business logic side both logically and physically. The fact is that you need your entities only for actual business operations and keeping the business constraints valid, and when does this happen? Only when someone changes the data. So there is no need to even build entities when you display the data. The way I like to structure the solutions is:

用户打开视图 - >查询时,才执行,以获得特定
  该视图数据 - >返回的数据模型(虽然你可以
  称之为DTO为好,在这种情况下,同样的事情)

User opens the view -> Query is performed only to get the specific data for the view -> Returned data is the model (although you could call it a DTO as well, in this case it's the same thing)

用户改变的东西 - >控制器(或服务)从回购建立完整的实体,
  是对实体执行业务逻辑操作 - >更改
  持久化了 - >返回结果

User changes something -> Controller (or service) builds the full entity from repo, business logic action is performed on the entity -> changes are persisted -> result is returned

我想说的是,它是确定从侧面写分开对待你的读取端。它是确定有不同的基础设施这一点。当你开始以不同的方式对待它,你会看到它的好处 - 例如,你可以定制你查询你需要什么样的用户界面。
你甚至可以得到的地步,你的基础设施将允许用不同的技术来构建查询,例如使用LINQ或纯SQL查询 - 什么是最适合某些情况下。

What I want to say is, it is ok to treat your read side separately from write side. It is ok to have different infrastructure for this as well. When you start to treat it differently, you will see the benefits - for example you can tailor you queries to what you need on UI. You can even get to the point where your infrastructure will allow to build your queries with different techniques, for example using LINQ or plain SQL queries - what is best for certain scenarios.

这篇关于如何处理在多层应用的观点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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