Analyze POCOs
Overview
Plain old CLR objects, or plain old class objects (POCOs), are simple class objects that don't inherit features from any framework-specific base classes or interfaces. If your application uses POCOs, you can use the C# Analyzer to preview them as JSON objects.
To learn more about POCOs, see the POCOs page on Wikipedia and the Work with POCOs page in the .NET/C# driver documentation.
Preview as JSON
The following code sample shows an example of a POCO class definition
and its corresponding JSON translation. By previewing your
POCOs in JSON, you can see how BSON serialization attributes, such as BsonId
and
BsonElement
, change the shape of the resulting BSON during serialization.
class Order { [ ] public int OrderNumber { get; set; } [ ] public int CustomerId { get; set; } public string Date { get; set; } [ ] public string Email { get; set; } }
{ "_id": 6783456, "customer_id": 678234, "Date": "06/03/2023" }
Tip
Sample Data
The C# Analyzer includes predefined sample values for certain common property names. If you use these property names in your POCO, the Analyzer uses the sample values in the JSON output. For any property names without predefined sample values, the Analyzer uses a random value that matches the property's data type.
For a list of property names with sample values, see the JSON sample value file in the MongoDB C# Analyzer GitHub repo.
Preview POCOs in Visual Studio
To preview your POCOs in Visual Studio, perform the following actions:
Install the C# Analyzer as described in the Install the MongoDB C# Analyzer guide.
Define a POCO class.
Move your cursor over the ... ellipsis annotation beneath the first word of your class definition to display an information message that contains the JSON translation.
Click on the following corresponding tab to see a POCO class definition with or without an information message displayed:
Tip
Rule ID
The information message begins with the C# Analyzer Rule ID that generated the
popup. In the previous example, the Rule ID is MAPoco1001
, indicating
the POCO is valid. To learn more about this rule, see the
Rules and Message Content guide.