来自关系数据库背景,我应该如何在 db4o(或任何对象数据库)中建模关系?

发布于 2024-09-13 08:59:51 字数 1987 浏览 1 评论 0原文

我正在尝试使用 db4o 作为数据存储,因此为了掌握它,我想我应该为自己构建一个简单的问题跟踪 Web 应用程序(在 ASP.NET MVC 中)。我发现 db4o 在快速开发方面非常出色,尤其是对于像这样的小型应用程序,而且它还不需要 ORM。

然而,由于具有 SQL Server/MySQL 背景,我有点不确定在涉及关系时应该如何构建对象(或者也许我只是没有正确理解对象数据库的工作方式)。

这是我的简单示例:我只有两个模型类:Issue 和 Person。

public class Issue
{
    public string ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime? SubmittedOn { get; set; }
    public DateTime? ResolvedOn { get; set; }
    public Person AssignedBy { get; set; }
    public Person AssignedTo { get; set; }
}

public class Person
{
    public string ID { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
}

ID 属性只是 .NET Guid.NewGuid() 帮助程序生成的 GUID 字符串。

这就是我最初认为该应用程序的工作方式;请忽略任何安全问题等,并假设我们已经在数据库中存储了一些 Person 对象:

  1. 用户登录。在数据库中查询与用户名和密码匹配的 Person ,并将他/她的 GUID id 存储为会话变量。重定向到应用程序主屏幕。
  2. 登录的用户创建一个新的问题单,从下拉列表中选择要分配给它的用户。他们填写其他详细信息(标题、描述等),然后提交表格。
  3. 查询数据库中的 Person 对象(通过其 GUID ID)以获取代表登录用户的对象和代表已分配票证的用户的对象。创建一个新的 Person 对象(用发布的表单数据填充),将 Person 对象分配给 Issue 对象的 AssignedByAssignedTo 属性,并存储它。

这意味着我针对每个 Issue 记录存储了两个 Person 对象。但是,如果我更新原始 Person 会发生什么 - 各个问题对象中存储的所有对该 Person 的引用是否都会更新,还是我必须手动处理? 它们是参考资料还是副本?

只存储分配者和分配者字段的 GUID 字符串(如下所示),然后每次根据该字符串查找原始人,这样会更好/更高效吗?

public class Issue
{
    public string ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime? SubmittedOn { get; set; }
    public DateTime? ResolvedOn { get; set; }
    public string AssignedByID { get; set; }
    public string AssignedToID { get; set; }
}

我想我只是陷入了某种让我困惑的思维方式。如果有人可以清楚地解释它,那将是最有帮助的!

I'm experimenting with db4o as a data store, so to get to grips with it I thought I'd build myself a simple issue tracking web application (in ASP.NET MVC). I've found db4o to be excellent in terms of rapid development, especially for small apps like this, and it also negates the need for an ORM.

However, having come from a SQL Server/MySQL background I'm a little unsure of how I should be structuring my objects when it comes to relationships (or perhaps I just don't properly understand the way object databases work).

Here's my simple example: I have just two model classes, Issue and Person.

public class Issue
{
    public string ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime? SubmittedOn { get; set; }
    public DateTime? ResolvedOn { get; set; }
    public Person AssignedBy { get; set; }
    public Person AssignedTo { get; set; }
}

public class Person
{
    public string ID { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
}

The ID properties are just GUID strings generated by the .NET Guid.NewGuid() helper.

So here's how I initially thought the application would work; please ignore any security concerns etc and assume we already have a few Person objects stored in the database:

  1. User logs in. Query the database for the Person which matches the username and password, and store his/her GUID id as a session variable. Redirect to app home screen.
  2. Logged in user creates a new issue ticket, selecting the user to assign it to from a drop-down list. They fill in the other details (Title, Description etc), and then submit the form.
  3. Query the Person objects in the database (by their GUID ID's) to get an object representing the logged in user and one representing the user the ticket has been assigned to. Create a new Person object (populated with the posted form data), assign the Person objects to the Issue object's AssignedBy and AssignedTo properties, and store it.

This would mean I have two Person objects stored against each Issue record. But what happens if I update the original Person—do all the stored references to that Person in the various issue objects update, or do I have to handle that manually? Are they references, or copies?

Would it be better/more efficient to just store a GUID string for the AssignedBy and AssignedTo fields (as below) and then look up the original person based on that each time?

public class Issue
{
    public string ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime? SubmittedOn { get; set; }
    public DateTime? ResolvedOn { get; set; }
    public string AssignedByID { get; set; }
    public string AssignedToID { get; set; }
}

I think I'm just stuck in a certain way of thinking which is confusing me. If someone could explain it clearly that would be most helpful!

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

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

发布评论

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

评论(2

弃爱 2024-09-20 08:59:51

对象数据库尝试提供与内存中对象相同的语义。经验法则是:它的工作方式就像内存中的对象一样。对象数据库存储数据库中对象之间的引用。当您更新对象时,该对象也会更新。如果您有对该对象的引用,您会看到更改后的版本。

在您的情况下,问题对象指的是人员对象。当您更新该人时,涉及该人的所有问题都会“看到”该更新。

当然,像 int、string、long 等基本类型会像值对象一样处理,而不是引用对象。此外,数组的处理方式与 db4o 中的值对象类似,这意味着数组与对象一起存储,而不是作为引用存储。其他所有内容都存储为参考,甚至是列表或字典等集合。

Object-Databases try to provide the same semantics as objects in memory. The rule of thumb is: It works like objects in memory. Object databases store references between the objects in the database. When you update the object, that object is updates. And if you have a reference to that objects, you see the changed version.

In your case, the Issue-objects refer to the person object. When you update that person, all Issues which refer to it 'see' that update.

Of course, primitive types like int, strings, longs etc are handled like value objects and not a reference objects. Also arrays are handled like value objects in db4o, this means a array is stored together with the object and not as a reference. Everything else is stored as a reference, even collections like List or Dictionaries.

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