title: How to deal with referencing of separately stored objects in document DBs like Mongo?
body: <p>This problem is easily solved in ORMs like Entity Framework or NHibernate, but I do not see any ready-made solution in c# driver for MongoDb. Let's say I have collection of objects of type A referencing objects type B which I need to store in separate collection, so that once specific object B is changed, all A referencing it need to be aware of the change. In other words, I need this object relation being normalized. In the same time I need B being referenced by A inside the class, not by Id, but by type reference like shown below:</p>
  
  <pre><code>public class A
  {
     public B RefB { get; set; }
  }
  </code></pre>
  
  <p>Do I have to handle all this referencing consistency on my own? If so, which approaches is the best to use? Do I have to keep both B's Id and B reference in the class and somehow take care of sync their values like that:</p>
  
  <pre><code>public class A
  {
      // Need to implement reference consistency as well
      public int RefBId { get; set; }
  
      private B _refB;
      [BsonIgnore]
      public B RefB
      {
          get { return _refB; }
          set { _refB = value; RefBId = _refB.Id }
      }
  }
  </code></pre>
  
  <p>I know somebody may say relational database meets this case the best, I know, but I really have to use document Db like MongoDb, it solves many problems, and in most cases I need to store objects denormalized for my project, however sometimes we might need mixed design inside single storage.</p>