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

、事务支持
别的关系型数据库和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
}

、文档元数据
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.
获取最后修改时间的例子:
);
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"] };

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

、查询指定列
分页查询某个列
);
);

、动态列

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();
这样子它不仅仅支持字符串,也支持数字等

、自定义序列化

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

)忽略一个属性

public class Carrot
{
    public string Id { get; set; }

    public decimal Length { get; set; }

    [JsonIgnore]
    public decimal LengthInInch
    {
        get
        {
            /* some calculations */
            return this.Length;
        }

        set
        {
            //...
        }
    }
}

)序列化的时候更换名字

public class Recipe
{
    public string Id { get; set; }

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

)允许自引用
//树形结构数据
[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);
    }
}

)自定义IContractResolver实现是序列 

store.Conventions.JsonContractResolver = new DefaultContractResolver(shareCache: true)
                                             {
                                                 DefaultMembersSearchFlags =
                                                     BindingFlags.Public | BindingFlags.Instance
                                             };
上一篇:异步SRAM控制器的Verilog建模


下一篇:shell 查找与替换