Elfocrash / Cosmonaut

🌐 A supercharged Azure CosmosDB .NET SDK with ORM support
https://cosmonaut.readthedocs.io
MIT License
342 stars 44 forks source link

Recommended practice for string id / guid id usage #93

Closed DennisBaekgaard closed 5 years ago

DennisBaekgaard commented 5 years ago

I tried searching around a little and couldn't find any information. I understand that CosmosDB enforces a string id property (for reasons unknown), but what is the recommended practice then? Was considering something like this:

        [JsonProperty("id")]
        public string CosmosId { get; set; }
        public Guid Id { get { return Guid.Parse(CosmosId); } set { CosmosId = value.ToString(); Id = value; };

But that will get proper confusing with queries I'd suppose. I do kind a want the type security though.

Elfocrash commented 5 years ago

This will work indeed but you can simplify this to just:

Yeah if you need to have a Guid as the Id you deal with in the code then what you pasted will work. The Id by default will be initialized as a string representation of a Guid anyway so if you don't set it will get initialized with Guid.NewGuid()

DennisBaekgaard commented 5 years ago

Awesome, thank you!