RavenDB官网文档翻译系列第一

 

本系列文章主要翻译自RavenDB官方文档,有些地方做了删减,有些内容整合在一起。欢迎有需要的朋友阅读。毕竟还是中文读起来更亲切吗。下面进入正题。

起航

 

获取RavenDB

 

RavenDB可以通过NuGet获取,也可以直接在RavenDB官网下载压缩包。

首先了解下压缩包中的内容,可以更好的根据需要进行选择。

  • Backup  – 包含用于备份的Raven.Backup工具
  • Bundles – 包含所有非内置的插件,如AuthenticationEncryption
  • Client – 包含用于开发的所有.NET客户端库
  • Server – 所有服务器文件(包括Raven.Server.exe.config等配置文件)
  • Smuggler – 包含用于在服务器间导入导出数据的工具
  • Web – 所有用于IIS开发的文件

 

官网下载页面还提供了安装包,并给出了安装包安装文档,图文并茂比较易懂,这里就不翻译了。安装包使用文档传送门

 

启动服务器

 

要想开始体验RavenDB,一个运行的RavenDB服务是不可少的。在下载的压缩包中包含一个Start.cmd文件,运行此文件会以控制台方式启动一个服务器(调试模式),这种模式适合作为开发目的或简单尝试下各种功能而非发布服务器。当服务运行后,可以通过http://localhost:port/访问管理工具。默认的端口号为8080,当8080被占用时会选择下一个可用的端口号。

 

提示

如果需要将RavenDB安装为服务请参考这篇文章。RavenDB也可以运行于IIS中或嵌入到可执行程序中。

 

客户端

在下载的压缩包的Client目录下可以找到所有的.NET客户端。在项目中引用相应的程序集后,可以在Raven.*命名空间下访问到所有的类,其中DocumentStore是最值得关注的。这是你的应用访问RavenDB的入口,它将建立并维护你的应用和服务器之间的连接。请参考介绍DocumentStore的几篇文章:

  • document store是什么

  • 创建一个document store

  • 设置默认数据库

提示

需要注意的是DocumentStore是一个重量级的对象,每个程序只应该有一个DocumentStore实例(单例)

有两种使用DocumentStore操作数据的方式,第一(也是推荐的方式)是通过Session,第二是Commands,Commands是一种底层操作数据的方式,只应该在确有需求的情况下使用。Session和Commands两者都包含同步和异步方法。

可以通过下面列出的文章可以更多:

  • 什么是Session,它如何工作?及打开一个Session

  • 什么是Commands?

 

示例

在继续之前,我希望指出这里的大部分文章都是用Northwind数据库。可以在这里查看如何部署这个数据库的更多细节。

 

原理及一些示例

RavenDB是一个文档型数据库,所有存储的对象都被称作文档。每一个文档都是以JSON格式存储,其中包含一个标识文档的key,data及metadata。metadata包含各种各样描述文档的信息,如修改日期或collection的布局。

 

创建DocumentStore,打开Session,存储及加载实体

下面的示例将演示怎样创建DocumentStore,打开Session,存储及加载一些实体。

using (IDocumentStore store = new DocumentStore
{
 Url = "http://localhost:8080/", // server URL
 DefaultDatabase = "Northwind" // default database
})
{
 store.Initialize(); // initializes document store, by connecting to server and downloading various configurations
 using (IDocumentSession session = store.OpenSession()) // opens a session that will work in context of 'DefaultDatabase'
 {
  Employee employee = new Employee
  {
   FirstName = "John",
   LastName = "Doe"
  };
  session.Store(employee); // stores employee in session, assigning it to a collection `Employees`
  string employeeId = employee.Id; // Session.Store will assign Id to employee, if it is not set
  session.SaveChanges(); // sends all changes to server
  // Session implements Unit of Work pattern,
  // therefore employee instance would be the same and no server call will be made
  Employee loadedEmployee = session.Load<Employee>(employeeId);
  Assert.Equal(employee, loadedEmployee);
 }
}

 

查询

要实现查询,必须使用索引。简言之,索引是一个定义使用哪些字段(及什么值)来在文档中查找的服务器端函数。整个索引过程是异步进行的,执行这个操作会很快得到响应结果,即使有大量数据被更新,然而这种实现方式下索引不一定是最新的。在继续之前,建议阅读下面文章:

这个示例假设你的数据库包含Northwind示例数据。如果你不知道如何部署示例数据,请查看这篇文章。

/// <summary>
/// All _ in index class names will be converted to /
/// it means that Employees_ByFirstNameAndLastName will be Employees/ByFirstNameAndLastName
/// when deployed to server
/// 
/// AbstractIndexCreationTask is a helper class that gives you strongly-typed syntax
/// for creating indexes
/// </summary>
public class Employees_ByFirstNameAndLastName : AbstractIndexCreationTask<Employee>
{
 public Employees_ByFirstNameAndLastName()
 {
  // this is a simple (Map) index LINQ-flavored mapping function
  // that enables searching of Employees by
  // FirstName, LastName (or both)
  Map = employees => from employee in employees
         select new
       {
        FirstName = employee.FirstName,
        LastName = employee.LastName
       };
 }
}

 

关于文档的一些点点滴滴

文档被分为这么几个部分:

  • Indexes部分中,你可以找到所有索引和查询相关的理论。

  • Transformers部分包含服务器端转换函数的信息,这些函数用来形成查询结果。

  • Client API部分,包含客户端中大部分函数的API参考及相关基本示例。

  • Server部分包含服务器管理,维护,配置,安装及调试的信息。

  • Studio部分让你了解可以使用Studio完成那些工作

 

示例

下面列出可用的示例程序:

 

演练服务器

如果对此感兴趣,请查看这篇文章。

 

 

 

 

 

 

 

posted @ 2015-06-01 11:47  hystar  阅读(1088)  评论(9编辑  收藏  举报