Advertisement
Guest User

Custom Discriminator Convention

a guest
May 11th, 2012
3,270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. using System.Linq;
  2. using System.Threading;
  3. using MongoDB.Bson.Serialization.Attributes;
  4. using MongoDB.Driver;
  5. using MongoDB.Driver.Builders;
  6. using MongoDB.Driver.Linq;
  7. using NUnit.Framework;
  8. using System.Collections.Generic;
  9. using System;
  10. using MongoDB.Bson;
  11. using MongoDB.Bson.Serialization;
  12. using System.Linq.Expressions;
  13. using MongoDB.Bson.Serialization.Conventions;
  14.  
  15. namespace MongoDB.Groups.Repro
  16. {
  17.     public static class Program
  18.     {
  19.         public interface IFoo
  20.         {
  21.             string Description { get; set; }
  22.         }
  23.  
  24.         [Serializable]
  25.         public class Foo : IFoo
  26.         {
  27.             public string Description { get; set; }
  28.         }
  29.  
  30.         [Serializable]
  31.         public class Bar
  32.         {
  33.             public ObjectId Id { get; set; }
  34.  
  35.             public IFoo MyFoo { get; set; }
  36.         }
  37.  
  38.         public static void Main()
  39.         {
  40.             BsonSerializer.RegisterDiscriminatorConvention(typeof(IFoo), new FooDiscriminatorConvention());
  41.  
  42.             var server = MongoServer.Create();
  43.             var database = server.GetDatabase("test");
  44.             var collection = database.GetCollection<Bar>("bars");
  45.  
  46.             collection.RemoveAll();
  47.  
  48.             var docWithDiscriminator = new BsonDocument
  49.             {
  50.                 new BsonElement("MyFoo", new BsonDocument("Description", "Blah").Add("_t", "Foo"))
  51.             };
  52.             var docWithOutDiscriminator = new BsonDocument
  53.             {
  54.                 new BsonElement("MyFoo", new BsonDocument("Description", "Blah"))
  55.             };
  56.  
  57.             collection.InsertBatch(new[] { docWithDiscriminator, docWithOutDiscriminator });
  58.  
  59.             var bars = collection.FindAll();        
  60.         }
  61.  
  62.         private class FooDiscriminatorConvention : IDiscriminatorConvention
  63.         {
  64.             public string ElementName
  65.             {
  66.                 get { return "_t"; }
  67.             }
  68.  
  69.             public Type GetActualType(MongoDB.Bson.IO.BsonReader bsonReader, Type nominalType)
  70.             {
  71.                 var bookmark = bsonReader.GetBookmark();
  72.                 bsonReader.ReadStartDocument();
  73.                 if (bsonReader.FindElement(ElementName))
  74.                 {
  75.                     var value = bsonReader.ReadString();
  76.                     if (value != "Foo")
  77.                     {
  78.                         throw new NotSupportedException();
  79.                     }
  80.                 }
  81.  
  82.                 bsonReader.ReturnToBookmark(bookmark);
  83.                 return typeof(Foo);
  84.             }
  85.  
  86.             public BsonValue GetDiscriminator(Type nominalType, Type actualType)
  87.             {
  88.                 return actualType.Name;
  89.             }
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement