[CSHARP-205] Add Insert Methods to MongoCollection<TDefaultDocument> for improved powershell support Created: 18/Apr/11 Updated: 02/Apr/15 Resolved: 25/Apr/11 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Feature Request |
| Affects Version/s: | 1.0 |
| Fix Version/s: | 1.1 |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Justin Dearing | Assignee: | Robert Stam |
| Resolution: | Done | Votes: | 0 |
| Labels: | powershell | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Powershell |
||
| Description |
|
As previously stated powershell cannot call generic methods of non-generic classes without nasty reflection. So Added these insert overloads to MongoCollection<TDefaultDocument> allows us to insert documents. Powershell script to test:
Add-Type -Path "$($mongoDriverPath)\MongoDB.Driver.dll"; $db = [MongoDB.Driver.MongoDatabase]::Create('mongodb://localhost/powershell'); |
| Comments |
| Comment by Robert Stam [ 25/Apr/11 ] |
|
Added new non-generic Insert methods. |
| Comment by Robert Stam [ 25/Apr/11 ] |
|
I tested a slightly different script to confirm that the new MongoCollection constructor is not needed. Here's the script I used: $mongoDriverPath = "C:\work\10gen\mongodb\mongo-csharp-driver\Driver\bin\Debug" $server = [MongoDB.Driver.MongoServer]::Create("mongodb://localhost/?safe=true") $document = @{ $collection.RemoveAll() $fetched = $collection.FindOne() I think the general rule for Powershell friendliness is: "For every generic method there should be a equivalent non-generic method that Powershell users can call". This rule will also help C# users when they are writing meta data driven code where the types of the documents being inserted are not known at compile time. |
| Comment by Robert Stam [ 21/Apr/11 ] |
|
Still don't see why the constructor is needed. The return value of $db["name"] is MongoCollection<BsonDocument>, NOT MongoCollection. So no cast should be required (and therefore no special constructor needed to support the cast). Is there some weird Powershell thing going on that I'm not seeing? |
| Comment by Justin Dearing [ 21/Apr/11 ] |
|
Robert, I agree we should try to understand the ramification of supporting powershell. I'll move that general discussion to the dev list. To touch on your specific points. I don't think the new keyword is needed, but I will test some more. The constructor is needed, take it away and my code doesn't work. What the constructor does (like the HashTable/Idictionary BsonDocument constructor) is allow Powershell to cast objects. In this case it is casting from a MongoCollection to a MontoCollection<TDefaultDocument>. I will make InsertBatch work as well. I will make separate tickets for update and remove as well. As far as being "sure," we can write more unit tests for ambiguous cases. However, without an explicit or implicit case between the two types I don't see how ambiguity can creep in. |
| Comment by Robert Stam [ 20/Apr/11 ] |
|
I'm going to hold off on this until we fully understand all the ramifications of supporting Powershell. Comments:
|