StorageContext

All data access in Iridium is ultimately done via a StorageContext object. Multiple storage contexts can exist, each one pointing to a different database.

Creating a StorageContext

A StorageContext object needs a data provider as the only constructor parameter. Data providers implement the IDataProvider interface.

public class StorageContext
{
    public StorageContext(IDataProvider dataProvider) { ... }
}
var dbContext = new StorageContext(new SqliteDataProvider(dbName));

Using dependency injection

You can also use dependency injection to create a storage context. In some applications, it's the preferred way:

// register the data provider
ServiceLocator.Register(new SqliteDataProvider(dbName);
// create a storage context. The previously registered data provider will be used
var dbContext = ServiceLocator.Create<StorageContext>();

The StorageContext class contains all methods to access data.

Subclassing StorageContext

When subclassing StorageContext, DataSets can be declared in the class for specific tables. These fields will be assigned when the StorageContext is created.

public class MyContext : StorageContext
{
    public MyContext(IDataProvider dataProvider) : base(dataProvider)
    {
    }

    public IDataSet<Customer> Customers;
    public IDataSet<Order> Orders;
    public IDataSet<OrderItem> OrderItems;
}

This is equivalent to:

public class MyContext : StorageContext
{
    public MyContext(IDataProvider dataProvider) : base(dataProvider)
    {
          Customers = DataSet<Customer>();
          Orders = DataSet<Order>();
          OrderItem = DataSet<OrderItem>();
    }

    public IDataSet<Customer> Customers;
    public IDataSet<Order> Orders;
    public IDataSet<OrderItem> OrderItems;
}