Ayende @ Rahien

It's a girl

And sometimes Things Just Works

I am in the process of writing an article about RavenDB, and I just wrote the following code to demonstrate RavenDB schema less nature:

using (var session = documentStore.OpenSession())
{
    session.Store(new Customer
    {
        Name = "Joe Smith",
        Attributes =
            {
                {"IsAnnoyingCustomer", true},
                {"SatisfactionLevel", 8.7},
                {"LicensePlate", "B7D-12JA"}
            }
    });

    session.SaveChanges();
}

using (var session = documentStore.OpenSession())
{
    var customers = session.Query<Customer>()
        .Where(x => x.Attributes["IsAnnoyingCustomer"].Equals(true))
        .ToList();

    Console.WriteLine(customers.Count);

    session.SaveChanges();
}

This worked, flawlessly.

The amount of work that we have put into RavenDB to make such things work is really scary when you sit down to think about it.

But it works, it does what I expect it to do and it doesn’t get in my way, woohoo!

Tags:

Posted By: Ayende Rahien

Published at

Originally posted at

Comments

Valeriu Caraulean
05/01/2012 09:38 AM by
Valeriu Caraulean

Are calls to session.SaveChanges(); mandatory here? (I don't know much about RavenDB)

Won't be changes saved when session is disposed?

Matt Warren
05/01/2012 09:49 AM by
Matt Warren

@Valeriu

No in RavenDB you have to explicitly call .SaveChange(). It triggers a batch upload of docs and commits a transaction, so having it as an explicit call is better.

configurator
05/01/2012 10:26 AM by
configurator

Do you need that .Equals(true) there for some reason or did you put that in just to be explicit?

Ayende Rahien
05/01/2012 10:51 AM by
Ayende Rahien

Valeriu, We don't save unless you call to SaveChanges(). If you don't need to save, you don't need to call SaveChanges

Ayende Rahien
05/01/2012 10:52 AM by
Ayende Rahien

Configurator, We put the .Equals(true) there because C# would generate a compiler warning about a == between the types.

Henrik Stenbæk
05/01/2012 10:58 AM by
Henrik Stenbæk

What is the second call to session.SaveChanges(); good for?

Ayende Rahien
05/01/2012 11:00 AM by
Ayende Rahien

Henrik, Nothing, I added it out of habit.

Nathan King
05/01/2012 11:45 AM by
Nathan King

In the Customer class, what is the C# type of the Attiributes property defined as?

Richard Wilde
05/01/2012 12:44 PM by
Richard Wilde

Does "Joe Smith" come back in the results count? I remember something about it will only be there after it has been indexed and that could take several to several hundred milli-secs

Bob
05/01/2012 02:01 PM by
Bob

Henrik, Nothing, I added it out of habit.

If there's one thing I always enjoy, it's a confusing 3 line program for a beginners tutorial.

while (;;) { }

Just about as useful as "added it out of habit."

Ayende Rahien
05/01/2012 03:59 PM by
Ayende Rahien

Nathan, Dictionary of string & object

Nick
05/01/2012 09:11 PM by
Nick

What does the "satisfaction level" refer to lol?

Stuart
05/02/2012 05:47 AM by
Stuart

Out of interest, with the second session.SaveChanges(); - would that actually fire off any thing to RavenDb, or would it know there are no changes, and so do nothing?

Mike
05/02/2012 06:02 PM by
Mike

So RavenDB is designed to enable magic strings, eh? :)

Comments have been closed on this topic.