Table of contents
- 1 Configuring ActiveRecord
- 2 Initializing ActiveRecord
- 3 Creating the tables
Initializing the Framework
Castle ActiveRecord must be initialized before you use any class. This should be done only once and preferably at application start up. In our case, as our example is a WinForms application, we would initialize ActiveRecord before show any form.
ActiveRecord also needs a small configuration to tell it which database you are using and how to speak to it. In fact this is NHibernate configuration that ActiveRecord pass on when it initializes NHibernate. So it is time to decide where you want to keep this configuration.
Configuring ActiveRecord
We have some option on how to configure ActiveRecord and where to keep the configuration. Those are detailed explained on the documentation. For simplicity's sake we would use a simple standalone xml file:
<?xml version="1.0" encoding="utf-8" ?> <activerecord> <config> <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect" /> <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="hibernate.connection.connection_string" value="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" /> </config> </activerecord>
The configuration above uses MS SQL Server 2005. If you are using a different database you need to change the dialect and driver. Please refer to the Xml Configuration Reference document for more information.
Now save the file (on the folder to where the assembly is generated) with the content above as appconfig.xml. The following code creates a XmlConfigurationSource that loads the configuration above:
namespace BlogSample { using System.Windows.Forms; using BlogSample.UI; using Castle.ActiveRecord; using Castle.ActiveRecord.Framework.Config; public class App { public static void Main() { XmlConfigurationSource source = new XmlConfigurationSource("appconfig.xml"); } } }
Initializing ActiveRecord
Finally we will initialize ActiveRecord passing the configuration loaded. We also need to specify the types ActiveRecord should inspect. We can do this by specifying an assembly, an array of assemblies or an array of types. For simplicity's sake, let's use an array of types:
namespace BlogSample { using System.Windows.Forms; using BlogSample.UI; using Castle.ActiveRecord; using Castle.ActiveRecord.Framework.Config; public class App { public static void Main() { XmlConfigurationSource source = new XmlConfigurationSource("appconfig.xml"); ActiveRecordStarter.Initialize( source, typeof(Blog), typeof(Post), typeof(User) ); } } }
At this point we are ready to use our type (as long as the configuration is right and the database exists).
Creating the tables
You may let ActiveRecord create the tables if you have not created it:
public class App { public static void Main() { XmlConfigurationSource source = new XmlConfigurationSource("appconfig.xml"); ActiveRecordStarter.Initialize( source, typeof(Blog), typeof(Post), typeof(User) ); if (MessageBox.Show("Do you want to let ActiveRecord create the database tables?", "Schema", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { ActiveRecordStarter.CreateSchema(); } } }
Now that our hands are partially dirty, we can start our real code that use our classes. We can include more methods on our classes to perform more database related operations.
Proceed with using the classes.