Skip to content

Instantly share code, notes, and snippets.

@davetheninja
Created September 25, 2009 15:48
Show Gist options
  • Save davetheninja/193646 to your computer and use it in GitHub Desktop.
Save davetheninja/193646 to your computer and use it in GitHub Desktop.
public class FluentNHibernateFacility : NHibernateFacility
{
protected override void Init()
{
var connectionString = FacilityConfig.Attributes["connection_string"];
if (string.IsNullOrEmpty(connectionString))
throw new ConfigurationErrorsException("No connection string has been supplied. Please supply a connection string.");
var factoryConfig = BuildFactory(connectionString);
RegisterNewConfiguration(factoryConfig);
base.Init();
}
private IConfiguration BuildFactory(string connectionString)
{
SetConfigurationBuilder(typeof(FluentNHibernateConfigurationBuilder));
var factoryConfig = BuildFactoryCore(connectionString);
return factoryConfig;
}
private void SetConfigurationBuilder(Type configurationBuilderType)
{
var configurationBuilder = FacilityConfig.Attributes["configurationBuilder"];
if (string.IsNullOrEmpty(configurationBuilder))
FacilityConfig.Attributes.Add("configurationBuilder", configurationBuilderType.AssemblyQualifiedName);
}
protected virtual IConfiguration BuildFactoryCore(string connectionString)
{
var factoryConfig = new MutableConfiguration("factory");
factoryConfig.Attributes.Add("id", "nhibernate.factory");
SetSettings(factoryConfig, connectionString);
return factoryConfig;
}
private static void SetSettings(IConfiguration configuration, string connectionString)
{
var settingsConfig = new MutableConfiguration("settings");
RegisterDatabase(settingsConfig, connectionString);
configuration.Children.Add(settingsConfig);
}
private static void RegisterDatabase(IConfiguration configuration, string connectionString)
{
AddChildToConfiguration(configuration, "connection.provider", "NHibernate.Connection.DriverConnectionProvider");
RegisterDatabaseDetails(configuration, "NHibernate.Driver.SqlClientDriver", "NHibernate.Dialect.MsSql2005Dialect", connectionString);
AddChildToConfiguration(configuration, "default_schema", "dbo");
AddChildToConfiguration(configuration, "show_sql", "false");
AddChildToConfiguration(configuration, "proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
}
private static void RegisterDatabaseDetails(IConfiguration configuration, string driver, string dialect, string connectionString)
{
AddChildToConfiguration(configuration, "connection.driver_class", driver);
AddChildToConfiguration(configuration, "dialect", dialect);
AddChildToConfiguration(configuration, "connection.connection_string", connectionString);
}
private static void AddChildToConfiguration(IConfiguration configuration, string key, string value)
{
var item = new MutableConfiguration("item", value);
item.Attribute("key", key);
configuration.Children.Add(item);
}
private void RegisterNewConfiguration(IConfiguration factoryConfig)
{
FacilityConfig.Children.Add(factoryConfig);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment