的N-分层不使用ORM,如何界面中指定它所需要的数据,以显示数据库应用程序? [英] N-layered database application without using an ORM, how does the UI specify what it needs of data to display?

查看:194
本文介绍了的N-分层不使用ORM,如何界面中指定它所需要的数据,以显示数据库应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要找的指针和信息在这里,因为我怀疑它有没有单一的一个正确答案,我会做这个CW。这是C#,所以我就对下面的LINQ一些参考。我也道歉了长期职位。让我在这里总结一下问题,然后全部问题如下

I'm looking for pointers and information here, I'll make this CW since I suspect it has no single one correct answer. This is for C#, hence I'll make some references to Linq below. I also apologize for the long post. Let me summarize the question here, and then the full question follows.

摘要:在UI / BLL / DAL / DB 4层的应用,怎么能更改为用户界面,显示更多的列(在网格中说的),避免因业务逻辑层,到数据访问层漏水,得到的数据保持显示(假设它已经在数据库中)。

Summary: In a UI/BLL/DAL/DB 4-layered application, how can changes to the user interface, to show more columns (say in a grid), avoid leaking through the business logic layer and into the data access layer, to get hold of the data to display (assuming it's already in the database).


让我们假设有3(4)层的分层应用程序:

Let's assume a layered application with 3(4) layers:


  • 的用户界面(UI)

  • 业务逻辑层(BLL)

  • 数据访问层( DAL)的

  • 数据库(DB;第4层)

  • User Interface (UI)
  • Business Logic Layer (BLL)
  • Data Access Layer (DAL)
  • Database (DB; the 4th layer)

在这种情况下,DAL是负责构建SQL语句和执行这些针对数据库,返回数据。

In this case, the DAL is responsible for constructing SQL statements and executing them against the database, returning data.

是为了正确构建这样一个层,只是一直做选择的唯一途径* ?对我来说这是一个很大的禁忌,但让我解释一下为什么我不知道。

Is the only way to "correctly" construct such a layer to just always do "select *"? To me that's a big no-no, but let me explain why I'm wondering.

让我们说,我想,对于我的用户界面,可显示已全体员工积极的就业记录。通过活动我的意思是就业记录从到日期中含有今日(或者甚至一个日期,我可以在用户界面中设置)。

Let's say that I want, for my UI, to display all employees that have an active employment record. By "active" I mean that the employment records from-to dates contains today (or perhaps even a date I can set in the user interface).

在这种情况下,比方说,我想发一封电子邮件给所有的人,所以我必须在确保我尚未发出电子邮件给同样的人已经等了BLL一些代码。

In this case, let's say I want to send out an email to all of those people, so I have some code in the BLL that ensures I haven't already sent out email to the same people already, etc.

有关的BLL,它需要很少的数据。也许它调用了数据访问层获得在职职工的名单,然后打电话把它发出了邮件列表。然后它加入这些并构造一个新的列表。也许这可以与数据访问层的帮助下完成的,这并不重要。

For the BLL, it needs minimal amounts of data. Perhaps it calls up the data access layer to get that list of active employees, and then a call to get a list of the emails it has sent out. Then it joins on those and constructs a new list. Perhaps this could be done with the help of the data access layer, this is not important.

重要的是,对于业务层,有它需要真的没有太多的数据。也许它只是需要为每个员工的唯一标识,这两个名单,以配合后,然后说:这些都是积极的,你还没有发出一封电子邮件给这些的唯一标识符。难道我那么构造构造SQL语句只检索什么业务层的需求DAL代码? IE浏览器。只是SELECT ID FROM雇员WHERE ......?

What's important is that for the business layer, there's really not much data it needs. Perhaps it just needs the unique identifier for each employee, for both lists, to match upon, and then say "These are the unique identifiers of those that are active, that you haven't already sent out an email to". Do I then construct DAL code that constructs SQL statements that only retrieve what the business layer needs? Ie. just "SELECT id FROM employees WHERE ..."?

我怎么为用户界面怎么办呢?对于用户来说,这或许会是最好的,包括了很多信息,这取决于的为什么的我想发送电子邮件。举例来说,我可能要包括一些基本的联系人信息,或者他们工作的部门,或者他们的经理的名字等等,不是说我至少姓名和电子邮件地址信息来显示。

What do I do then for the user interface? For the user, it would perhaps be best to include a lot more information, depending on why I want to send out emails. For instance, I might want to include some rudimentary contact information, or the department they work for, or their managers name, etc., not to say that I at least name and email address information to show.

如何在UI获得这些数据?难道我改变DAL,以确保我返回足够的数据返回给用户界面?难道我改变BLL,以确保它的UI返回足够的数据?如果从DAL回BLL返回的对象或数据结构可以被发送到用户界面,以及,也许BLL不需要变化的多,但随后的用户界面的影响的要求超出了它应与通信的层。并且如果两个世界上不同的数据结构进行操作,改变很可能不得不进行到两个

How does the UI get that data? Do I change the DAL to make sure I return enough data back to the UI? Do I change the BLL to make sure that it returns enough data for the UI? If the object or data structures returned from the DAL back to the BLL can be sent to the UI as well, perhaps the BLL doesn't need much of a change, but then requirements of the UI impacts a layer beyond what it should communicate with. And if the two worlds operate on different data structures, changes would probably have to be done to both.

和什么然后当UI被改变,以帮助用户甚至此外,通过添加更多的列,有多深会/我应该在为了改变UI去? (假设数据存在于已因此不需要没有变化的数据库。)

And what then when the UI is changed, to help the user even further, by adding more columns, how deep would/should I have to go in order to change the UI? (assuming the data is present in the database already so no change is needed there.)

一个建议,想出了是使用LINQ到SQL和IQueryable的,因此,如果DAL,它与涉及的内容(如在什么类型的数据),以及为什么(如在WHERE子句)返回IQueryables,BLL的可能返回这些到用户界面中,然后可以构建的LINQ查询将检索它需要的数据。然后,用户接口代码可以拉在它需要的列。这工作,因为与IQuerables,用户界面最终将实际执行查询,然后它可以使用选择新{X,Y,Z}来指定它需要什么,甚至在其他表连接,如果需要的话。

One suggestion that has come up is to use Linq-To-SQL and IQueryable, so that if the DAL, which deals with what (as in what types of data) and why (as in WHERE-clauses) returned IQueryables, the BLL could potentially return those up to the UI, which could then construct a Linq-query that would retrieve the data it needs. The user interface code could then pull in the columns it needs. This would work since with IQuerables, the UI would end up actually executing the query, and it could then use "select new { X, Y, Z }" to specify what it needs, and even join in other tables, if necessary.

这看起来杂乱无章我。该UI执行SQL代码本身,尽管它一直隐藏一个LINQ前台后面。

This looks messy to me. That the UI executes the SQL code itself, even though it has been hidden behind a Linq frontend.

不过,要做到这一点,在BLL或DAL不应该允许关闭数据库连接,并在一个IoC类型的世界中,DAL-服务可能会设置在略高于UI代码越早想,这样的LINQ查询可能刚刚结束与异常无法访问已释放的对象

But, for this to happen, the BLL or the DAL should not be allowed to close the database connections, and in an IoC type of world, the DAL-service might get disposed of a bit sooner than the UI code would like, so that Linq query might just end up with the exception "Cannot access a disposed object".

所以我在寻找指针。我们如何为期不远了?你是如何处理的呢?我认为,要改变用户界面将通过BLL,进入DAL一个非常糟糕的解决方案泄漏的事实,但现在它看起来并不像我们可以做的更好。

So I'm looking for pointers. How far off are we? How are you dealing with this? I consider the fact that changes to the UI will leak through the BLL and into the DAL a very bad solution, but right now it doesn't look like we can do better.

请告诉我,我们是多么愚蠢和证明我错了?

Please tell me how stupid we are and prove me wrong?

和注意,这是一个遗留系统。更改数据库模式是不是在范围几年还,所以一个解决方案中使用ORM对象将基本上做相当于SELECT *是不是一个真正的选择。我们有我们想避免通过层的整个列表拉了一些大表。

And note that this is a legacy system. Changing the database schema isn't in the scope for years yet, so a solution to use ORM objects that would essentially do the equivalent of "select *" isn't really an option. We have some large tables that we'd like to avoid pulling up through the entire list of layers.

推荐答案

使用的概念视图模型(或数据传输对象),这些UI的消费情况。这将是BLL的作业采取这些对象,如果数据是不完整的,要求额外的数据(我们称之为模型)。然后,BLL可以对哪些视图模型返回正确的决策。不要让你的模型(数据)细节渗透到用户界面。

Use the concept of a view model (or data transfer objects) that are UI consumption cases. It will be the job of the BLL to take these objects and if the data is incomplete, request additional data (which we call model). Then the BLL can make correct decisions about what view models to return. Don't let your model (data) specifics permeate to the UI.

UI <-- (viewmodel) ---> BLL <-- (model) --> Peristence/Data layers

这让解耦,以更好地扩展你的应用程序。持久的独立性,我认为很自然地落在了这种方法,因为视图模型的建设和规范可以通过使用linq2ql或其他ORM技术在BLL灵活地完成。

This decoupling lets to scale you application better. The persistence independence I think just naturally falls out of this approach, as construction and specification of the view models could done flexibly in the BLL by using linq2ql or another orm technology.

这篇关于的N-分层不使用ORM,如何界面中指定它所需要的数据,以显示数据库应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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