Step 2 - Using The compiled DAL In Your Application
Reference the generated DAL dll from your application.
Create or upgrade a database
The following code either creates a new database or upgrades it if it already exists. In this example, we are going to create a SQL Server 2000 database. We'll need to provide a bog standard SQL Server connection string to tell the DAL which server and database to deal with.
The first two lines of code are the ONLY place that you will need to deal with a specific database type. From this point on, you will be dealing with objects that implement the IODSInActionDALDataStoreProvider interface rather than any specific data store provider.
// A SQL Server connection string for an ODSInAction database
string connectionString = "data source=mySQLServer; initial catalog=ODSInAction;" +
" integrated security=true;";
// Create a data store provider object
IODSInActionDALDataStoreProvider dataStore = new
SQLServerDataStoreProvider(connectionString);
// Does the database already exist?
if (dataStore.Exists)
{
// The database already exists. Make sure the schema is upto-date
dataStore.Upgrade();
}
else
{
// The database does not exist. Create a new database
dataStore.Create();
}
Store and Retrieve objects
The following code can be used to create a new Product object, add it to the database and then retrieve all existing Product objects from the database:
// Add a new Product object to the database. Firstly, create a new Product
Product product = new Product("Product1", "The description of product1", 25f, 20f);
// Add the new product object to the database
Product.Add(product, dataStore);
// Retrieve all Product objects from the database
List<DataProduct> products = Product.GetAll(dataStore);
Results
Running our application results in a new database being created and a new Product object being stored in it. In this case, the connection string points us to a SQL Server 2005 server.
Conclusions
The example we've demonstrated includes a fully featured DAL and can be put together in a matter of a couple of minutes. Hand coding the DAL would have taken considerably longer, most likely hours. Imagine the time that would be saved developing a much larger application. We hope that this simple demonstration illustrates the huge time saving potential of generating your DAL using ODS. For example source code generated by ODS see the example source code page.