[CSHARP-539] Support C# dynamic type Created: 24/Jul/12  Updated: 26/Jan/15  Resolved: 14/Jun/14

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.5
Fix Version/s: 2.0

Type: New Feature Priority: Major - P3
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to CSHARP-933 Improve IBsonSerializer design Closed

 Description   

We have not yet determined in which version we might add support for C# dynamic types. This ticket is a place where you can comment on the desirability of the feature and/or vote for it.



 Comments   
Comment by Robert Stam [ 14/Jun/14 ]

The changes made for CSHARP-933 already included support for serializing C# dynamic types.

Comment by Craig Wilson [ 12/Feb/14 ]

Github commit: https://github.com/mongodb/mongo-csharp-driver/commit/a9b7afb7aa25d4e7e0bc29b4e7fc28d9688af9f0

Comment by Michael Kennedy [ 24/Jul/13 ]

That's perfect, thanks for the tentative update.

Comment by Craig Wilson [ 24/Jul/13 ]

Probably towards the end of year, likely in conjunction with server 2.6. Not set in stone and won't release it until it's ready.

Comment by Michael Kennedy [ 23/Jul/13 ]

Great, thanks Craig. Any time frame for the release of v2.0?

Comment by Craig Wilson [ 23/Jul/13 ]

Yep, version 2.0 of the driver will have full support for dynamic objects, regardless of if they are expando or not as long as they implement IDynamicMetaObjectProvider. It may require the use of a special collection (i.e. db.GetDynamicCollection()), but we'll see when we implement it. You can see a spike for support here: https://github.com/craiggwilson/mongo-csharp-driver/tree/dynamic/Bson.Dynamic. This was done a long time ago, but proves the point.

Comment by Michael Kennedy [ 23/Jul/13 ]

Hi, I'd like to add some thoughts. It would be nice if you could save dynamic objects and similarly load them back (as ExpandoObjects). Here's some code I would expect to work which does not currently (error is "to save a doc, it must have an id").

var ctx = new MongoDbDataContext();

dynamic dyn = new ExpandoObject();
dyn._id = ObjectId.GenerateNewId();
dyn.Name = "Pierre";
dyn.Age = 39;

WriteConcernResult writeConcern = ctx.Db.GetCollection("tester").Save(dyn);
if (!writeConcern.Ok)
throw new ApplicationException(writeConcern.ErrorMessage);
else
Console.WriteLine ("Save successful");

I think the actual problem is simply that the way properties are retrieved using reflection doesn't work for ExpandoObjects. But there is a simple alternative if the object is that type.

Cheers,
Michael

Comment by Vladimir Perevalov [ 08/Apr/13 ]

After looking into it a bit more. I see, that this may have notable performance cost. So, if you should implement dynamic behavior, it should be opt-in, not default.
Something like a different set of methods "FindAllDynamic" etc.

Comment by Vladimir Perevalov [ 08/Apr/13 ]

It would very convenient if BsonDocument implemented dynamic calls. So instead of doc["SomeProp"] you could write doc.SomeProp. Basically it should require a very simple wrapper over normal dictionary based workflow.

Comment by Craig Wilson [ 17/Sep/12 ]

Check this here: http://huddledmasses.org/powershell-3-finally-on-the-dlr/. Essentially, powershell now runs on the DLR which means that it will natively use the dynamic support we build into the driver. Hence, us using dynamic will make it much simpler to use with powershell across the board. The real trick is to figure out how to expose the dynamic functionality. For instance, MongoCollection<dynamic> would be nice. Except we don't know it's "dynamic" at runtime. All we see is "object".

Comment by Justin Dearing [ 17/Sep/12 ]

I've never done dynamic in C#, but a PSObject/PSCustomObject in powershell is a dynamic object. Also if you add NoteProperties to an object, it wraps that object in a PSObject. Therefore this has powershell implications.

Generated at Wed Feb 07 21:37:08 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.