RavenDb学习(八)高级特性上半部分

时间:2022-04-29
本文章向大家介绍RavenDb学习(八)高级特性上半部分,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
1、事务支持
别的关系型数据库和RavenDb一起使用

using (var transaction = new TransactionScope())
{
    BlogPost entity = session.Load<BlogPost>("blogs/1");
 
    entity.Title = "Some new title";
 
    session.SaveChanges(); // will create HTTP request
 
    session.Delete(entity);
 
    session.SaveChanges(); // will create HTTP request
 
    transaction.Complete(); // will commit transaction
}

2、文档元数据
Raven-Clr-Type - Records the CLR type, set and used by the JSON serialization/deserialization process in the Client API.
Raven-Entity-Name - Records the entity name, aka the name of the RavenDB collection this entity belongs to.
Non-Authoritive-Information - This boolean value will be set to true if the data received by the client has been modified by an uncommitted transaction. You can read more on it in the Advanced section.
Temp-Index-Score - When querying RavenDB, this value is the Lucene score of the entity for the query that was executed.
Raven-Read-Only - This document should be considered read only and not modified.
Last-Modified - The last modified time-stamp for the entity.
@etag - Every document in RavenDB has a corresponding e-tag (entity tag) stored as a sequential Guid. This e-tag is updated by RavenDB every time the document is changed.
@id - The entity id, as extracted from the entity itself.
获取最后修改时间的例子:
var product = session.Load<Product>(1);
RavenJObject metadata = session.Advanced.GetMetadataFor(product);
// Get the last modified time stamp, which is known to be of type DateTime
DateTime collectionName = metadata.Value<DateTime>("Last-Modified");

返回所有表的表名的map函数:
from doc in docs
where doc["@metadata"]["Raven-Entity-Name"] != null
select new { Tag = doc["@metadata"]["Raven-Entity-Name"] };

//修改元数据
1)通过session.Advanced.GetMetadatFor(entity) 获取元数据,然后修改并保存
2)通过documentStore.RegisterListener(myStoreListener) 注册一个IDocumentStoreListener,当有session要进行保存的时候,它能修改元数据

3、查询指定列
分页查询某个列
var firstPage = session.Advanced.DocumentStore.DatabaseCommands.GetTerms("indexName", "MyProperty", null, 128);
var secondPage = session.Advanced.DocumentStore.DatabaseCommands.GetTerms("indexName", "MyProperty", firstPage.Last(), 128);

4、动态列

public class Product
{
    public string Id { get; set; }
    public List<Attribute> Attributes { get; set; }
}
 
public class Attribute
{
    public string Name { get; set; }
    public string Value { get; set; }
}

上述例子,一个产品有很多个属性,属性的类型是不固定的,需要是dynamic的

//创建索引
public class Product_ByAttribute : AbstractIndexCreationTask<Product>
{
    public Product_ByAttribute()
    {
        Map = products => from p in products
                          select new
                                     {
                                         _ = p.Attributes
                                            .Select(attribute =>
                                                CreateField(attribute.Name, attribute.Value, false, true))
                                     };
    }
}

//我们对值并不关注,只是希望调用CreateField方法,所以用_作为一种转换反射。

//查询
var products = session.Advanced.LuceneQuery<Product>("Product/ByAttribute")
    .WhereEquals("Color", "Red")
    .ToList();
这样子它不仅仅支持字符串,也支持数字等


5、自定义序列化

当RavenDb收到一个POCO的时候,它会默认把它序列化为JSON格式的数据。

1)忽略一个属性

public class Carrot
{
    public string Id { get; set; }
 
    public decimal Length { get; set; }
 
    [JsonIgnore]
    public decimal LengthInInch
    {
        get
        {
            /* some calculations */
            return this.Length;
        }
 
        set
        {
            //...
        }
    }
}

2)序列化的时候更换名字

public class Recipe
{
    public string Id { get; set; }
 
    [JsonProperty(PropertyName = "dishes")]
    public IList<IVegetable> SideDishes { get; set; }
}

3)允许自引用
//树形结构数据
[JsonObject(IsReference = true)]
public class Category
{
    public string Id { get; set; }
    public string Name { get; set; }
    public Category Parent { get; set; }
    public List<Category> Children { get; set; }
 
    public Category()
    {
        this.Children = new List<Category>();
    }
 
    public void Add(Category category)
    {
        category.Parent = this;
        this.Children.Add(category);
    }
}

4)自定义IContractResolver实现是序列 

store.Conventions.JsonContractResolver = new DefaultContractResolver(shareCache: true)
                                             {
                                                 DefaultMembersSearchFlags =
                                                     BindingFlags.Public | BindingFlags.Instance
                                             };