Created
June 29, 2011 20:18
-
-
Save gamlerhart/1054829 to your computer and use it in GitHub Desktop.
RavenDB-Documents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var order = session.Query<Order>() | |
.Customize(x => x.Include<Order>(o=>o.CustomerId)) // Load also the costumer | |
.First(); | |
var customer = session.Load<Customer>(order.CustomerId); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var customer = new Customer("Gamlor"); | |
session.Store(customer); | |
// After storing we have a valid id | |
var firstOrder = new Order() | |
{ | |
CustomerId = costumer.Id | |
}; | |
firstOrder.AddToOrder(new OrderItem("Magic Unicorn")); | |
session.Store(firstOrder); | |
session.SaveChanges(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var session = documentStore.OpenSession()) | |
{ | |
// We store a person and its city | |
session.Store(new Person("Gamlor",new City("Vals"))); | |
session.SaveChanges(); | |
} | |
using (var session = documentStore.OpenSession()) | |
{ | |
// We can get the person, because it's in a person document | |
var hasPeopleInDB = session.Query<Person>().Any(); | |
Console.Out.WriteLine("Do we have people-documents in the db? {0}", | |
hasPeopleInDB ? "Yes, we do" : "No, we dont"); | |
// However the city isn't in its own document | |
var hasCitiesInDB = session.Query<City>().Any(); | |
Console.Out.WriteLine("Do we have city-documents in the db? {0}", | |
hasCitiesInDB ? "Yes, we do" : "No, we dont"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var order = session.Query<Order>().First(); | |
var costumer = session.Load<Customer>(order.CustomerId); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface INamedObject | |
{ | |
string Id { get; set; } | |
string Name { get; set; } | |
} | |
internal class Customer : INamedObject | |
{ | |
public Customer(string name) | |
{ | |
Name = name; | |
} | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public string Address { get; set; } | |
} | |
internal class Order | |
{ | |
public Order() | |
{ | |
Items = new List<OrderItem>(); | |
} | |
public string Id { get; set; } | |
public DenormalizedReference<Customer> CustomerReference { get; set; } | |
public IList<OrderItem> Items { get; private set; } | |
} | |
// Denormalized reference, which stores the name of the named object. | |
public class DenormalizedReference<T> where T : INamedObject | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public static implicit operator DenormalizedReference<T>(T doc) | |
{ | |
return new DenormalizedReference<T> | |
{ | |
Id = doc.Id, | |
Name = doc.Name | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Customer | |
{ | |
public Customer(string name) | |
{ | |
Name = name; | |
} | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public string Address { get; set; } | |
} | |
class Order | |
{ | |
public Order() | |
{ | |
Items = new List<OrderItem>(); | |
} | |
public string Id { get; set; } | |
public string CustomerId { get; set; } | |
public IList<OrderItem> Items { get;private set; } | |
public void AddToOrder(OrderItem item) | |
{ | |
Items.Add(item); | |
} | |
} | |
class OrderItem | |
{ | |
public OrderItem(string name) | |
{ | |
Name = name; | |
} | |
public string Name { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var customer = new Customer("Gamlor"); | |
session.Store(customer); | |
var firstOrder = new Order() | |
{ | |
// This is automatically converted to a named-reference | |
// due to the magic of the implicit cast operator. | |
// Now the order has the id and the name of the customer document | |
CustomerReference = customer | |
}; | |
firstOrder.AddToOrder(new OrderItem("Magic Unicorn")); | |
session.Store(firstOrder); | |
// Later on: | |
var order = session.Query<Order>().First(); | |
// no need for loading the customer document as long as we only need the name | |
var customerName = order.CustomerReference.Name; | |
Console.Out.WriteLine(customerName); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment