Reputation: 5223
I have been looking through different samples and posts, and I cant find any recommended naming conventions. When I look into a document I can see the standard properties (id, ts ..) are lower case. However naming conventions for C# is that properties are upper case. What is the recommended approach here, do you lower/upper case everything, or lower case only properties in Cosmos DB and not in your C# models or .. ? Is there any guidelines here?
Upvotes: 5
Views: 7950
Reputation: 3184
I went with CamelCase to follow JS like standard but, here is something to consider. Microsoft recommends:
Avoid LINQ, instead construct queries from strings using QueryDefinition
Using that, the query would look like this:
var query = new QueryDefinition("SELECT * FROM c WHERE c.category = @category")
.WithParameter("@category", "Electronics");
NOTICE CAMEL CASE!
Now let say you go to your entity that represent the JSON and you do the rename on the property (for a reason, like better context when reading bare data). Now you must remember to go back to the query and update it!
What if you would utilize nameof
to build your queries, and thus support property rename seamlessly? The query would look like this
var query = new QueryDefinition($"SELECT * FROM c WHERE c.{nameof(EntityName.Category)} = @category")
.WithParameter("@category", "Electronics");
NOTICE Default PascalCase! would be generated.
Sure you can use [JsonProperty("someName")]
and if you would rename the property in the entity class it would not affect the query from the first example, but then what if you must rename the property of JSON output? Now you have to change in 2 places in [JsonProperty()] and in the query of the first example.
So my strategy moving forward is:
Default
naming and that is PascalCase (or if you really want camelCase JSON in the DB, break the C# style and for DB entities only use camelCase and then use AutoMapper to map to PascalCase DTOs)[JsonProperty("someName")]
or you will have to rename in both places.nameof
to specify property names to strongly tie to entity property name, for easy renames and "Find All References" feature of the Visual StudioLet me know what you think in the comments section. Thanks!
Upvotes: 0
Reputation: 7200
There is no recommended approach or guidelines on naming. id
is lowercase because it is a system property and _ts
is prefixed with an underscore because it is a system meta property. The rest is completely up to you do decide.
It comes down to how your application serialises and deserialises the data when it converts them from object to Json and visa versa. Keep in mind that the way your store the data in terms of naming is part of a contract with all the applications that consume it, so as long as you are consistent with how you are serialising your objects the case doesn't matter.
Upvotes: 4