Db4O - 我可以保存字符串吗?

发布于 2024-08-08 07:19:48 字数 535 浏览 4 评论 0原文

我有以下代码:

 Assert.IsTrue(Repository.FindAll<string>().Count() == 0);
 string newString = "New String";
 Repository.Save(newString);
 Assert.IsTrue(Repository.FindAll<string>().Count() == 1);

但它失败了。我想这与我保存字符串有关。

我的 Save() 代码是这样的:

  public void Save<T>(T obj)
  {
     if (obj == null)
        throw new ArgumentNullException("obj not allowed to be null");

     Db.Store(obj);
     Db.Commit();
  }

我的持久类应该有一些特别的东西吗?或者我可以用 db4o 保存几乎所有东西?

I have the following code:

 Assert.IsTrue(Repository.FindAll<string>().Count() == 0);
 string newString = "New String";
 Repository.Save(newString);
 Assert.IsTrue(Repository.FindAll<string>().Count() == 1);

But it is failing. I suppose it has something to do with the fact that I'm saving a string.

My Save() code is this:

  public void Save<T>(T obj)
  {
     if (obj == null)
        throw new ArgumentNullException("obj not allowed to be null");

     Db.Store(obj);
     Db.Commit();
  }

Should my persistent classes have something special? Or I can just save pretty much anything with db4o?

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

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

发布评论

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

评论(1

享受孤独 2024-08-15 07:19:48

据我所知,平台基元,即使它们可能是引用类型(如 System.String),也只有在实例是另一个对象的子对象(即在字段中引用)时才会被存储,然后该对象才会被存储。因此,在 C# 或 Java 中对原语调用 Store() 不会保存它。基本上,您需要创建一个实体来引用您的字符串才能保存它们;与您需要创建表以将字符串保存到 RDBMS 的方式大致相同。

public class StringEntity
{
   private readonly string value;

   public StringEntity(string value)
      : base()
   {
      this.value = value;
   }
}

...

Assert.IsTrue(Repository.FindAll<StringEntity>().Count() == 0);
Repository.Save(new StringEntity("New String"));
Assert.IsTrue(Repository.FindAll<StringEntity>().Count() == 1);

编辑:
或者更好

public class Primitive<T>
{
   private readonly T value;

   public Primitive(T value)
      : base()
   {
      this.value = value;
   }
}

...

Assert.IsTrue(Repository.FindAll<Primitive<string>>().Count() == 0);
Repository.Save(new Primitive<string>("New String"));
Assert.IsTrue(Repository.FindAll<Primitive<string>>().Count() == 1);

As far as I know, platform primitives, even though they may be reference types (like System.String), will only be stored if an instance is a child of another object (i.e. referenced in a field) and then that object is stored. Therefore, calling Store() on a primitive in C# or Java will not save it. Basically, you'll need to create an entity to reference your strings in order to save them; much the same way as you'd need to create a table in order to save strings to an RDBMS.

public class StringEntity
{
   private readonly string value;

   public StringEntity(string value)
      : base()
   {
      this.value = value;
   }
}

...

Assert.IsTrue(Repository.FindAll<StringEntity>().Count() == 0);
Repository.Save(new StringEntity("New String"));
Assert.IsTrue(Repository.FindAll<StringEntity>().Count() == 1);

EDIT:
or better yet

public class Primitive<T>
{
   private readonly T value;

   public Primitive(T value)
      : base()
   {
      this.value = value;
   }
}

...

Assert.IsTrue(Repository.FindAll<Primitive<string>>().Count() == 0);
Repository.Save(new Primitive<string>("New String"));
Assert.IsTrue(Repository.FindAll<Primitive<string>>().Count() == 1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文