Skip to content

06. Creating contexts manually

Akos Nagy edited this page Jun 12, 2018 · 1 revision

Creating contexts manually

When using the static driver, you have to create the data contexts manually. What this means is that for Entity Framework, for example, you have to create your typed DataContext descendant class, build it into an assembly, then import the assembly when creating a connection in LinqPad and finally select your type. The procedure is the same for this extension.

Get the driver base library

Download the driver base library from here and use it to build your typed contexts.

Create your typed context

The context class

Create a class that inherits the CosmosDbContext.CosmosDbContext class:

public class VehicleContext : CosmosDbContext.CosmosDbContext
{
  public VehicleContext() : base("https://localhost:8081", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", "VehicleDb") { }

}

The constructor parameters are: service url, service key and database name.

Add collections

Then add your collections to the class by adding properties of type CosmosDbCollection<T>:

public class VehicleContext : CosmosDbContext.CosmosDbContext
{
  public VehicleContext() : base("https://localhost:8081", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", "VehicleDb") { }

  [CosmosDbCollection(CollectionName = "vehicles")]
  public CosmosDbCollection<Vehicle> Vehicles { get; set; }

}

Don't forget to add the CosmosDbCollection attribute! You can also specify the collection name in the attribute (by default the name of the property is used).

If you're collection contains untyped data (i.e. no T for the type parameter), you have to use the ICosmosDbCollection<dynamic> interface as the property type:

public class VehicleContext : CosmosDbContext.CosmosDbContext
{
  public VehicleContext() : base("https://localhost:8081", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", "VehicleDb") { }

  [CosmosDbCollection(CollectionName = "vehicles")]
  public ICosmosDbCollection<dyanmic> Vehicles { get; set; }

}

Add stored procedures

Stored procedures are also supported. Just create your method that return IEnumerable<T> (or IEnumrable<dynamic> for the untyped scenario) and inside the method call the ExecuteStoredProcedure<T> method (or ExecuteDynamicStoredProcedure<dynamic> for the untyped scenario):

public class VehicleContext : CosmosDbContext.CosmosDbContext
{
  public VehicleContext() : base("https://localhost:8081", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", "VehicleDb") { }
       

  public IEnumerable<Vehicle> GetVehicles()
  {
    var result = base.ExecuteStoredProcedure<Vehicle>("mySpecialSp", "Vehicles");
    return result;
  }
  
  public IEnumerable<dynamic> GetVehiclesUntyped()
  {
    var result = base.ExecuteDynamicStoredProcedure<dynamic>("mySpecialSp", "Vehicles");
    return result;
  }
}

Parameters are: name of the stored procedure and name of the collection that the stored procedure belongs to.

Ready to use

Build your assembly with this type, then, when creating a connection with the static driver, browse to this assembly, open it, select your type and knock yourself out!

More information

For more information you can read my blogpost on creating the driver backend package here.

Clone this wiki locally