0
votes

I have ObjectModel that contains for example, 2 class Student and Course .

I want to use documentDB for save that 2 collection (one collection for courses and one for students) I need to create 2 diffrent collection in my documentDB DB ? , Or may I can use only one Collection?

Another problem when I want to convert from document to ObjectModel.Student , I don't want to use dynamic casting (real time performance issue).

public static async Task<ObjectModel.Student> GetGroupAsyncByID(string id)
    {
        try
        {
            Document document = await _client.ReadDocumentAsync(UriFactory.CreateDocumentUri(cDatabaseId, cAllCollections[0], id));
            return (ObjectModel.Student)document; // compile error 
        }
        catch (DocumentClientException e)
        {
            if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                return null;
            }
            else
            {
                throw;
            }
        }
    }

My Student Class

public class Student 
    {
        #region Fields
        [JsonProperty(PropertyName = "id")]
        public string ID { get; set; }
        public String StudentName { get; set; }

}

How I can cast from Doucment to Student object?

Thanks!

1

1 Answers

0
votes

DocumentDB is schema-agonostic and yes, you can save both Course and Student object in one collection. To easily tell which class the Document should be cast to, you can define, for example, "Type" property in both objects and 1 for Course object and 2 for Student object.

The Document object implement the dynamics contract. You can cast to Student by Student s = (dynamics) document. The performance implication here is very minimal because document already contained the PropertyBag of JSON object, it will just set the property of "Student" to the value in PropertyBag.

If you like to handle this yourselves, ResourceResponse also expose the raw stream so that you can handle the deserialization youselves.