RavenDb是文档型数据库,但是我们常常也需要定义对象之间的关系,那RavenDb当中是如何处理的呢?
RavenDb提供了优雅的解决方式,使用正确的话,可以减少数据开销以及网络拥堵
Denormalization
第一种就是反规范化,下面是一个订单的JSON格式
在Order这个订单当中我们把我们需要的客户信息(名字)也保存下来了,使用的时候,它直接就读出来了。
{ // Order document with id: orders/1234
"Customer": {
"Name": "Itamar",
"Id": "customers/2345"
},
Items: [
{
"Product": {
"Id": "products/1234",
"Name": "Milk",
"Cost": 2.3
},
"Quantity": 3
}
]
}
初始的类设计如下:
public class Order
{
public string CustomerId { get; set; }
public string[] SupplierIds { get; set; }
public Referral Refferal { get; set; }
public LineItem[] LineItems { get; set; }
public double TotalPrice { get; set; }
}
public class Customer
{
public string Name { get; set; }
public string Address { get; set; }
public short Age { get; set; }
public string HashedPassword { get; set; }
}
在Order中持有下面这个反规范化的类,而不只是CustomerId
public class DenormalizedCustomer
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
Includes
1)RavenDb提供了这个Includes的功能去限制反规范化,上一种方式是直接保存了另外一个对象的一些属性,这种方式只是保存了一个引用,当根对象被加载的时候,和它关联的选项也会预加载。
我们可以这样做:
var order = session.Include<Order>(x => x.CustomerId)
.Load("orders/1234");
//这一句不会在服务端执行
var cust = session.Load<Customer>(order.CustomerId);
2)加载多个文档
同时加载"orders/1234", "orders/4321"
var orders = session.Include<Order>(x => x.CustomerId)
.Load("orders/1234", "orders/4321");
foreach (var order in orders)
{
// this will not require querying the server!
var cust = session.Load<Customer>(order.CustomerId);
}
另外一种用法:
var orders = session.Query<Order>()
.Customize(x => x.Include<Order>(o => o.CustomerId))
.Where(x => x.TotalPrice > 100)
.ToList();
foreach (var order in orders)
{
// this will not require querying the server!
var cust = session.Load<Customer>(order.CustomerId);
}
这里面有两个通道,当调用Load()方式时,调用了Results channel,第二个是 Includes channel,被包含的文档是通过它来返回的,并且保存在session cache当中。
3)一对多Includes
一个订单,多个提供商
var order = session.Include<Order>(x => x.SupplierIds)
.Load("orders/1234");
foreach (var supplierId in order.SupplierIds)
{
// this will not require querying the server!
var supp = session.Load<Supplier>(supplierId);
}
4)二级包含关系
二级包含关系是值,Order类的属性里面没有,是在Order类的属性Referral的属性当中有
public class Referral
{
public string CustomerId { get; set; }
public double CommissionPercentage { get; set; }
}
var order = session.Include<Order>(x => x.Refferal.CustomerId)
.Load("orders/1234");
// this will not require querying the server!
var referrer = session.Load<Customer>(order.Refferal.CustomerId);
它也支持集合
var order = session.Include<Order>(x => x.Refferal.CustomerId)
.Load("orders/1234");
// this will not require querying the server!
var referrer = session.Load<Customer>(order.Refferal.CustomerId);
5)lucene query
var orders = session.Advanced.LuceneQuery<Order2>()
.Include(x => x.Customer2Id)
.WhereGreaterThan(x => x.TotalPrice, 100)
.ToList();
foreach (var order in orders)
{
// this will not require querying the server!
var cust2 = session.Load<Customer2>(order.Customer2Id);
}
var orders = session.Advanced.LuceneQuery<Order2>()
.Include("CustomerId")
.WhereGreaterThan(x => x.TotalPrice, 100)
.ToList();
foreach (var order in orders)
{
// this will not require querying the server!
var cust2 = session.Load<Customer2>(order.Customer2Id);
}
Combining Approaches
这种是把前两种方式结合起来了
public class Order3
{
public DenormalizedCustomer Customer { get; set; }
public string[] SupplierIds { get; set; }
public Referral Refferal { get; set; }
public LineItem[] LineItems { get; set; }
public double TotalPrice { get; set; }
}
var order = session.Include<Order3, Customer2>(x => x.Customer.Id)
.Load("orders/1234");
// this will not require querying the server!
var fullCustomer = session.Load<Customer2>(order.Customer.Id);