RavenDB 动态对象 [英] RavenDB dynamic objects

查看:47
本文介绍了RavenDB 动态对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码看起来像这样:

I have code that looks something like this:

using (var session = DocumentStore.OpenSession())
{
    var dbItem = session.Load<dynamic>(item.Id);    
    if (dbItem is DynamicJsonObject)
    {
        dbItem["PropertyName"] = "new value";
    }
    session.SaveChanges();
}

我不知道如何更新 dbItem 的属性.
有谁知道该怎么做?我试过像这样直接访问属性名称:dbItem.PropertyName我还尝试过转换为 ExpandoObject、IDictionary 等.但似乎没有任何效果.

What I can't figure out is how to update properties of the dbItem.
Does anyone know what to do? I have tried accessing the property name directly like this: dbItem.PropertyName I have also tried casting to ExpandoObject, IDictionary and more. But nothing seems to work.

推荐答案

从 Raven 2.5 开始,对动态对象的支持似乎主要针对事物的读取方面,并且在现有对象上设置属性并不那么容易因为Raven.Abstractions.Linq.DynamicJsonObject继承了DynamicObject,只实现了动态合约的read/invoke方法,比如TryGetMemberTryGetIndexTryInvokeMember.但没有像 TrySetMember 这样的项目.

As of Raven 2.5, the support for dynamic objects seems to be mostly for the read side of things, and it's not that easy to set properties on an existing object because Raven.Abstractions.Linq.DynamicJsonObject, which inherits DynamicObject, only implements the read/invoke methods of the dynamic contract, like TryGetMember, TryGetIndex, and TryInvokeMember. but none of the items like TrySetMember.

但是,如果您转换为 IDynamicJsonObject,它将提供对您可以操作的内部 RavenJObject 的访问.

However, if you cast to IDynamicJsonObject it provides access to the inner RavenJObject which you can manipulate.

此代码示例应说明如何:

This code sample should illustrate how:

using (var session = store.OpenSession())
{
    dynamic entity = new ExpandoObject();
    entity.Id = "DynamicObjects/1";
    entity.Hello = "World";
    session.Store(entity);
    session.SaveChanges();
}

using (var session = store.OpenSession())
{
    var json = session.Load<dynamic>("DynamicObjects/1") as IDynamicJsonObject;
    json.Inner["Name"] = "Lionel Ritchie";
    json.Inner["Hello"] = "Is it me you're looking for?";
    session.SaveChanges();
}

using (var session = store.OpenSession())
{
    dynamic loadedAgain = session.Load<dynamic>("DynamicObjects/1");
    Console.WriteLine("{0} says Hello, {1}", loadedAgain.Name, loadedAgain.Hello);
    // -> Lionel Ritchie says Hello, Is it me you're looking for?"
}

这篇关于RavenDB 动态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆