0

I have the following code which takes in a object of type employee based on it's model, I want to convert this to a DocumentDB Document then post to the database. How would I do the conversion?

[HttpPost]
        public async Task Post([FromBody]Employee employee)
        {
            using (_logger.BeginScope("Post employee"))
            {
                try
                {
                    // convert employee to Document??
                    await _documentDbRepository.CreateItemsAsync(document);
                }
                catch (Exception e)
                {
                    _logger.LogError(e.Message);
                    throw;
                }

            }
        }
DarkW1nter
  • 2,933
  • 11
  • 67
  • 120
  • Does the signature for `CreateItemsAsync` explicitly require you to pass in a `Document`? This appears to be some custom abstraction you have over the actual DocumentClient which is capable of taking just about any `object` and inserting it into Cosmos using default JSON serialization – Jesse Carter Nov 02 '17 at 11:38
  • The CreateItemsAsync does for the moment require a Document, so you mean it can accept in a type 'object' and this would work whether it's a Document or a custom object based on a model? – DarkW1nter Nov 02 '17 at 11:47
  • What is `_documentDbRepository`? It sounds like a custom layer that you have built on top. If it's hard coded for Document then you're going to have to respect that t the moment. The regular DocumentClient can accept any objects, not just Documents. – Jesse Carter Nov 02 '17 at 11:49
  • changed it to accept in an object, so as long as I create a new id guid for each thing being passed in it works fine. If you want to put your original comment as an answer i'll accept, thanks – DarkW1nter Nov 02 '17 at 11:57

1 Answers1

1

It seems that you're using a custom layer over top of the regular Cosmos libraries which is hard coded to only accept a Document. The libraries supplied by Microsoft are capable of inserting any generic object and will use default JSON serialization to turn it into a Document for you at insert time. Changing the signature on your custom repository to accept Object instead of Document should get you unblocked.

Jesse Carter
  • 20,062
  • 7
  • 64
  • 101