Just read this post. http://ayende.com/Blog/archive/2011/03/08/support-dynamic-fields-with-nhibernate-and-.net-4.0.aspx

It this also possible with DO?


asked Mar 08 '11 at 13:46

Marco's gravatar image


edited Mar 16 '11 at 02:50

Alex%20Yakunin's gravatar image

Alex Yakunin

An intermediate note: did you notice this case doesn't really differs from static mapping? I.e. if I'd want to add a new Attributes.Country field to Attributes, NH won't persist it. You must explicitly declare mapping from it to make it work.

So "dynamic" here is just usage of dynamically typed object instead of statically typed one. You still can't add generally any fields to it - likely, that's you expect here.

Another suspicious part is join usage. Any query to Customer type will contain all these joins - probably that's not what you expect as well.

(Mar 09 '11 at 02:50) Alex Yakunin Alex%20Yakunin's gravatar image

Btw, does NH LINQ translator supports fields of dynamic objects?

(Mar 09 '11 at 02:50) Alex Yakunin Alex%20Yakunin's gravatar image

I just added the answer.

(Mar 16 '11 at 03:26) Alex Yakunin Alex%20Yakunin's gravatar image

One Answer:

There are few ways to implement dynamic fields with DO:

1) EAV approach. Everything is pretty obvious here: you should store additional attribute values in a special table.


  • Immutable DB schema
  • No need to rebuild the Domain to add new properties
  • It's easy to use new properties in queries: ... where entity.DynamicPart.Attributes.SingleOrDefault(a => a.Name=="DynamicPropertyKey").IntValue==someInt.


  • You don't have the same level of freedom in indexing such attributes as in regular case.
  • You can store reference properties this way (e.g. as identifier of referenced entity or in Key.Format(...) format). The first option allows you to use them in queries (e.g. join referenced entities), the second doesn't.
  • Dealing with dynamic collection properties should be pretty complex in this case.

2) Runtime entity generation approach.


  • You can store & index dynamic properties as you wish => the performance in this case must be much better.
  • This approach allows you to support reference & collection properties - and e.g. use them in queries.
  • It's easy to use new properties in queries: ... where entity.GetField<int>("DynamicPropertyKey")==someInt.


  • You should implement some logic responsible for (re)generating your own dynamic entity types
  • You should rebuild the Domain to add new properties
  • Mutable DB schema.

answered Mar 16 '11 at 03:08

Alex%20Yakunin's gravatar image

Alex Yakunin

edited Mar 16 '11 at 13:55

Btw, in both cases possible usage of dynamic seems quite attractive. It must be relatively easy to implement support of dynamic for all DO4 entities, the only complex part is querying: expression rewriters for dynamic properties must differ dependently on a particular case (EAV / runtime code generation / your own approach).

If you're interested in such feature, please vote for them in UserVoice - I already created 3 corresponding items there.

(Mar 16 '11 at 03:24) Alex Yakunin Alex%20Yakunin's gravatar image

Thanks for the complete answer! My vote is added

(Mar 16 '11 at 05:49) Marco Marco's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview


Once you sign in you will be able to subscribe for any updates here



Asked: Mar 08 '11 at 13:46

Seen: 3,382 times

Last updated: Mar 16 '11 at 13:55

powered by OSQA