Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-140

Hide hierarchy discriminator in query when anonymous type is used for wrapping.

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.0
    • Affects Version/s: 0.9
    • Component/s: None
    • None
    • Environment:
      MongoDB v 1.6.3
      C# Driver version: both Bison and Driver 0.9.1 (from github)

      Here is sample code firstly:
      ----------------------------------------
      A a = new A();
      a.b = "asd1";
      a.c = "asd2";

      db.GetCollection<A>("A").Insert(a);

      var o1 = db.GetCollection<A>("A").FindOneAs<A>(Query.Wrap(new

      { c = "asd2" }));

      object selector = new { c = "asd2" }

      ;
      var o2 = db.GetCollection<A>("A").FindOneAs<A>(Query.Wrap(selector));

      Assert.IsNotNull(o1); // Success
      Assert.IsNotNull(o2); // Fail
      ----------------------------------------

      The problem is when I want to use anonymous type instance as a selector for wrapping query.
      Getting o1 I use it in a place of calling, so actualType and nominalType are equals and driver does not insert hierarhy discriminator into query.
      sometimes I want to pass filter as argument for functions, so this case anonymous type insance casted to object, so actualType and nominalType are differ, and driver adds discriminator according to this code:

      BsonClassMapSerialzier.Serialize(..) line 175.
      ----------------------------------------
      if (actualType != nominalType || classMap.DiscriminatorIsRequired || classMap.HasRootClass) {
      var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType);
      var discriminator = discriminatorConvention.GetDiscriminator(nominalType, actualType);
      if (discriminator != null)

      { bsonWriter.WriteName(discriminatorConvention.ElementName); discriminator.WriteTo(bsonWriter); }

      }
      ----------------------------------------

      I think it would be better if no descriminator applied when using anonymous instance as a subject for wrapping.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            bug0r Timofei Mironov
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: