Docs Menu
Docs Home
/
MongoDB C# Analyzer
/

Analyze POCOs

On this page

  • Overview
  • Preview as JSON
  • Preview POCOs in Visual Studio

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.

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
{
[BsonId]
public int OrderNumber { get; set; }
[BsonElement("customer_id")]
public int CustomerId { get; set; }
public string Date { get; set; }
[BsonIgnore]
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.

To preview your POCOs in Visual Studio, perform the following actions:

  1. Install the C# Analyzer as described in the Install the MongoDB C# Analyzer guide.

  2. Define a POCO class.

  3. 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:

Screenshot of POCO definition with an ellipsis annotation in Visual Studio
Screenshot of POCO definition with an information message displayed in Visual Studio

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.

Back

LINQ Expressions