The AsXyz and ToXyz methods and properties in BsonValue have the problem that because they are declared in BsonValue you can call any of them all of the time even if there is no chance they will succeed. The shear number of them also clutter up Intellisense.
These methods and properties exist because the only accessor in BsonDocument returns values of type BsonValue, so you need these methods and properties to work with the resulting BsonValue.
However, a better way is to have a family of GetXyz methods in BsonDocument that return different types as appropriate, so since the return value is already of the desired type no further AsXyz or ToXyz methods or properties are needed.
For example, the old way:
var pages = document["pages"].AsInt32;
the new way:
var pages = document.GetInt32("pages");
- is related to
-
CSHARP-560 Remove explicit conversions from BsonValue
-
- Closed
-
- related to
-
CSHARP-563 Add indexing to BsonValue
-
- Closed
-