title: C# MongoDB: How to correctly map a domain object?
body: <p>I recently started reading Evans' Domain-Driven design book and started a small sample project to get some experience in DDD. At the same time I wanted to learn more about MongoDB and started to replace my SQL EF4 repositories with MongoDB and the latest official C# driver.
  Now this question is about MongoDB mapping. I see that it is pretty easy to map simple objects with public getters and setters - no pain there. But I have difficulties mapping domain entities without public setters. As I learnt, the only really clean approach to construct a valid entity is to pass the required parameters into the constructor.  Consider the following example:</p>
  
  <pre><code>public class Transport : IEntity&lt;Transport&gt;
  {
      private readonly TransportID transportID;
      private readonly PersonCapacity personCapacity;
  
      public Transport(TransportID transportID,PersonCapacity personCapacity)
      {
          Validate.NotNull(personCapacity, "personCapacity is required");
          Validate.NotNull(transportID, "transportID is required");
  
          this.transportID = transportID;
          this.personCapacity = personCapacity;
      }
  
      public virtual PersonCapacity PersonCapacity
      {
          get { return personCapacity; }
      }
  
      public virtual TransportID TransportID
      {
          get { return transportID; }
      } 
  }
  
  
  public class TransportID:IValueObject&lt;TransportID&gt;
  {
      private readonly string number;
  
      #region Constr
  
      public TransportID(string number)
      {
          Validate.NotNull(number);
  
          this.number = number;
      }
  
      #endregion
  
      public string IdString
      {
          get { return number; }
      }
  }
  
   public class PersonCapacity:IValueObject&lt;PersonCapacity&gt;
  {
      private readonly int numberOfSeats;
  
      #region Constr
  
      public PersonCapacity(int numberOfSeats)
      {
          Validate.NotNull(numberOfSeats);
  
          this.numberOfSeats = numberOfSeats;
      }
  
      #endregion
  
      public int NumberOfSeats
      {
          get { return numberOfSeats; }
      }
  }
  </code></pre>
  
  <p>Obviously automapping does not work here. Now I can map those three classes by hand via <code>BsonClassMaps</code> and they will be stored just fine. The problem is, when I want to load them from the DB I have to load them as <code>BsonDocuments</code>, and parse them into my domain object. I tried lots of things but ultimately failed to get a clean solution. Do I really have to produce DTOs with public getters/setters for MongoDB and map those over to my domain objects? Maybe someone can give me some advice on this.</p>