[CSHARP-801] IIdGenerator.GenerateId gives "InsertOpreration" instead of "MongoCollection" for the "container" paramter in 1.8.2 Created: 13/Aug/13  Updated: 25/Aug/15  Resolved: 26/Aug/13

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.8.2
Fix Version/s: 1.8.3

Type: Bug Priority: Major - P3
Reporter: Christian Sparre Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Zip Archive GenerateIdBug.zip    

 Description   

After updating to version 1.8.2 our custom id generator came crashing down. The "container" parameter of IIdGenerator.GenerateId used to be a MongoCollection instance but now seems to be an InsertOperation whatever that is, the class is internal so there is really no way to use it for anything.

I could not find anything in the release notes and documentation or issues here on Jira to hint at a change. We downgraded to 1.8.1 and it works.

I have attached a small test project that for me shows the error.



 Comments   
Comment by Githook User [ 25/Aug/15 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-801: The container parameter to GenerateId should be a MongoCollection (not an InsertOperation).
Branch: v1.8.x
https://github.com/mongodb/mongo-csharp-driver/commit/0bcca2e5abae5e7d26807bafc2c079ce5667c744

Comment by auto [ 25/Sep/13 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-801: The container parameter to GenerateId should be a MongoCollection (not an InsertOperation).
Branch: v1.8.3
https://github.com/mongodb/mongo-csharp-driver/commit/0bcca2e5abae5e7d26807bafc2c079ce5667c744

Comment by auto [ 26/Aug/13 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-801: The container parameter to GenerateId should be a MongoCollection (not an InsertOperation).
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/3407deaa73005a656c8b29188a6b13dac0bf473a

Comment by Robert Stam [ 23/Aug/13 ]

Fix is in code review.

Comment by Craig Wilson [ 19/Aug/13 ]

No, it's just an inconsistency. This slipped by and was unintended.

Comment by Attila Hajdrik [ 19/Aug/13 ]

Is that normal that for a .Save operation on the collection the IdGenerator gets MongoCollection in context, for .Insert and .InsertBatch it receives an InsertOperation instance in the same variable?

Comment by Craig Wilson [ 18/Aug/13 ]

Not a hack for that one. I think you can probably just use the database name and create a new MongoClient and get the database off of it. Sorry 'bout this. We are trying to figure out the best way to handle this given the refactoring.

Comment by Attila Hajdrik [ 17/Aug/13 ]

Hi!

I also hit this issue. I've to generate sequential integer IDs. From GenerateId method I need to access the Database instance to get hold of the "IDs" collection.

Is there a hack for that too?

Thanks,
Attila

Comment by Craig Wilson [ 13/Aug/13 ]

Ok, until we can come up with a solution, I have a hack for you. If all you need is the database name and the collection name, you can use reflection to get it. Let me know if this is enough for now and we'll work on getting a proper fix in our next release.

public object GenerateId(object container, object document)
{
    var type = container.GetType();
    var databaseName = type.GetProperty("DatabaseName", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(container, null);
    var collectionName = type.GetProperty("CollectionName", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(container, null);
 
    ...
}

Comment by Christian Sparre [ 13/Aug/13 ]

Hi Craig

Just went through the code of our id generator and we actually do not use the MongoCollection directly, but we use the name and a reference to the MongoDatabase instance.

It's a HiLo kind of id generator where each application instance gets a batch of ids.

So a solution where we can access the Database instance/name and the collectionname would be enough in our case.

The InsertOperation does seem to have the databaseName and collectionName information so a solution should be possible.

Comment by Craig Wilson [ 13/Aug/13 ]

Thanks for the report and sorry about the issues. As we no longer have access to MongoCollection, we're going to have to figure something out. Could you tell us what you are using the MongoCollection for in your IdGenerator so we can come up with something to use in it's place?

Craig

Comment by Christian Sparre [ 13/Aug/13 ]

After looking a bit at the code on GitHub I think it might have happened with this commit https://github.com/mongodb/mongo-csharp-driver/commit/f61f965cc1e741d39bda247dfdf2445c2000ffed where Robert seems to have moved some code from MongoCollection to a number of "operations"

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