简体   繁体   English

用子类的实例替换db4o存储的对象

[英]Replacing a db4o stored object with an instance of a subclass

I want to change all objects that match a condition to use a specific/new subclass. 我想更改所有符合条件的对象以使用特定/新的子类。

There are no other subclasses being used with the stored objects, so its in all cases a change from the base class to its subclass. 存储的对象没有使用其他子类,因此,在所有情况下,它都从基类变为其子类。

In order to keep all references pointing to these objects, I tried using bind to replace them with new instances of the subclass (copying all the data), but it seems bind its restricted only to items of the same specific class. 为了使所有引用都指向这些对象,我尝试使用bind将其替换为子类的新实例(复制所有数据),但似乎仅将其绑定到同一特定类的项目。

Is there any way to do this that doesn't require adding code to explicitly update every reference that points to these objects? 有什么方法不需要添加代码来显式更新指向这些对象的每个引用吗? A single call that replaces all references from one object to another would be great. 一次调用将所有引用从一个对象替换为另一个对象将是很好的。

Good question. 好问题。 As far as I know this isn't possible =(. The only way is to copy the data over to the new type. 据我所知这是不可能=(。唯一的方法是将数据复制到新类型。

As you said, when you copy the data over to the new type, you have the issue that there are lots of references which point to the old instance instead of the new instance. 如您所说,将数据复制到新类型时,您会遇到一个问题,即有很多引用指向旧实例而不是新实例。 And bind indeed checks the type. 并绑定确实检查类型。

This leaves no option but coping and updating all references, which is of course a tedious process. 这样就只能处理和更新所有参考,这当然是一个乏味的过程。 =( So query for all object you need to change the type. Copy the data to the new object. Then query for all object which reference to the old object and replace the reference. =(因此查询所有需要更改类型的对象。将数据复制到新对象。然后查询所有引用旧对象的对象并替换引用。

IObjectContainer container = ... //
foreach(var oldObject in container.Query<MyType>())
{
     NewSubType newCopy = copyToSubType(oldObject); // copy the data

     var referencesFromTypeA = from TypeA a in container
                               where a.ReferenceToMyType == oldObject
                               select a
     // repeat this for all types which can refer to the object which are copied
     // it can certainly be generified
     foreach(var referenceToUpdate in referencesFromTypeA)
     {
            referenceToUpdate.ReferenceToMyType=newCopy;
            container.Store(referenceToUpdate);
     }
     container.Store(newCopy);
     container.Delete(oldObject);
}

Don't forget to replace the referenced in collections and array. 不要忘记替换集合和数组中引用的内容。

Btw. 顺便说一句。 I think I've some code around which analyses the types and finds properties which refer to another type and find the object for it. 我想我周围有一些代码可以分析类型并找到引用另一种类型的属性并为其找到对象。 If it helps? 如果有帮助吗?

Now to another potential way to do it, if your up for adventures: Change the db4o code a little. 现在,如果您喜欢冒险,可以尝试另一种可能的方法:稍微更改db4o代码。 Because you want to change the objects to a subtype, it should be safe to 'Bind' it to the new object. 由于要将对象更改为子类型,因此将其“绑定”到新对象应该是安全的。 So if the existing object suddenly point to the new subtype, it still should work. 因此,如果现有对象突然指向新的子类型,则它仍然应该起作用。 So what you could do it to remove the check in the Bind-Method-implementation and try to run it. 因此,您可以执行以下操作来删除Bind-Method-implementation中的检查并尝试运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM