数据映射器和关系:实现策略? [英] Data Mapper and Relationships: Implementation strategies?

查看:120
本文介绍了数据映射器和关系:实现策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了我的数据映射器,但现在我已经到了关系的位置。



我将尝试在这里说明我的想法。我没有找到关于这个话题的好文章/信息,所以也许我会重新发明这个轮子(当然我可以使用一个大的框架,但是我想通过这样做来学习)。 / p>

1:1关系



首先,看看1:1的关系。一般来说,当我们有一个名为公司的域名和一个名为地址的域名时,我们的公司类将具有类似于address_id的类。让我们说在大多数情况下,我们只是显示一个公司列表,只有当有人看到细节时,才需要这个地址。在这种情况下,我的Data Mapper(CompanyDataMapper)只是简单地加载,这意味着它将从数据库中获取该address_id,但是也不会加入来获取地址数据。


$ b $一般来说,我对每个关系都有一个getter方法。所以在这种情况下,有一个getAddress(Company companyObject)方法。它需要一个公司对象,寻找它的地址属性,如果为NULL,则从数据库中获取相应的Address对象,使用该Address对象(AddressDataMapper)的Mapper类,并将该地址对象分配给指定的地址属性公司对象。



重要提示:数据映射器是否允许使用另一个数据映射器?



在大多数情况下,您需要同时使用公司对象和地址对象,因为您始终将其显示在列表中。在这种情况下,CompanyDataMapper不仅获取公司对象,而且还使用JOIN进行SQL查询,以获取地址对象的所有字段。最后,它迭代记录集,并将新对象与其对应的值进行比较,将地址对象分配给公司对象。



听起来很简单,到目前为止。 >

1:n关系



这些如何?与1:1的唯一区别是,公司可能有多个Address对象。让我们来看看:当我们大部分时间只对本公司感兴趣时,数据映射器只会将公司对象的地址属性设置为NULL。 addresses属性是可以引用none,一个或多个地址的数组。但是我们还不知道,因为我们加载懒惰,所以它只是NULL。但是,在大多数情况下,如果我们需要所有的地址呢?如果我们将显示所有公司的所有地址的大名单?在这种情况下,事情开始变得很丑。首先,我们不能为每个地址对象加入地址表五十次(我坚信这是不可能的,如果是,性能将低于零)。所以,当我们进一步认为这一点,在这种情况下不可能懒洋洋地加载。



重要提示:这是真的吗?必须发出100个查询才能获得100个地址对象,如果我有10个公司,每10个地址?



m:n关系



让我们说一个地址对象只包含国家,州,城市,道路和房屋号码。但是,一个房子可能是一个大型商业大厦,其中有很多公司。像那些现代办公楼之一,任何人都可以租一个小型的rom来在其网站上炫耀这座塔楼。所以:许多公司可以共享相同的地址。



我还没有计划处理这种问题。



重要提示:这可能不是比1:n关系更大的问题吗?



如果有人知道一个关于解决/实现这个问题的详细资料,我会对一个链接感到高兴!

解决方案

我期待着您能够获得有关此主题的任何答案,但在此期间,为什么不跳过亚马逊(或您当地的图书经销商),最后购买





这些书包含您在各种问题中指出的原始模式,并被认为是设计模式和软件架构的参考书。 p>

I've almost finished my Data Mapper, but now I'm at the point where it comes to relationships.

I will try to illustrate my ideas here. I wasn't able to find good articles / informations on this topic, so maybe I'm re-inventing the wheel (for sure I am, I could just use a big framework - but I want to learn by doing it).

1:1 Relationships

First, lets look at 1:1 relationships. In general, when we've got an domain class called "Company" and one called "Address", our Company class will have something like address_id. Lets say in most cases we just display a list of Companies, and the address is only needed when someone looks at the details. In that case, my Data Mapper (CompanyDataMapper) simply loads lazily, meaning it will just fetch that address_id from the database, but will not do a join to get the address data as well.

In general, I have an getter method for every Relationship. So in this case, there's an getAddress(Company companyObject) method. It takes an company object, looks for it's address property and - if it's NULL - fetches the corresponding Address object from the database, using the Mapper class for that Address object (AddressDataMapper), and assigns that address object to the address property of the specified company object.

Important: Is a Data Mapper allowed to use another Data Mapper?

Lets say in most cases you need both the company object AND the address object, because you always display it in a list all together. In this case, the CompanyDataMapper not only fetches company objects, but does an SQL query with JOIN to also get all the fields of the address object. Finally, it iterates over the record set and feeds new objects with their corresponding values, assigning the address object to the company object.

Sounds simple, so far.

1:n Relationships

How about these? The only difference to 1:1 is, that an Company may have multiple Address objects. Lets have a look: When we're most of the time only interested in the Company, the Data Mapper would just set the addresses property of the company object to NULL. The addresses property is an array which may reference none, one or multiple addresses. But we don't know yet, since we load lazily, so it's just NULL. But what, if we would need all the addresses in most cases as well? If we would display a big list with all companys together with all their addresses? In this case, things start to get really ugly. First, we can't join the address table fifty times for every address object (I strongly believe that's impossible, and if it is, performance would be below zero). So, when we think this further down the road, it's impossible to NOT load lazily in this case.

Important: Is this true? Must I send out 100 queries to get 100 address objects, if I have 10 companies with each 10 addresses?

m:n Relationships

Lets say an address object only contains the country, state, city, road and house number. But one house could be a big business tower with lots of companies in them. Like one of those modern office buildings where anyone can rent a small rom to show off that tower on its website. So: Many companies can share the same address.

I have no plans yet to deal with that kind of problem.

Important: Probably it's not a bigger problem than the 1:n Relationships?

If anyone knows a good ressource that goes into details about solving / implementing this, I would be happy about a link!

解决方案

I am looking forward to any answers you'll get on this topic, but in the meantime why not just hop over to Amazon (or your local books dealer) and finally buy

These book contain the original patterns you have been pointed at in various of your questions and are considered reference works in Design Patterns and Software Architecture.

这篇关于数据映射器和关系:实现策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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