Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge remote branch 'craig/master'

  • Loading branch information...
commit f604a53bc7fcb38381d1a6fe8a0b0259ee0760cc 2 parents 6fd8ac4 + 8f7cde7
authored August 02, 2010
91  examples/Simple/Main.cs
... ...
@@ -1,6 +1,9 @@
1 1
 using System;
2 2
 using System.Configuration;
  3
+using System.Linq;
3 4
 using MongoDB;
  5
+using MongoDB.Configuration;
  6
+using MongoDB.Linq;
4 7
 
5 8
 namespace Simple
6 9
 {
@@ -18,11 +21,93 @@ internal class MainClass
18 21
         private Mongo mongo;
19 22
         private IMongoDatabase simple;
20 23
 
  24
+        private class MyClass
  25
+        {
  26
+            public string Name { get; set; }
  27
+            public  int Corners { get; set; }
  28
+        }
  29
+
  30
+        private class SubClass : MyClass
  31
+        {
  32
+            public double Ratio { get; set; }
  33
+        }
  34
+
21 35
         public static void Main(string[] args)
22 36
         {
23  
-            var main = new MainClass();
24  
-            main.Setup();
25  
-            main.Run();
  37
+            var config = new MongoConfigurationBuilder();
  38
+
  39
+            // COMMENT OUT FROM HERE
  40
+            config.Mapping(mapping =>
  41
+            {
  42
+                mapping.DefaultProfile(profile =>
  43
+                {
  44
+                    profile.SubClassesAre(t => t.IsSubclassOf(typeof(MyClass)));
  45
+                });
  46
+                mapping.Map<MyClass>();
  47
+                mapping.Map<SubClass>();
  48
+            });
  49
+            // TO HERE
  50
+
  51
+            config.ConnectionString("Server=127.0.0.1");
  52
+
  53
+            using (Mongo mongo = new Mongo(config.BuildConfiguration()))
  54
+            {
  55
+                mongo.Connect();
  56
+                try
  57
+                {
  58
+                    var db = mongo.GetDatabase("TestDb");
  59
+                    var collection = db.GetCollection<MyClass>();
  60
+
  61
+                    MyClass square = new MyClass()
  62
+                    {
  63
+                        Corners = 4,
  64
+                        Name = "Square"
  65
+                    };
  66
+
  67
+                    MyClass circle = new MyClass()
  68
+                    {
  69
+                        Corners = 0,
  70
+                        Name = "Circle"
  71
+                    };
  72
+
  73
+                    SubClass sub = new SubClass()
  74
+                    {
  75
+                        Name = "SubClass",
  76
+                        Corners = 6,
  77
+                        Ratio = 3.43
  78
+                    };
  79
+
  80
+                    collection.Save(square);
  81
+                    collection.Save(circle);
  82
+                    collection.Save(sub);
  83
+
  84
+                    var superclass = (from item in db.GetCollection<MyClass>("MyClass").Linq()
  85
+                                where item.Corners > 1
  86
+                                select item.Corners).ToList();
  87
+
  88
+                    var subclass = (from item in db.GetCollection<SubClass>("MyClass").Linq()
  89
+                                    where item.Ratio > 1
  90
+                                    select item.Corners).ToList();
  91
+
  92
+                    Console.WriteLine("Count by LINQ on typed collection: {0}", collection.Linq().Count(x => x.Corners > 1));
  93
+                    Console.WriteLine("Count by LINQ on typed collection2: {0}", db.GetCollection<SubClass>().Linq().Count(x => x.Corners > 1));
  94
+                    //Console.WriteLine("Count by LINQ on typed collection3: {0}", db.GetCollection<SubClass>().Count(new { Corners = Op.GreaterThan(1) }));
  95
+                    Console.WriteLine("Count on typed collection: {0}", collection.Count(new { Corners = Op.GreaterThan(1) }));
  96
+
  97
+                    var coll = db.GetCollection("MyClass");
  98
+                    var count = coll.Count(new Document("Corners", Op.GreaterThan(1)));
  99
+                    Console.WriteLine("Count: {0}", count);
  100
+                    Console.ReadKey();
  101
+                }
  102
+                finally
  103
+                {
  104
+                    mongo.Disconnect();
  105
+                }
  106
+            }
  107
+
  108
+            //var main = new MainClass();
  109
+            //main.Setup();
  110
+            //main.Run();
26 111
         }
27 112
 
28 113
         /// <summary>
4  source/MongoDB/Configuration/Mapping/Auto/AutoMapper.cs
@@ -81,8 +81,8 @@ public AutoMapper(Func<Type, bool> filter)
81 81
                 CollectionName = _profile.GetCollectionName(classType),
82 82
                 DiscriminatorAlias = _profile.GetDiscriminatorAlias(classType)
83 83
             };
84  
-            if(!classType.IsInterface && !classType.IsAbstract)
85  
-                classMap.Discriminator = _profile.GetDiscriminator(classType);
  84
+            //if(!classType.IsInterface && !classType.IsAbstract)
  85
+            //    classMap.Discriminator = _profile.GetDiscriminator(classType);
86 86
 
87 87
             classMap.IdMap = CreateIdMap(classType);
88 88
             classMap.ExtendedPropertiesMap = CreateExtendedPropertiesMap(classType);
5  source/MongoDB/Serialization/Builders/PolymorphicClassMapBuilder.cs
@@ -38,7 +38,10 @@ public void AddProperty(string name, object value)
38 38
         public object BuildObject()
39 39
         {
40 40
             if (_concreteEntityBuilder == null)
41  
-                throw new Exception("Discriminator did not show up.");
  41
+            {
  42
+                //we'll assume that this is the root class in the hierarchy.
  43
+                _concreteEntityBuilder = new ConcreteClassMapBuilder(_classMap);
  44
+            }
42 45
 
43 46
             return _concreteEntityBuilder.BuildObject();
44 47
         }
18  source/MongoDB/Serialization/Descriptors/DocumentClassMapPropertyDescriptor.cs
@@ -31,8 +31,22 @@ public DocumentClassMapPropertyDescriptor(IMappingStore mappingStore, IClassMap
31 31
         /// <returns></returns>
32 32
         public override IEnumerable<BsonProperty> GetProperties()
33 33
         {
34  
-            if (ShouldPersistDiscriminator())
35  
-                yield return CreateProperty(ClassMap.DiscriminatorAlias, ClassMap.Discriminator.GetType(), ClassMap.Discriminator, false);
  34
+            if(ShouldPersistDiscriminator())
  35
+            {
  36
+                if (_document.ContainsKey("count")) //this is a special case
  37
+                {
  38
+                    var queryDoc = _document["query"] as Document;
  39
+                    if (queryDoc == null)
  40
+                    {
  41
+                        //TODO: implement someway of shoving this value into the doc...
  42
+                        throw new NotSupportedException("Count queries on subclasses using anonymous types is not supported.");
  43
+                    }
  44
+                    else if (!queryDoc.ContainsKey(ClassMap.DiscriminatorAlias))
  45
+                        queryDoc.Append(ClassMap.DiscriminatorAlias, ClassMap.Discriminator);
  46
+                }
  47
+                else
  48
+                    yield return CreateProperty(ClassMap.DiscriminatorAlias, ClassMap.Discriminator.GetType(), ClassMap.Discriminator, false);
  49
+            }
36 50
 
37 51
             foreach (string key in _document.Keys)
38 52
             {

0 notes on commit f604a53

Please sign in to comment.
Something went wrong with that request. Please try again.