by Mikael Henriksson
25. July 2009 15:09
To start with I’d like to state (and people might shoot me for this) that NHibernate has as many issues as any other framework I’ve laid my hands on. As I said before it has it’s pro’s and con’s. In the following examples I will be using FluentNhibernate for the simple reason it is so simple to use :)
To use NHibernate in any form successfully you would need a whole bunch of managers. I will start with the configuration and I’ll try being as DRM (don’t repeat myself) as I can.
They sure made stuff fluent with FNH. And in NHibernate 2.1 the configuration part is fluent. What does a fluent API mean? See Alex James blog poston the subject! The fluent part is all well but only to a certain extent. After that I want to have a bit better control over things for obvious reasons. With FNH you can configure your data access and just return a session to use for saving, retrieving or deleting data. This is fine in a simple example but I’d like to have a more multipurpose configuration and what if the application grows on me. I have had a look at the Sharp-Architecture and it looks very nice but what is the fun of using a complete framework? I want to create my own :)
The class for handling the configuration will be named ConfigurationManager is there a better name?
It will just return an NHibernate configuration for starters but I’ll add a couple of extra methods to it.
internal static class ConfigurationManager
{
private static readonly string PathToSqlLiteDb =
System.Configuration
.ConfigurationManager.AppSettings["PathToSqlLiteDb"];
internal static Configuration GetConfiguration()
{
return GetFluentConfiguration().BuildConfiguration();
}
internal static void UpdateSchema()
{
GetFluentConfiguration().ExposeConfiguration(BuildSchema);
}
private static void BuildSchema(Configuration config)
{
if (File.Exists(PathToSqlLiteDb))
{
new SchemaUpdate(config);
}
new SchemaExport(config).Create(true, true);
}
private static FluentConfiguration GetFluentConfiguration()
{
FluentConfiguration fluentConfig = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.UsingFile(PathToSqlLiteDb))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Entity>());
return fluentConfig;
}
}
I feel this is a pretty good start. Now I’ll see if I can get a grip on the session management. Since this is a web application I suppose it will be a bit more difficult. I somehow need to create a session per user if I am not mistaken.