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.
Pros:
- 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
.
Cons:
- 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.
Pros:
- 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
.
Cons:
- 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 Yakunin
2971●4●4●12
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 toAttributes
, 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.Btw, does NH LINQ translator supports fields of dynamic objects?
I just added the answer.