Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

added AllClassMaps static method to BsonClassMap #129

Closed
wants to merge 1 commit into from

3 participants

Alex Brown Craig Wilson Justin Nel
Alex Brown

Allows us to get all class maps out of BsonClassMap.

Craig Wilson
Collaborator

Good idea Alex. We'll discuss pulling this in.

Alex Brown

Thanks :)

Alex Brown

just wondering if there was any update on this?

Craig Wilson
Collaborator

Alex, there is an IsClassMapRegistered method? Is this suitable for what you need, or is there another reason you would like to have a list.

Alex Brown

There is, and while that works, it doesn't allow me to get all mapped types, without iterating over my types, and running .IsClassMapRegistered on each one...

Justin Nel

@craiggwilson I could do with this feature from @alexjamesbrown as well, as I need to call the "Reset" method on each of them. Without a stored list, this does not perform well at all...

Alex Brown

It would really make life a lot easier for people...

Justin Nel

As a temp solution, I am using the following...

typeof(BsonClassMap).GetField("__classMaps", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null)
Alex Brown

Yeah, but again, not ideal
This pull request literally just returns __classMaps.Values as a public IEnumerable<BsonClassMap>
It's even covered by unit tests!

Would make the lives of people wanting this functionality so much easier.

Justin Nel

Fully agree with you that the temp solution I am using is not ideal, and I would much rather have your pull request and a couple other bits from the Experiment branch, as I have to do a lot of dirty hacks to get what I need currently.

Craig Wilson
Collaborator

in code review... track at https://jira.mongodb.org/browse/CSHARP-628.

I renamed the method to GetRegisteredClassMaps().

Craig Wilson
Collaborator

pushed to master

Craig Wilson craiggwilson closed this November 10, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 13, 2012
Alex Brown added AllClassMaps - to enable us to get all the currently registered…
… class maps out of BsonClassMap
a075402
This page is out of date. Refresh to see the latest.
8  Bson/Serialization/BsonClassMap.cs
@@ -288,6 +288,14 @@ public static bool IsClassMapRegistered(Type type)
288 288
         }
289 289
 
290 290
         /// <summary>
  291
+        /// Returns all registered class maps.
  292
+        /// </summary>
  293
+        public static IEnumerable<BsonClassMap> AllClassMaps()
  294
+        {
  295
+            return __classMaps.Values;
  296
+        }
  297
+
  298
+        /// <summary>
291 299
         /// Looks up a class map (will AutoMap the class if no class map is registered).
292 300
         /// </summary>
293 301
         /// <param name="classType">The class type.</param>
42  BsonUnitTests/DefaultSerializer/BsonClassMapTests.cs
@@ -310,6 +310,48 @@ public void TestIsClassMapRegistered()
310 310
     }
311 311
 
312 312
     [TestFixture]
  313
+    public class BsonClassMapAllClassMapTests
  314
+    {
  315
+        private static bool __testAlreadyRan;
  316
+
  317
+        public class C
  318
+        {
  319
+            public ObjectId Id;
  320
+            public int X;
  321
+        }
  322
+
  323
+        public class D
  324
+        {
  325
+            public ObjectId Id;
  326
+            public string Y;
  327
+        }
  328
+
  329
+        [Test]
  330
+        public void TestAllClassMaps()
  331
+        {
  332
+            // test can only be run once
  333
+            if (__testAlreadyRan)
  334
+            {
  335
+                return;
  336
+            }
  337
+
  338
+            __testAlreadyRan = true;
  339
+
  340
+            Assert.IsFalse(BsonClassMap.IsClassMapRegistered(typeof(C)));
  341
+            Assert.IsFalse(BsonClassMap.IsClassMapRegistered(typeof(D)));
  342
+            BsonClassMap.RegisterClassMap<C>(cm => cm.AutoMap());
  343
+            BsonClassMap.RegisterClassMap<D>(cm => cm.AutoMap());
  344
+
  345
+            var classMaps = BsonClassMap.AllClassMaps();
  346
+            var classMapTypes = classMaps.Select(x => x.ClassType).ToList();
  347
+
  348
+            Assert.IsTrue(BsonClassMap.IsClassMapRegistered(typeof(C)));
  349
+            Assert.Contains(typeof(C), classMapTypes);
  350
+            Assert.Contains(typeof(D), classMapTypes);
  351
+        }
  352
+    }
  353
+
  354
+    [TestFixture]
313 355
     public class BsonShouldSerializeTests
314 356
     {
315 357
         public class ClassA
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.