none
Instantiating a new Document in DocumentDB RRS feed

  • Question

  • In order to create a generic base class that can add/update/delete documents, I created a base class that takes a document. I then took my standard objects, and created a version that inherited from Document. This works fine except that when I instantiate a new document as a type of document, AttachmentsLink seems to default to null, and causes an error. Is there a way to set this (it does not set now) or have it not default to throwing null? I am not adding any attachments to my document, but because it is expecting something there (i.e. not handling null by default) it is blowing up.

    Monday, May 23, 2016 10:21 PM

All replies

  • Hi,

    Are you inheriting directly from Document? Are you using C#?

    We have a similar approach but with normal custom classes and using Generics (C#) for Add/Update/Delete methods.

    This is a very simple DocumentDB provider to be used as reference:

    public class DocumentDbProvider{
    
    	private Uri _collectionUri;
    	private string _databaseServiceUri;
    	private string _databaseAccessKey;
    	private string _databaseName;
    	private string _collectionName;
    	public DocumentDbProvider(string serviceUri, string serviceKey, string databaseName, string collectionName)
    	{
    		_databaseServiceUri=serviceUri;
    		_databaseAccessKey = serviceKey;
    		_databaseName=databaseName;
    		_collectionName = collectionName;
    		_collectionUri = UriFactory.CreateDocumentCollectionUri(databaseName, collectionName);
    	}
    	private  DocumentClient _dbClient;
    	private  DocumentClient GetClient()
    	{
    		if (_dbClient != null) return _dbClient;
    		_dbClient = new DocumentClient(_databaseServiceUri, _databaseAccessKey);
    		return _dbClient;
    	}
    	private Uri GetDocumentLink(string id)
    	{
    		return UriFactory.CreateDocumentUri(_databaseName,_collectionName, id);
    	}
    	public  string AddItem<T>(T document)
    	{
    		var client = GetClient();
    		var result = client.CreateDocumentAsync(_collectionUri, document).Result;
    		return result.Resource.Id;
    	}
    	public string UpdateItem<T>(T document, string id)
    	{
    		var client = GetClient();
    		var result = client.ReplaceDocumentAsync(GetDocumentLink(id), document).Result;
    		return result.Resource.Id;
    	}
    	public string DeleteItem(string id)
    	{
    		var client = GetClient();
    		var result = client.DeleteDocumentAsync(GetDocumentLink(id)).Result;
    		return result.Resource.Id;
    	}
    	public  IQueryable<T> CreateQuery<T>(FeedOptions feedOptions)
    	{
    		var client = GetClient();
    		return client.CreateDocumentQuery<T>(_collectionUri, feedOptions);
    	}
    }

    Taking a scenario with base and inherited classes:

    public abstract class Vehicle
    {
    	public Vehicle(string type)
    	{
    		this.Type = type;
    	}
    	
    	public string id { get; set; }
    	
    	public string Type { get; private set; }
    }
    
    public class Car:Vehicle
    {
    	public Car() : base("car") { }
    	
    	public string SomeOtherProperty(){get;set;}
    }
    
    public class Bus:Vehicle
    {
    	public Bus() : base("bus") { }
    	
    	public string SomeDifferentProperty(){get;set;}
    	
    	public string MoreProperties(){get;set;}
    }


    You can use generic operations with the same provider:

    var _provider = new DocumentDbProvider("https://yourservice.documents.azure.com:443/","YourServiceKey", "mydatabase", "mycollection");
    var mycar = new Car();
    //set the car properties
    var newcarid = _provider.AddItem(mycar);
    var mybus = new Bus();
    //set bus properties
    var newbusid = _provider.AddItem(mybus);
    //find a car
    var aCar = _provider.CreateQuery<Car>(new FeedOptions() { MaxItemCount = 1 }).Where(x => x.id == newcarid);
    aCar.SomeOtherProperty = "1";//change values
    _provider.UpdateItem(aCar, newcarid);

    This is just an example, but shows that you can use Generics and inherited classes without needing to inherit from the Document class.

    Hope it helps!

    Tuesday, May 24, 2016 5:00 PM