MongoDb contract mapper
It is available as a nuget package http://nuget.org/List/Packages/DataDuctus.MongoDb.Serialization

[DataContract]
public class SingleLevelCompositionOneInheritanceMain
{
  [Key]
  [DataMember]
  public string Id { get; set; }

  [DataMember]
  public SingleLevelCompositionOneInheritanceA Composite { get; set; }
}

[DataContract]
[KnownType(typeof(SingleLevelCompositionOneInheritanceB))]
public class SingleLevelCompositionOneInheritanceA
{
  [Key]
  [DataMember]
  public string Id { get; set; }

  [DataMember]
  public string BaseName { get; set; }
}

[DataContract]
public class SingleLevelCompositionOneInheritanceB : SingleLevelCompositionOneInheritanceA
{
  [DataMember]
  public string SubClassName { get; set; }
}

public void TestMethod()
{
  new DataContractMapper<SingleLevelCompositionOneInheritanceMain>().Register();

  var coll = this.TestDatabase.GetCollection<OneInheritanceA>("theCollection");

  coll.RemoveAll(); // Clear everything

  var clsA = new SingleLevelCompositionOneInheritanceMain
                        { 
                          Composite = new SingleLevelCompositionOneInheritanceA 
                                                    { 
                                                      BaseName = "Base only as composite" 
                                                     } 
                        };

  var clsB = new SingleLevelCompositionOneInheritanceMain
                        {
                           Composite = new SingleLevelCompositionOneInheritanceB 
                                                     { 
                                                        BaseName = "Base in composite", 
                                                        SubClassName = "Sub in composite" 
                                                     }
                        };

  coll.Save(clsA);
  coll.Save(clsB);

  var objA = coll.FindOneByIdAs<SingleLevelCompositionOneInheritanceMain>(clsA.Id);
  var objB = coll.FindOneByIdAs<SingleLevelCompositionOneInheritanceMain>(clsB.Id);
}

Last edited Aug 17, 2012 at 8:03 AM by solo95, version 3

Comments

No comments yet.