However, the current mongodb C# driver FindAll() return
List<BsonDocument> which does not support dynamic object. Anybody know
a .NET 4 dynamic supported mongodb C# driver?
However, the current mongodb C# driver FindAll() return
List<BsonDocument> which does not support dynamic object. Anybody know
a .NET 4 dynamic supported mongodb C# driver?
Thanks a lot
-- You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongodb-user@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
Currently, there is no support for dynamic in the MongoDB driver. This is because it is based on .NET 3.5. However, since a .NET 4.0 assembly can reference a .NET 3.5 assembly, it is possible for you to write a IBsonSerializationProvider and an IBsonSerializer in .NET 4.0 to support dynamics.
Thanks Craigg, i just found a way to do it and it work!
Add BsonValue to inherit DynamicObject
public abstract class BsonValue : DynamicObject, IComparable<BsonValue>, IConvertible, IEquatable<BsonValue>
and then add these methods to BsonDocument to implement dynamic support:
public override bool TryDeleteMember(DeleteMemberBinder binder) { if (binder == null) throw new ArgumentNullException("binder"); if (Contains(binder.Name)) { Remove(binder.Name); return true; } return false; }
public override bool TryGetMember(GetMemberBinder binder, out object result) { BsonValue value; if (TryGetValue(binder.Name, out value)) { result = value; return true; } result = null; return true; }
public override bool TrySetMember(SetMemberBinder binder, object value) { if (binder == null) throw new ArgumentNullException("binder"); Set(binder.Name, BsonValue.Create(value)); return true; }
public override IEnumerable<string> GetDynamicMemberNames() { return _indexes.Keys; }
On Thursday, April 19, 2012 9:29:30 PM UTC+7, craiggwilson wrote:
> Currently, there is no support for dynamic in the MongoDB driver. This is > because it is based on .NET 3.5. However, since a .NET 4.0 assembly can > reference a .NET 3.5 assembly, it is possible for you to write a > IBsonSerializationProvider and an IBsonSerializer in .NET 4.0 to support > dynamics.
> We, 10gen, are looking at doing this in the future. I have spiked some > support athttps://github.com/craiggwilson/mongo-csharp-driver/tree/dynamic if > you want to take a look. There are most definitely bugs, but it shows that > it is possible. > On Thursday, April 19, 2012 1:57:31 AM UTC-5, huy hoang le wrote:
Yes, that would work. However, we cannot do this as it would mean changing our minimum supported framework from 3.5 to 4.0. If you look at how I did it in the link I posted above, you'll find that it is fairly similar with the exception that I cannot use DynamicObject and instead have to drop lower down the stack because I'm already inheriting from BsonValue.
> On Thursday, April 19, 2012 9:29:30 PM UTC+7, craiggwilson wrote:
>> Currently, there is no support for dynamic in the MongoDB driver. This is >> because it is based on .NET 3.5. However, since a .NET 4.0 assembly can >> reference a .NET 3.5 assembly, it is possible for you to write a >> IBsonSerializationProvider and an IBsonSerializer in .NET 4.0 to support >> dynamics.
>> We, 10gen, are looking at doing this in the future. I have spiked some >> support at >> https://github.com/craiggwilson/mongo-csharp-driver/tree/dynamic if you >> want to take a look. There are most definitely bugs, but it shows that it >> is possible. >> On Thursday, April 19, 2012 1:57:31 AM UTC-5, huy hoang le wrote:
On Friday, April 20, 2012 7:19:48 PM UTC+7, craiggwilson wrote:
> Yes, that would work. However, we cannot do this as it would mean > changing our minimum supported framework from 3.5 to 4.0. If you look at > how I did it in the link I posted above, you'll find that it is fairly > similar with the exception that I cannot use DynamicObject and instead have > to drop lower down the stack because I'm already inheriting from BsonValue.
> On Thursday, April 19, 2012 11:44:35 PM UTC-5, huy hoang le wrote:
>> Thanks Craigg, i just found a way to do it and it work!
>> Add BsonValue to inherit DynamicObject
>> public abstract class BsonValue : DynamicObject, IComparable<BsonValue>, >> IConvertible, IEquatable<BsonValue>
>> and then add these methods to BsonDocument to implement dynamic support:
>> public override bool TryDeleteMember(DeleteMemberBinder binder) >> { >> if (binder == null) >> throw new ArgumentNullException("binder"); >> if (Contains(binder.Name)) >> { >> Remove(binder.Name); >> return true; >> } >> return false; >> }
>> public override bool TryGetMember(GetMemberBinder binder, out >> object result) >> { >> BsonValue value; >> if (TryGetValue(binder.Name, out value)) >> { >> result = value; >> return true; >> } >> result = null; >> return true; >> }
>> public override bool TrySetMember(SetMemberBinder binder, object >> value) >> { >> if (binder == null) >> throw new ArgumentNullException("binder"); >> Set(binder.Name, BsonValue.Create(value)); >> return true; >> }
>> On Thursday, April 19, 2012 9:29:30 PM UTC+7, craiggwilson wrote:
>>> Currently, there is no support for dynamic in the MongoDB driver. This >>> is because it is based on .NET 3.5. However, since a .NET 4.0 assembly can >>> reference a .NET 3.5 assembly, it is possible for you to write a >>> IBsonSerializationProvider and an IBsonSerializer in .NET 4.0 to support >>> dynamics.
>>> We, 10gen, are looking at doing this in the future. I have spiked some >>> support at >>> https://github.com/craiggwilson/mongo-csharp-driver/tree/dynamic if you >>> want to take a look. There are most definitely bugs, but it shows that it >>> is possible. >>> On Thursday, April 19, 2012 1:57:31 AM UTC-5, huy hoang le wrote:
Theoretically, it is supporting dynamic type. However, (practice) dynamic type itself conflict with BSON de-serialization,
i.e. public class TestClass2<T> { public MongoDB.Bson.ObjectId Id { get; set; } public T data { get; set; } }
1. Inserting with TestClass2<int> instance typed as dynamic c# variable, it will smoothly insert into mongodb as well as retrieve.
2. Now when you try to insert TestClass2<string> dynamic type, save will work but won't retrieve because of type mismatch and will fail. Exception message: "An error occurred while deserializing the data property of class LearningPad.TestClass2`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]: Input string was not in a correct format." @MongoConnection.cs Line 442
3. Only overcome is to use distinguish collections for each possible generic type (by writing some custom function that evaluate collection name runtime) but that is impracticable as you can't query them at once.
4. I use object wrapper (that works fine) with generic promotion of fields in wrapper to make them queryable and indexable. So far that is the best solution I'm into.
On Friday, 20 April 2012 20:26:09 UTC+1, huy hoang le wrote:
> I see, thanks Craig. I think you guys should release a separate version > for .NET 4
> On Friday, April 20, 2012 7:19:48 PM UTC+7, craiggwilson wrote:
>> Yes, that would work. However, we cannot do this as it would mean >> changing our minimum supported framework from 3.5 to 4.0. If you look at >> how I did it in the link I posted above, you'll find that it is fairly >> similar with the exception that I cannot use DynamicObject and instead have >> to drop lower down the stack because I'm already inheriting from BsonValue.
>> On Thursday, April 19, 2012 11:44:35 PM UTC-5, huy hoang le wrote:
>>> Thanks Craigg, i just found a way to do it and it work!
>>> Add BsonValue to inherit DynamicObject
>>> public abstract class BsonValue : DynamicObject, IComparable<BsonValue>, >>> IConvertible, IEquatable<BsonValue>
>>> and then add these methods to BsonDocument to implement dynamic support:
>>> public override bool TryDeleteMember(DeleteMemberBinder binder) >>> { >>> if (binder == null) >>> throw new ArgumentNullException("binder"); >>> if (Contains(binder.Name)) >>> { >>> Remove(binder.Name); >>> return true; >>> } >>> return false; >>> }
>>> public override bool TryGetMember(GetMemberBinder binder, out >>> object result) >>> { >>> BsonValue value; >>> if (TryGetValue(binder.Name, out value)) >>> { >>> result = value; >>> return true; >>> } >>> result = null; >>> return true; >>> }
>>> public override bool TrySetMember(SetMemberBinder binder, object >>> value) >>> { >>> if (binder == null) >>> throw new ArgumentNullException("binder"); >>> Set(binder.Name, BsonValue.Create(value)); >>> return true; >>> }
>>> On Thursday, April 19, 2012 9:29:30 PM UTC+7, craiggwilson wrote:
>>>> Currently, there is no support for dynamic in the MongoDB driver. This >>>> is because it is based on .NET 3.5. However, since a .NET 4.0 assembly can >>>> reference a .NET 3.5 assembly, it is possible for you to write a >>>> IBsonSerializationProvider and an IBsonSerializer in .NET 4.0 to support >>>> dynamics.
>>>> We, 10gen, are looking at doing this in the future. I have spiked some >>>> support at >>>> https://github.com/craiggwilson/mongo-csharp-driver/tree/dynamic if >>>> you want to take a look. There are most definitely bugs, but it shows that >>>> it is possible. >>>> On Thursday, April 19, 2012 1:57:31 AM UTC-5, huy hoang le wrote: