对象关系映射中的多值属性(Android 应用程序)
在我正在构建的 Android 应用程序中,我将关系查询封装在模型层中。
我的一个对象(我们称之为 Place
)具有多值属性(我们称之为 Image
)。简化一下,我可以说 Java 中的模型如下所示:
class Place{
List<Image> images;
...
}
class Image {
String description;
String imageName;
...
}
在数据库中,这些类映射到表 PLACE 和 IMAGE。
在 Java 端,我使用 DAO 来查询、插入、更新和删除 Place
对象。
作为一个简化的示例,我想要一个名为 PlaceDataSource
的类,如下所示:
class PlaceDataSource {
public List<Place> findAll();
public void update(Place place);
public void add(Place place);
public void delete(Place place);
...
}
这些方法的描述如下:
findAll
方法回答 列表>Place
,每个位置都包含 Image
对象的集合。
update
方法更新 Place
及其图像(更新、插入新图像或根据需要删除现有图像)。
add
方法添加一个新的 Place
及其图像。
delete
方法删除 Place
及其所有图像。
findAll
、add
和 delete
方法非常容易实现。 但是,我还没有看到一种考虑多值属性的实现 update 方法的直接方法。我想到的程序算法是:
- 从数据库查询要更新的
Place
对象中当前保存的图像。 - 找出
Place
对象的images
属性中不再存在的Image
对象并将其删除。 - 更新
images
属性中存在的持久图像。 - 添加
images
属性中存在的新图像。
我认为这段代码会很丑陋且效率低下。 有没有更好的方法来实现 DAO? 在使用 DAO 的应用程序中,您是否更喜欢将每个 DAO 始终映射到一个物理表?
我考虑过在这种情况下使用两种不同的 DAO,一种用于持久保存 Place
对象,另一种用于 Image
对象。那么 Place
DAO 将不负责图像对象的持久性,而是“业务”层应该保持它们的一致性。 但是,从概念上来说,Image
是 Place
对象的一部分,因此这个解决方案也不能完全说服我。
非常感谢您的反馈!
注意:目前我对在 Android 上使用 ORM 框架不感兴趣,只是想从概念的角度找到这个问题的答案。
In an Android application I am building I am encapsulating the relational queries in a model layer.
One of my objects, let's call it Place
, has a multivalue property (let's call it Image
). Simplifying things I could say that the model in Java looks like:
class Place{
List<Image> images;
...
}
class Image {
String description;
String imageName;
...
}
In the database these classes are mapped to the tables PLACE and IMAGE.
In the Java side I am using a DAO to query, insert, update, and delete Place
objects.
As a simplified example, I would like to have a class called PlaceDataSource
that looks like:
class PlaceDataSource {
public List<Place> findAll();
public void update(Place place);
public void add(Place place);
public void delete(Place place);
...
}
The description of these methods are the following:
The findAll
method answers a list of Place
s, each of them containing a collection of Image
objects.
The update
method updates a Place
and its images (updating, inserting new images, or deleting existing images if necessary).
The add
method adds a new Place
with its images.
The delete
method deletes a Place
with all its images.
The findAll
, add
and delete
methods are very easy to implement.
However, I have not seen a straightforward way of implementing the update
method, that takes into consideration the multivalue property. The procedural algorithm that comes to my mind is:
- Querying from the database the currently persisted images from the
Place
object to update. - Find out which
Image
objects are not present anymore in theimages
property of thePlace
object and delete them. - Update the persisted images that exists in the
images
property. - Add new images present in the
images
property.
I think this code is going to be ugly and inefficient.
Is there a better way to implement the DAO ?
In your applications using DAOs, do you prefer to have each DAO mapped always to exactly one physical table ?
I have thought about using two different DAOs in this scenario, one for persisting Place
objects and another for Image
objects. Then the Place
DAO would not be responsible for the persistance of image objects, but rather the 'business' layer should keep them consistent.
However, Image
s are conceptually part of Place
objects, so this solution does not completely convince me neither.
Thanks a lot for your feedback !
Note: at the moment I am not interested in using an ORM framework for Android, just would like to find an answer to this problem from a conceptual point of view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是 ORMLite 的作者,它可以为您完成所有这些工作,但您可以通过多种方法自己完成此操作。
您可以编写自己的
List
类(Collection
会更容易)来实现您想要的方法:add
、remove 等。这些都会更改列表并通过 DAO 方法进行调用。对于示例代码,您可以看看 ORMLite 如何执行此操作
BaseForeignCollection
和EagerForeignCollection
但这对于您的需求来说可能有点过分了。正如您提到的,手动维护列表会更容易。也许可以在
PlaceDataSource
(看起来更像是一个 DAO 而不是DataSource
)上添加方法,这些方法会从列表和数据库中删除两者。例如:您必须确保不直接使用该列表,否则它将与数据库不同步。
您还可以在
Place
本身上使用这些方法,然后将每个Place
对象与其 DAO 一起注入。有些人喜欢这种模式,但我一直认为在另一个类中使用 DAO 方法会更干净。希望这有帮助。
I'm the author of ORMLite which does all of this for you but there are a couple of ways you can do this yourself.
You could write your own
List
class (Collection
would be easier) that implements the methods that you want:add
,remove
, etc.. These would both change the list and call through the DAO methods. For an example code, you could take a look at how ORMLite does thisBaseForeignCollection
andEagerForeignCollection
but it's probably overkill for your needs.Easier would be as you mention to maintain the list by hand. Maybe add methods on the
PlaceDataSource
(that seems like a DAO more than aDataSource
) that would remove both from the list and the database. Something like:You'll have to make sure that you do not use the list directly otherwise it will get out of sync with the database.
You could also have these methods on the
Place
itself and then have eachPlace
object be injected with its DAO. Some people like this pattern but I've always thought that having the DAO methods in another class was cleaner.Hope this helps.