I am new to DataObjects. I read its documents and searched the website, but I didn't find a way to create/extend an entity at run time.

For I let end users to add some attributes(fields), even some entities (tables) at run time in UI layer, I need such a feature to make my story true.

A fake statments may be the following:

EntityBuilder b=new EntityBuilder() ...

Just like TypeBuilder in System.Refelection.Emit.



This thread was imported from our support forum. The original discussion may contain more detailed answer. Original topic by thinkly.

asked May 20 '10 at 02:37

Editor's gravatar image


One Answer:

mahdness wrote:

I was thinking about this same idea the other day...

Does it offer any benefits over the traditional approach of adding an EntitySet<customvalues> property to your entities?

I guess one benefit is that you can index by these fields since they would be in the same DB table

Dmitri Maximov (Xtensive) wrote:

Hello guys,

Let me introduce one of the DataObjects.Net 4 unknown treasures. ;) Probably, you've already met IModule interface but haven't known the purpose of OnDefinitionsBuild method. Let me remind you the signature:

/// <summary>
  /// The contract of extension module.
  /// </summary>
  public interface IModule
    /// <summary>
    /// Called when the build of <see cref="DomainModelDef"/> is completed.
    /// </summary>
    /// <param name="context">The domain building context.</param>
    /// <param name="model">The domain model definition.</param>
    void OnDefinitionsBuilt(BuildingContext context, DomainModelDef model);


As one might knows, Domain model building process consists of 2 phases: during the first one we build definitions of persistent types, fields, indexes, etc. (this is so-called 'Definition model'), whereas during the second phase we construct full-fledged model objects such as TypeInfo, FieldInfo, IndexInfo, AssociationInfo and everything. The reason why we're using such a 2-phase process is that we want to allow user-code to get a control over Definition model and make all necessary changes to it in right time in right place.

The method I'm talking about (OnDefinitionsBuilt) is exactly for this purpose. It allows you to redefine anything in the Definition model, even to create it from scratch.

Let's look at the following example:

public class MyModule : IModule
    public void OnBuilt(Domain domain)
      // Nothing interesting here

    public void OnDefinitionsBuilt(BuildingContext context, DomainModelDef model)
      // Here model parameter is the Definition model, it contains a collection of TypeDef, HierarchyDef instances
      // You can register additional persistent types
      model.DefineType(typeof (MyClass));
      // Or remove already registered

      // Add, modify, remove fields
      model.Types[typeof(MyClass)].DefineField("MyDynamicField", typeof(string));
      model.Types["MyClass"].Fields["MyDynamicField"].Length = 50;

      // Manage indexes
      indexDef = model.Types["MyClass"].DefineIndex("IX_MYDYNAMICFIELD");
      indexDef.KeyFields.Add("MyDynamicField", Direction.Positive);

      // And much more, e.g. HierarchyDef & FullTextIndexDef management

Notice that the process is being taken place during the Domain build procedure. So it is not in pure runtime as you may think as DO 4 need complete and built model to perform database schema upgrade at the very beginning of its work.

Hope that helps.

P.S. Is such a material worth inclusion in DO Samples or at least publishing with more details in DO blog? It might be a bit specific & geek-oriented, imho.

answered May 20 '10 at 06:57

Editor's gravatar image


Dmitry, this is what you would call "joga bonito".

Naturally, everything deserves documentation!

(May 20 '10 at 06:57) ara ara'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: May 20 '10 at 02:37

Seen: 6,295 times

Last updated: May 20 '10 at 02:37

powered by OSQA