Allow configuration of BytecodeProvider with a LocalSessionFactory property

This commit is contained in:
lahma
2009-12-14 15:30:15 +00:00
parent 29410497a7
commit f3972d09d4
3 changed files with 46 additions and 12 deletions

View File

@@ -28,6 +28,7 @@ using System.Data.Common;
using Common.Logging;
using NHibernate;
using NHibernate.Bytecode;
using NHibernate.Cfg;
using NHibernate.Connection;
using NHibernate.Dialect;
@@ -114,10 +115,15 @@ namespace Spring.Data.NHibernate
private IResourceLoader resourceLoader;
private IApplicationContext applicationContext;
// Configuration time DB provider.
// This will not be available after configuration has been done.
private static IDbProvider configTimeDbProvider;
#if NH_2_1
private IBytecodeProvider bytecodeProvider;
#endif
#endregion
#region Constants
@@ -146,12 +152,9 @@ namespace Spring.Data.NHibernate
/// <summary>
/// Setting the Application Context determines were resources are loaded from
/// </summary>
IApplicationContext IApplicationContextAware.ApplicationContext
public IApplicationContext ApplicationContext
{
set
{
this.ResourceLoader = value;
}
set { this.applicationContext = value; }
}
/// <summary>
@@ -416,6 +419,19 @@ namespace Spring.Data.NHibernate
}
}
#if NH_2_1
/// <summary>
/// Sets custom byte code provider implementation to be used. This corresponds to setting
/// the <see cref="Environment.BytecodeProvider" /> property before NHibernate session factory
/// configuration.
/// </summary>
public virtual IBytecodeProvider BytecodeProvider
{
get { return this.bytecodeProvider; }
set { this.bytecodeProvider = value; }
}
#endif
#endregion
@@ -572,9 +588,14 @@ namespace Spring.Data.NHibernate
if (this.mappingResources != null)
{
IResourceLoader loader = this.ResourceLoader;
if (loader == null)
{
loader = this.applicationContext;
}
foreach (string resourceName in mappingResources)
{
config.AddInputStream(this.ResourceLoader.GetResource(resourceName).InputStream);
config.AddInputStream(loader.GetResource(resourceName).InputStream);
}
}
@@ -662,11 +683,22 @@ namespace Spring.Data.NHibernate
}
}
#if NH_2_1
#endif
// Perform custom post-processing in subclasses.
PostProcessConfiguration(config);
#if NH_2_1
if (BytecodeProvider != null)
{
// set custom IBytecodeProvider
Environment.BytecodeProvider = BytecodeProvider;
}
else
{
// use Spring's as default
// Environment.BytecodeProvider = new Bytecode.BytecodeProvider(this.applicationContext);
}
#endif
// Build SessionFactory instance.
log.Info("Building new Hibernate SessionFactory");
this.configuration = config;

View File

@@ -26,11 +26,13 @@ using NHibernate.Connection;
using NHibernate.Dialect;
using NHibernate.Driver;
using Spring.Context.Support;
using Spring.Data.Common;
using NUnit.Framework;
#if NH_2_1
using Spring.Data.NHibernate.Bytecode;
#endif
namespace Spring.Data.NHibernate
@@ -50,6 +52,8 @@ namespace Spring.Data.NHibernate
dbProvider.ConnectionString = "Data Source=(local);Database=Spring;Trusted_Connection=false";
LocalSessionFactoryObject sfo = new LocalSessionFactoryObject();
sfo.DbProvider = dbProvider;
sfo.ApplicationContext = new StaticApplicationContext();
IDictionary properties = new Hashtable();
properties.Add(Environment.Dialect, typeof(MsSql2000Dialect).AssemblyQualifiedName);
properties.Add(Environment.ConnectionDriver, typeof(SqlClientDriver).AssemblyQualifiedName);
@@ -70,6 +74,8 @@ namespace Spring.Data.NHibernate
#if NH_2_1
Assert.AreEqual(sfo.Configuration.Properties[Environment.ProxyFactoryFactoryClass], typeof(ProxyFactoryFactory).AssemblyQualifiedName);
// Spring's IBytecodeProvider should be the default
// Assert.AreEqual(typeof(BytecodeProvider), Environment.BytecodeProvider.GetType(), "default IBytecodeProvider was not Spring's BytecodeProvider");
#endif
}

View File

@@ -44,10 +44,6 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\lib\NHibernate21\net\2.0\Iesi.Collections.dll</HintPath>
</Reference>
<Reference Include="LinFu.DynamicProxy, Version=1.0.3.14911, Culture=neutral, PublicKeyToken=62a6874124340d6e, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\lib\NHibernate21\net\2.0\LinFu.DynamicProxy.dll</HintPath>
</Reference>
<Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\lib\NHibernate21\net\2.0\log4net.dll</HintPath>