Puzzling Duplicates

12 views
Skip to first unread message

Wade Wright

unread,
Dec 2, 2016, 12:59:34 PM12/2/16
to rav...@googlegroups.com
I am trying to replicate an issue we are seeing in our application where basically a doing a load or query returns an object with a duplication of the items in a list when it wasn't persisted that way. 

Here is a simple test that demonstrates one of the ways where I can demonstrate this: 


    public class Foo 
    {
        public Foo()
        {
            Id = Guid.NewGuid().ToString();
            Bars = new List<Bar> { new Bar { Name = "Bar1" }, new Bar { Name = "Bar2" } };
        }

        public string Id { get; set; }
        public string Name { get; set; }
        public List<Bar> Bars { get; set; }
    }

    
    public class Bar
    {

        public Bar()
        {
            Id = Guid.NewGuid().ToString();
        }
        public string Id { get; set; }
        public string Name { get; set; }
    }

    public class MysteriousDupes 
    {

        [Fact]
        private void WTF()
        {
            var ds  = new EmbeddableDocumentStore { RunInMemory = true };
            ds.Initialize();

            string id;
            var originalFoo = new Foo {Name = "Original Foo"};
            Foo loadedFoo = null;

            //store original
            using (var rs = ds.OpenSession())
            {
                id = originalFoo.Id;
                rs.Store(originalFoo); 
                rs.SaveChanges();               
            }

            //load original
            using (var rs = ds.OpenSession())
            {
                loadedFoo = rs.Load<Foo>(id);
            }

            //should have the same number of bars as when we stored it
            Assert.Equal(loadedFoo.Bars.Count, originalFoo.Bars.Count);
           
        }
    }




Now we don't populate lists in constructors so this isn't exactly my issue but it is very similar and is easy to demonstrate in a test. 

Lastly, this is the latest production 3.51


--
W

Oren Eini (Ayende Rahien)

unread,
Dec 2, 2016, 3:21:33 PM12/2/16
to ravendb
When we deserialize the object, we call the ctor, then we _add_ the items to the property. 
That is why you have duplicates.
You can control that with:

[JsonProperty(ObjectCreationHandling=ObjectCreationHandling.Replace)]
        public List<Bar> Bars { get; set; }


Hibernating Rhinos Ltd  

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

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

 


--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wade Wright

unread,
Dec 2, 2016, 3:33:36 PM12/2/16
to rav...@googlegroups.com
Thank you sir. 
--
W
Reply all
Reply to author
Forward
0 new messages