Dynamic Entities?

536 views
Skip to first unread message

EricH

unread,
Jun 1, 2010, 1:29:43 PM6/1/10
to ravendb
Hi,

Under the "What is Raven" documentation, in the "Expected Usage and
Sweet Spots" section, it states...

"The schema-less nature makes it ideal to store dynamic data..."
"Dynamic Entities, such as user-customizable entities..."

How does one achieve dynamic entities in C#? Is the documentation
implying that the dynamic type introduced with .Net 4 be used in these
cases with Raven? Or is there something else?

How can a user add attributes to a statically typed class (at
runtime), unless you create an attribute which is a "dictionary" of
"user added" attributes (or some other form of composition which would
be similar to what you'd do with a RDBMS).

I'm struggling with whether I'm missing the obvious, or if it's just
the dynamic type that is being implied.

Thanks,

Eric

Ayende Rahien

unread,
Jun 1, 2010, 1:38:15 PM6/1/10
to rav...@googlegroups.com
Eric,
This really depend on the way that you implement your dynamic entities in your model.
One option is to simply put all the dynamic stuff in a dictionary, which would just work with Raven.
Another would be to use dynamic objects, support for doing that in the client API would come in a build or two.

Rob Ashton

unread,
Jun 1, 2010, 3:01:09 PM6/1/10
to ravendb
> Another would be to use dynamic objects, support for doing that in the
> client API would come in a build or two.

I'm really looking forward to that, it'll help massively with one of
my projects :)
Message has been deleted

Brian Vallelunga

unread,
Jun 1, 2010, 6:19:34 PM6/1/10
to ravendb
Sorry, that was posted prematurely. Anyway, what I meant was:

public class User {

public IDictionary<string, dynamic> Attributes { get; set; }

}

I've tried this out and it works really well. You can then do castless
assignments such as:

var user = GetUser();
int age = user.Attributes("age");
string city = user.Attributes("city");


There are problems storing decimals however. If you store a decimal
and then try to retrieve it you'll receive an error about casting to/
from a double. I saw another thread about decimal storage, so
hopefully that will be resolved at some point.

user.Attributes.Add("amount", 500m);
decimal amount = user.Attributes("amount");


On Jun 1, 6:14 pm, Brian Vallelunga <br...@vallelunga.com> wrote:
> Right now you can do a hybrid, such as:
>
> public class User {

Matt Warren

unread,
Jun 24, 2010, 4:18:12 AM6/24/10
to ravendb
As of the latest build, there is now store/load support for dynamic
docs.

So you can write code like this:
dynamic employee = new ExpandoObject();
employee.Name = "John Smith";
employee.Age = 33;
employee.Phones = new ExpandoObject();
employee.Phones.Home = "0111 123123";
employee.Phones.Office = "0772 321123";

session.Store(employee);
string docId = employee.Id;
session.SaveChanges();
...
dynamic employeeLoad = session.Load<dynamic>(docId);

It supports all classes that inherit from IDynamicMetaObjectProvider.

Brian Vallelunga

unread,
Jun 24, 2010, 3:54:54 PM6/24/10
to ravendb
I'm currently doing something *interesting* with Raven and dynamic
types for a single load. For example:

public ItemBase GetItem(string id)
{
return session.Load<dynamic>(id);
}

This is working out very well, where the actual document returned is
of a type derived from ItemBase.

What I need now is to return multiple documents in a similar manner.

Something like:

Type t = GetTypeAtRuntime();
var docs = session.LuceneQuery(t); // Would return all documents of
type t as dynamic objects or objects of type t.

Brian

Matt Warren

unread,
Jun 25, 2010, 4:54:51 AM6/25/10
to ravendb
Yeah returning dynamic objects from a query is next on my list, I
wanted to get the initial dynamic stuff added first. But you're right
Load<dynamic> was always working, it was the Store(..) that wasn't.

Could you paste in some examples of how you'd want dynamic queries to
work? Then I can use these as tests cases to make sure I'm
implementing it correctly.
Reply all
Reply to author
Forward
0 new messages