This post is the beginning of the series about developing applications using Eloquera database. We are going to cover various topics: how to create databases, what is the best way to write applications with Eloquera, both for desktop and web applications.
We are not focussing only on the database part of the software, but will demonstrate how Eloquera database makes it easy to design and develop applications, even if the code related to data access is encapsulated into separate logical modules.
There is a simple application that we are going to develop, called Messenger. I have done the basic things, but it is not complete yet.
You can download the source code for the Messenger application using this link. It is a simple console application allowing ‘users’ to send messages to each other.
The application is written in C# using Visual Studio 2008, and requires .NET Framework 3.5. Here you can download Eloquera database (version 2.5 beta) , which we have used to create the application. As the database is in the beta stage, the documentation is not completely ready, but I hope the examples in the Messenger app will be extremely easy to understand, and quite self-descriptive.
All right, let’s start now.
Messenger application – database installation script
First, let’s get the app running on your machine.
Let’s run a database script. That is a simple console application called DbInstall, and included into Messenger solution.
Eloquera database can easily create databases and add types in the main application. Why do we need any database script in this case?
There are many reasons for that, e.g., you can run queries without prior checking if the type is present in the database (if you run the query against the type absent in the database, you will get an exception). The most important thing is that in the future versions of Eloquera only database administrators will be able to create databases and register types, while other authenticated users will be able to add, modify and query data only.
Registering types
What is the database script for Eloquera DB?
Just a few lines like this:
1: // Now we can create the database.
2: db.CreateDatabase(MessengerDbName);
3: db.OpenDatabase(MessengerDbName);
4:
5: // And register all the types.
6: db.RegisterType(typeof(Eloquera.Messenger.Objects.User));
7: db.RegisterType(typeof(Eloquera.Messenger.Objects.Message));
8: db.RegisterType(typeof(Eloquera.Messenger.Objects.Subscription));
The code sample above creates a database, opens it, registers the types you are going to use, and that’s typical database script.
Registering types is a good thing, because you don’t have to worry about getting exceptions when running queries against such types anymore.
Adding data
The installation script in Messenger application does more than that, actually. The code below adds some sample data into the newly created database as well.
1: // Add sample users
2: var userSam = new Eloquera.Messenger.Objects.User() { Name = "Sam"}; 3: var userWonka = new Eloquera.Messenger.Objects.User() { Name = "Willy Wonka" }; 4: var userMacquarie = new Eloquera.Messenger.Objects.User() { Name = "Lachlan Macquarie" }; 5: var userCervantes = new Eloquera.Messenger.Objects.User() { Name = "Miguel de Cervantes de Saavedra" };
Adding data may be helpful for testing, or just to initialise basic objects like an initial user account for a web application, e.g., an account for the administrator with the default password.
Changing definitions
Software development is about changing the code all the time. Adding new methods to the classes registered in the database, won’t break anything. But adding new fields and properties (with no [Ignore] attribute) to such classes will result in the EloqueraException exception with the message “Definition in database is different to one on the client”, thrown by Eloquera client. Eloquera database performs integrity checks of your data, and prevents data loss or misinterpretations when class definition changes (EloqueraException is thrown as a result.)
The database script registers all the types all over again, so, it can delete the old database and create a database with the updated types if your classes have changed during development. The code sample below shows implementation of this procedure:
1: // -f flags denotes forcible re-creation of the database.
2: if (args.Length > 0 && args[0] == "-f")
3: { 4: try
5: { 6: db.DeleteDatabase(MessengerDbName, true);
7: }
8: catch (Exception e)
9: { 10: Console.WriteLine("Cannot delete the database file. Check if the file is in use.\nThe error occured:\n{0}", e.Message); 11: return;
12: }
13: }
The script checks the command-line parameter, and if the -f flag is specified, tries to remove the database.
This code uses DeleteDatabase() method introduced in Eloquera DB 2.5. The first parameter specifies the name of the database to be deleted. The second parameter forces the database drop all open connections, and then deletes the database.