difficulty cloning object without saving changes to it.

29 skatījumi
Pāriet uz pirmo nelasīto ziņojumu

Stacey

nelasīta,
2016. gada 16. janv. 17:50:2716.01.16
uz RavenDB - 2nd generation document database
I have a situation where I need to copy an object from the database into another object, and then make changes to the copied version. But I am ending up with a problem, because it persists changes back to the original.

This is the method I am using to make a copy;

/// <summary>
/// Perform a deep Copy of the object, using Json as a serialisation method.
/// </summary>
/// <typeparam name="T">The type of object being copied.</typeparam>
/// <param name="source">The object instance to copy.</param>
/// <returns>The copied object.</returns>
public static T Clone<T>(this T source) {
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source, null)) {
return default(T);
}

// initialize inner objects individually
// for example in default constructor some list property initialized with some values,
// but in 'source' these items are cleaned -
// without ObjectCreationHandling.Replace default constructor values will be added to result
var deserializeSettings = new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace };

return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source), deserializeSettings);
}

And then I'll use it like this;

var clone = aspect.Clone<Aspect>();
    clone.Name = "New Name";

When I run this and go to the studio UI, some of the objects in the collection yield errors when I try to open them. If I do not have the copy, this does not occur.

Stacey

nelasīta,
2016. gada 16. janv. 17:52:5216.01.16
uz RavenDB - 2nd generation document database
Sorry, I forgot to add - when I clone the object and make changes, the changes do not persist back to the original, but I think something changes because the original seems to vanish from the database, but there is like a "ghost copy" left behind of it, that I cannot open. I'm at a complete loss as to what is happening, this method is the only thing I can trace the behavior back to.

Federico Lois

nelasīta,
2016. gada 17. janv. 07:00:4517.01.16
uz rav...@googlegroups.com
Use evict. In that way you are disconnecting the original object from the tracking of the session and treat it a new one.

From: Stacey
Sent: ‎16/‎01/‎2016 19:52
To: RavenDB - 2nd generation document database
Subject: [RavenDB] Re: difficulty cloning object without saving changes to it.

Sorry, I forgot to add - when I clone the object and make changes, the changes do not persist back to the original, but I think something changes because the original seems to vanish from the database, but there is like a "ghost copy" left behind of it, that I cannot open. I'm at a complete loss as to what is happening, this method is the only thing I can trace the behavior back to.

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ziņojums ir izdzēsts

Oren Eini (Ayende Rahien)

nelasīta,
2016. gada 2. febr. 07:05:3902.02.16
uz ravendb
session.Advanced.Evict() is a method that will cause the session to forget about your object

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


On Tue, Feb 2, 2016 at 1:18 PM, Stacey <stacey.ci...@gmail.com> wrote:
I'm not clear what "evict" is. Is this a method or something in Raven?
Atbildēt visiem
Atbildēt autoram
Pārsūtīt
0 jauni ziņojumi