带有延迟加载的 DAL 和 BLL

发布于 2024-09-16 01:23:28 字数 837 浏览 3 评论 0原文

如何在三层上下文中实现延迟加载?我了解表示层、业务层和数据层的基本架构:

您拥有基本的“哑”类,它们几乎是数据库中表的镜像,但有一个例外。您可以引用所引用内容的实际实例,而不是外键 ID。例如:具有姓名/出生日期/职务属性的员工。

然后,对于每个类,您都有一个提供 CRUD 操作的类以及您可能需要的任何自定义数据存储例程(调用与该对象配合使用的存储过程等)。如果您更改数据库,此类将被换出。例如: EmployeeDAL.Save(myEmployee)、EmployeeDAL.Get(myEmployee)(其中 myEmployee 填充了 ID,但没有其他内容)

您有执行验证等操作的业务层类。这些类中的方法通常以调用 DAL 来保存信息或检索信息结束。当客户改变对有效/无效数据构成的想法或想要改变某些计算的完成方式时,这种情况就会发生变化。

表示层与业务层交互以显示内容并将 UI 中进行的插入/更新传递到较低层。例如:它循环遍历员工列表并将其显示在 HTML 表中。

但是延迟加载引用的代码到底会去哪里呢?如果表示层有一个刚刚显示的 Company 对象,并且正在开始显示 myCompany.Employees 的过程,那么这是如何实现的? myCompany 是镜像数据库表的哑类之一的实例,并且不应该知道如何检索任何内容。

您是否按照这个问题的答案建议并创建每个对象的虚拟版本?然后,DAL 级别对象可以具有指示员工是否已加载的变量,并调用 DALEmployee.GetEmployees(this)?我觉得我好像错过了关于该模式的一些重要内容......

How would one implement lazy loading in the context of three tiers? I understand the basic architecture of Presentation Layer, Business Layer, and Data Layer:

You have your basic "dumb" classes that are nearly mirror images of the tables in the database with one exception. Instead of foreign key IDs you have a reference to the actual instance(s) of what is being referred to. For example: Employee with Name/DOB/Title properties.

Then for each of these classes you have a class that provides the CRUD operations on it plus any custom data storage routines you might need (calling a stored procedure that works with that object, etc). This class would be swapped out if you changed database. For example: EmployeeDAL.Save(myEmployee), EmployeeDAL.Get(myEmployee) (where myEmployee has their ID populated but nothing else)

You have business layer classes that perform validation and what not. The methods in these classes usually end by calling into the DAL to persist information or to retrieve it. This is changed when the customer changes their mind about what constitutes valid/invalid data or wants to change the way some calculation is done.

The presentation layer interacts with the business layer to display things and shuttle inserts/updates made in the UI to the lower layers. For example: it loops over a list of Employees and displays them in an HTML table.

But where exactly would the code for lazy loading references go? If the presentation layer has a Company object that it just displayed and is beginning the process of displaying myCompany.Employees, how is that achieved? myCompany is an instance of one of the dumb classes that mirror the database tables and isn't supposed to know about how to retrieve anything.

Do you do as the answer to this question suggests and create a Dummy version of each object? Then the DAL level object can have variables indicating if Employees has or has not been loaded and call DALEmployee.GetEmployees(this)? I feel as if I'm missing something crucial about the pattern...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长发绾君心 2024-09-23 01:23:29

如果您使用预先构建的框架(例如 nHibernate),这将使一切变得更加容易,您可以在类/表映射中以及运行查询时定义延迟加载。尽管 .NET 4 中的 System.Lazy 类可能会有所帮助,但要以简洁的方式自己完成此操作将需要相当多的代码。

If you use a pre-built framework such as nHibernate this will make it all much easier, you can define the lazy-loading in the class/table mapping and when a query is run. To do it yourself in a neat manner is going to take a fair bit of code although the System.Lazy class in .NET 4 may help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文