db4o、Linq 和 UUID

发布于 2024-08-19 14:18:12 字数 748 浏览 2 评论 0 原文

显然 db4o 网站最近重做了,现在旧的 url 出现 404 错误。每当我认为我已经找到答案时,我就会收到 404 错误。

我有一个简单的 db4o 数据库,用于存储人员。

public class Person
{
     public string Firstname { get; set;}
     public string Lastname {get;set;}
}

我已经能够在数据库上运行 Linq 查询,并且一切都运行良好。我在 Web 环境中编程,因此需要有人从数据库中识别和获取唯一对象,因此我只需将数据库配置为使用 UUID。现在问题变成了,如何从Linq查询得到的对象中获取UUID信息呢?

例如,假设我获取了数据库中存储的所有人员,并且需要为每个人生成唯一的 URL。我将使用 UUID 来执行此操作。所以我将运行这个:

var people = (from Person p in db select p).ToList();

然后我将迭代列表

foreach( Person person in people)
{
  DoSomething(person);
}

最好的解决方案是将 UUID 作为 Person 类的属性,这样我就可以:

var uuid = person.UUID;

但是,我没有看到执行此操作的机制。我错过了什么吗?

Apparently the db4o website was recently redone, and now old urls are giving 404 errors. Everytime I think I've found the answer, I get a 404 error.

I have a simple db4o database I've setup to store people.

public class Person
{
     public string Firstname { get; set;}
     public string Lastname {get;set;}
}

I've been able to run Linq queries on the database, and everything works wonderfully. I'm programming in a web environment, so I will need someone to identify and fetch unique objects from the database, so I simply configured the database to use UUID's. Now the problem has become, how can I get to the UUID information from the objects I get from the Linq query?

For example, let's say I get all the people stored in the database, and I need to generate the unique URL's for each person. I will use the UUID to do so. So I'll run this:

var people = (from Person p in db select p).ToList();

And then I'll iterate through the list

foreach( Person person in people)
{
  DoSomething(person);
}

The best solution would be making the UUID a property on the Person class, so that I could just:

var uuid = person.UUID;

I don't see a mechanism for doing that however. Am I missing something?

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

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

发布评论

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

评论(2

萌梦深 2024-08-26 14:18:12

我已经在此处回答了这个问题,但与其他问题一起回答了。所以我再次回答:
在 db4o 中通常没有 id。 db4o 使用 对象标识 来区分对象。因此,内存中的同一个对象对于数据库来说也是同一个对象。

只要你不序列化对象,它就可以正常工作,你不需要任何 ID。然而,一旦对象被序列化/断开连接,这就不再起作用(典型的网络应用程序)。

我认为这三个选项都是可能的:

当然,您可以创建一个基类,为上述任何实现提供 id 属性。

I've answered this already here but together with other question. Therefore I answer it again:
In db4o you have normally no id. db4o uses the object-identity to distinguish the object apart. So the same object in memory is going to be the same object for the database.

As long a you don't serialize object this works fine, you don't need any ID. However as soon as objects are serialized / disconnected this doesn't work anymore (Typical for web-apps).

I think this three options are possible:

  • Use the db4o internal id. You can get this id with IObjectContainer.Ext().GetID(object). And of course you can get a object by id: IObjectContainer.Ext().GetByID(id). However this id isn't forever. Defragmenting the database changes this id.
  • Using db4o's UUIDs. But db4o UUIDs are quite large
  • Creating ids by yourself. For example using a GUID. Or using a clever id-generator. Remember to index such a id-field when you use it in queries.

Of course you can create a base-class which provides the id-property with any of the implementations above.

画离情绘悲伤 2024-08-26 14:18:12

好的,这就是我快速制作的原型,而且看起来很有效。首先,我为所有数据对象创建了一个基类。现在,它只有一个 int Id 字段,尽管我可能也会将它用于其他一些事情。

然后我在 IObjectContainer 上创建了一个扩展方法。

public static class db4oExtensions
{
    public static void StoreWithId(this Db4objects.Db4o.IObjectContainer db, DataObject dao)
    {
        int count = (from DataObject datao in db select datao.Id).Max();
        dao.Id = count + 1;
        db.Store(dao);
    }
}

因此,现在当我在数据库中存储对象时,我将只调用 StoreWithId 而不是 Store。一些测试似乎表明这是有效的。现在我看到的一个问题是,如果我删除具有最大值的对象,我将拥有两个具有相同 id 的对象,但它们不会同时存在。稍后我会找出答案,可能只是在数据库本身中存储一个只能通过 StoreWithId 访问的 int 增量器。

Okay, here's what I've prototyped quickly, and it seems to be working. First, I created a base class for all my data objects. Right now, all it has is an int Id field, though I will probably use it for some other things as well.

Then I created an extension method on IObjectContainer.

public static class db4oExtensions
{
    public static void StoreWithId(this Db4objects.Db4o.IObjectContainer db, DataObject dao)
    {
        int count = (from DataObject datao in db select datao.Id).Max();
        dao.Id = count + 1;
        db.Store(dao);
    }
}

So now when I store an object in the db, I'll just call StoreWithId instead of Store. A few tests seem to show this is working. Now one problem I see is if I delete the object with the max value, I'll have two objects with identical id's, but which both don't exist at the same time. I'll figure out the answer to that later, possibly by just storing an int incrementer in the database itself that's only ever accessed through StoreWithId.

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