Raven ObjectTree question

50 views
Skip to first unread message

Jay Janarthanan

unread,
Aug 28, 2014, 6:19:58 PM8/28/14
to rav...@googlegroups.com

Why is that when I change a property value on Product, it does not change everywhere it is referenced? Sample code below. On Step 3 I will assume that the Product.Name would have changed as the ID is same 

Jay



public void Step1()
{
    var documentStore = new DocumentStore { Url = "http://localhost:9999/", DefaultDatabase = "demo" };
    documentStore.Initialize();
 
    using (IDocumentSession session = documentStore.OpenSession())
    {
        var customer = new Customer() { Name = "Jay" };
        session.Store(customer);
 
        var order = new Order() { Customer = customer };
 
        var product1 = new Product() { Name = "Product1" };
        session.Store(product1);
        var product2 = new Product() { Name = "Product2" };
        session.Store(product2);
 
        order.ProductIdList.Add(product1);
        order.ProductIdList.Add(product2);
 
        session.Store(order);
        session.SaveChanges();
 
        _orderId = order.Id;
        _productId = product2.Id;
    }
}
 
public void Step2()
{
 
    var documentStore2 = new DocumentStore { Url = "http://localhost:9999/", DefaultDatabase = "demo" };
    documentStore2.Initialize();
    using (IDocumentSession session = documentStore2.OpenSession())
    {
        var product = session.Load<Product>(_productId);
        Console.WriteLine(product.Id + " " + product.Name);
        product.Name = "Changed";
 
        session.Store(product);
        session.SaveChanges();
    }
}
 
public void Step3()
{
 
    var documentStore3 = new DocumentStore { Url = "http://localhost:9999/", DefaultDatabase = "demo" };
    documentStore3.Initialize();
    using (IDocumentSession session = documentStore3.OpenSession())
    {
        var existingOrder = session.Load<Order>(_orderId);
 
        foreach (var product in existingOrder.ProductIdList)
        {
            Console.WriteLine(product.Id + " " + product.Name);
        }
 
    }
}

Oren Eini (Ayende Rahien)

unread,
Aug 28, 2014, 8:15:53 PM8/28/14
to ravendb
That is by design, you need to read the docs about this, see: http://ravendb.net/docs/2.5/theory/document-structure-design

Basically, the issue is that you are embedded the document, not referencing it.



Oren Eini

CEO


Mobile: + 972-52-548-6969

Office:  + 972-4-622-7811

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

Jay Janarthanan

unread,
Aug 29, 2014, 9:10:57 AM8/29/14
to rav...@googlegroups.com
Thanks Oren. I see the needed part

"The reasoning behind this is simple: we want to make it just a tad harder to reference data in other documents. It is very common when using an OR/M to do something like: orderLine.Product.Name, which will load the Product entity. That makes sense when you are living in a relational world, but Raven is not relational. This deliberate omission from the Raven Client API is intended to remind users that they should model their Aggregates and Entities in a format that follows the recommended practice for Raven."

So in the above example, if I want to change the name of the product I have to go through all the documents and change the product name ? Is their an easy way to query all the Product objects that are stand alone and the one inside Orders document ?

Thanks

Jay

Jay Janarthanan

unread,
Aug 29, 2014, 9:58:00 AM8/29/14
to rav...@googlegroups.com
My confusion is how do I reference an object

Order.product = new Product(

or Order.ProductID = product.id ?

Then when I query I get the product ID and then query the product ?

Jay


On Thursday, August 28, 2014 8:15:53 PM UTC-4, Oren Eini wrote:

Kijana Woodard

unread,
Aug 29, 2014, 11:50:18 AM8/29/14
to rav...@googlegroups.com
Since it's an Order, you probably want to copy the Product Name onto the order. 

"As a buyer, when I ordered this product, it was called Xyz. When I look at my order history 1 year from now, I don't see Abc because that would confuse me and cause me to contact support."

In the general case, you can use a Transformer to grab the current product name at display time.

Jay Janarthanan

unread,
Aug 29, 2014, 12:05:32 PM8/29/14
to rav...@googlegroups.com
I agree the use case is wrong, I am just using this as an example. 

Will look at transformers now

Jay

Oren Eini (Ayende Rahien)

unread,
Aug 29, 2014, 12:15:13 PM8/29/14
to ravendb
Why would you want to do that?
If I bought a Widget A, if its name was changed later, did I buy something else?
Reply all
Reply to author
Forward
0 new messages