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!
Comments
Are calls to session.SaveChanges(); mandatory here? (I don't know much about RavenDB)
Won't be changes saved when session is disposed?
@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.
Do you need that .Equals(true) there for some reason or did you put that in just to be explicit?
Valeriu, We don't save unless you call to SaveChanges(). If you don't need to save, you don't need to call SaveChanges
Configurator, We put the .Equals(true) there because C# would generate a compiler warning about a == between the types.
What is the second call to session.SaveChanges(); good for?
Henrik, Nothing, I added it out of habit.
In the Customer class, what is the C# type of the Attiributes property defined as?
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
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."
Nathan, Dictionary of string & object
What does the "satisfaction level" refer to lol?
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?
Stuart, It is a noop
So RavenDB is designed to enable magic strings, eh? :)
Comment preview