Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

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

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
Owner

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

I renamed the method to GetRegisteredClassMaps().

Craig Wilson

pushed to master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 13, 2012
  1. Alex Brown

    added AllClassMaps - to enable us to get all the currently registered…

    alexjamesbrown authored
    … class maps out of BsonClassMap
This page is out of date. Refresh to see the latest.
8 Bson/Serialization/BsonClassMap.cs
View
@@ -288,6 +288,14 @@ public static bool IsClassMapRegistered(Type type)
}
/// <summary>
+ /// Returns all registered class maps.
+ /// </summary>
+ public static IEnumerable<BsonClassMap> AllClassMaps()
+ {
+ return __classMaps.Values;
+ }
+
+ /// <summary>
/// Looks up a class map (will AutoMap the class if no class map is registered).
/// </summary>
/// <param name="classType">The class type.</param>
42 BsonUnitTests/DefaultSerializer/BsonClassMapTests.cs
View
@@ -310,6 +310,48 @@ public void TestIsClassMapRegistered()
}
[TestFixture]
+ public class BsonClassMapAllClassMapTests
+ {
+ private static bool __testAlreadyRan;
+
+ public class C
+ {
+ public ObjectId Id;
+ public int X;
+ }
+
+ public class D
+ {
+ public ObjectId Id;
+ public string Y;
+ }
+
+ [Test]
+ public void TestAllClassMaps()
+ {
+ // test can only be run once
+ if (__testAlreadyRan)
+ {
+ return;
+ }
+
+ __testAlreadyRan = true;
+
+ Assert.IsFalse(BsonClassMap.IsClassMapRegistered(typeof(C)));
+ Assert.IsFalse(BsonClassMap.IsClassMapRegistered(typeof(D)));
+ BsonClassMap.RegisterClassMap<C>(cm => cm.AutoMap());
+ BsonClassMap.RegisterClassMap<D>(cm => cm.AutoMap());
+
+ var classMaps = BsonClassMap.AllClassMaps();
+ var classMapTypes = classMaps.Select(x => x.ClassType).ToList();
+
+ Assert.IsTrue(BsonClassMap.IsClassMapRegistered(typeof(C)));
+ Assert.Contains(typeof(C), classMapTypes);
+ Assert.Contains(typeof(D), classMapTypes);
+ }
+ }
+
+ [TestFixture]
public class BsonShouldSerializeTests
{
public class ClassA
Something went wrong with that request. Please try again.