0
votes

I have a class that inherits from List, this class has some additional properties on it. When I store the parent document in RavenDB, the list items get stored but the additional properties do not.

The failing test below probably explains my issue better:

[TestFixture]
public class RDBIssueTests
{
    private DocumentStore _documentStore;

    [TestFixtureSetUp]
    public void TestFixtureSetUp()
    {
        _documentStore = new EmbeddableDocumentStore
        {
            RunInMemory = true,
            UseEmbeddedHttpServer = true,
            DataDirectory = "Data",
        };
        _documentStore.Initialize();
    }

    [Test]
    public void StoreSimpleDataTest()
    {
        string id = "people/1";
        string laptopName = "MacPro";
        string personName = "Joe Bloggs";
        string attrOne = "Screen";
        string attrTwo = "Keyboard";

        var person = new Person()
        {
            Id = id,
            Name = personName,
            Laptop = new Possession<string>()
        };
        person.Laptop.Name = laptopName;
        person.Laptop.Add(attrOne);
        person.Laptop.Add(attrTwo);

        using (var session = _documentStore.OpenSession())
        {
            session.Store(person);
            session.SaveChanges();
        }

        using (var session = _documentStore.OpenSession())
        {
            var loadedPerson = session.Load<Person>(id);

            Assert.AreEqual(personName, loadedPerson.Name);
            Assert.AreEqual(2, loadedPerson.Laptop.Count);    // 2 items in the list
            Assert.IsTrue(loadedPerson.Laptop.Contains(attrOne));
            Assert.IsTrue(loadedPerson.Laptop.Contains(attrTwo));
            Assert.AreEqual(laptopName, loadedPerson.Laptop.Name);  // fails here - Person.Laptop.Name is not persisted in RBD
        }
    }
}

public class Person
{
    public string Id { get; set; }
    public string Name { get; set; }
    public Possession<string> Laptop { get; set; }
}

public class Possession<TValueType> : PossessionAttribute<TValueType>
{
    public string Name { get; set; }    // RDB doesn't persist this value
}

public class PossessionAttribute<TValueType> : List<TValueType>
{
}

As you can see from the test, the string property 'Name' on the Possession class does not get saved.

Is there something I need to do to get this property to persist?

Many thanks!

1

1 Answers

0
votes

JSON has no way of representing an object that is both a list and has properties. That is why you cannot do that. You can have an object that contains a list property, which is a more natural way of going about it.