This repository has been archived by the owner on Aug 16, 2018. It is now read-only.
forked from sharparchitecture/Sharp-Architecture
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NHibernateSessionFactoryBuilder, see sharparchitecture#61
Also fixes sharparchitecture#54
- Loading branch information
Showing
10 changed files
with
342 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
Solutions/SharpArch.NHibernate/NHibernateSessionFactoryBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
namespace SharpArch.NHibernate | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Domain; | ||
using global::FluentNHibernate.Automapping; | ||
using global::FluentNHibernate.Cfg; | ||
using global::FluentNHibernate.Cfg.Db; | ||
using global::NHibernate; | ||
using global::NHibernate.Cfg; | ||
using NHibernateValidator; | ||
|
||
/// <summary> | ||
/// Creates NHibernate SessionFactory <see cref="ISessionFactory" /> | ||
/// </summary> | ||
/// <remarks> | ||
/// Transient object, session factory must be redistered as singletone in DI Container. | ||
/// </remarks> | ||
public class NHibernateSessionFactoryBuilder | ||
{ | ||
/// <summary> | ||
/// Default configuration file name. | ||
/// </summary> | ||
public const string DefaultNHibernateConfigFileName = "Hibernate.cfg.xml"; | ||
|
||
private INHibernateConfigurationCache configurationCache; | ||
private readonly string configurationName = "Default"; | ||
private AutoPersistenceModel autoPersistenceModel; | ||
private string configFile; | ||
private readonly List<string> mappingAssemblies; | ||
private IPersistenceConfigurer persistenceConfigurer; | ||
private IDictionary<string, string> properties; | ||
private bool useDataAnnotationValidators; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NHibernateSessionFactoryBuilder"/> class. | ||
/// </summary> | ||
public NHibernateSessionFactoryBuilder() | ||
{ | ||
this.configurationCache = NullNHibernateConfigurationCache.Null; | ||
this.mappingAssemblies = new List<string>(); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Creates the session factory. | ||
/// </summary> | ||
/// <returns> NHibernate session factory <see cref="ISessionFactory"/>.</returns> | ||
public ISessionFactory CreateSessionFactory() | ||
{ | ||
var configuration = BuildConfiguration(); | ||
|
||
return configuration.BuildSessionFactory(); | ||
} | ||
|
||
|
||
public Configuration BuildConfiguration() | ||
{ | ||
var configuration = configurationCache.LoadConfiguration(configurationName, configFile, mappingAssemblies); | ||
|
||
if (configuration == null) | ||
{ | ||
configuration = LoadExternalConfiguration(); | ||
configuration = ApplyCustomSettings(configuration); | ||
configurationCache.SaveConfiguration(configurationName, configuration); | ||
} | ||
return configuration; | ||
} | ||
|
||
public NHibernateSessionFactoryBuilder UseConfigurationCache(INHibernateConfigurationCache configurationCache) | ||
{ | ||
Check.Require(configurationCache != null, "Please provide configuration cache instance."); | ||
this.configurationCache = configurationCache; | ||
return this; | ||
} | ||
|
||
public NHibernateSessionFactoryBuilder UseMappingAssemblies(IEnumerable<string> mappingAssemblies) | ||
{ | ||
Check.Require(mappingAssemblies != null, "Please specify mapping assemblies."); | ||
this.mappingAssemblies.AddRange(mappingAssemblies); | ||
return this; | ||
} | ||
|
||
public NHibernateSessionFactoryBuilder UseAutoPersitenceModel(AutoPersistenceModel autoPersistenceModel) | ||
{ | ||
Check.Require(autoPersistenceModel != null); | ||
this.autoPersistenceModel = autoPersistenceModel; | ||
return this; | ||
} | ||
|
||
public NHibernateSessionFactoryBuilder UseProperties(IDictionary<string, string> properties) | ||
{ | ||
Check.Require(properties != null); | ||
this.properties = properties; | ||
|
||
return this; | ||
} | ||
|
||
|
||
public NHibernateSessionFactoryBuilder UseDataAnnotationValidators(bool addDataAnnotatonValidators) | ||
{ | ||
this.useDataAnnotationValidators = addDataAnnotatonValidators; | ||
return this; | ||
} | ||
|
||
public NHibernateSessionFactoryBuilder UseConfigFile(string nhibernateConfigFile) | ||
{ | ||
Check.Require(!string.IsNullOrEmpty(nhibernateConfigFile), "NHibernate config file must be specified"); | ||
configFile = nhibernateConfigFile; | ||
|
||
return this; | ||
} | ||
|
||
public NHibernateSessionFactoryBuilder UsePersistenceConfigurer(IPersistenceConfigurer persistenceConfigurer) | ||
{ | ||
Check.Require(persistenceConfigurer != null); | ||
this.persistenceConfigurer = persistenceConfigurer; | ||
return this; | ||
} | ||
|
||
private Configuration ApplyCustomSettings(Configuration cfg) | ||
{ | ||
var fluentConfig = Fluently.Configure(cfg); | ||
if (persistenceConfigurer != null) | ||
{ | ||
fluentConfig.Database(persistenceConfigurer); | ||
} | ||
|
||
fluentConfig.Mappings(m => | ||
{ | ||
foreach (string mappingAssembly in this.mappingAssemblies) | ||
{ | ||
Assembly assembly = Assembly.LoadFrom(MakeLoadReadyAssemblyName(mappingAssembly)); | ||
m.HbmMappings.AddFromAssembly(assembly); | ||
m.FluentMappings.AddFromAssembly(assembly).Conventions.AddAssembly(assembly); | ||
} | ||
if (autoPersistenceModel != null) | ||
{ | ||
m.AutoMappings.Add(autoPersistenceModel); | ||
} | ||
}); | ||
|
||
if (this.useDataAnnotationValidators) | ||
{ | ||
fluentConfig.ExposeConfiguration( | ||
e => | ||
{ | ||
e.EventListeners.PreInsertEventListeners = InsertFirst(e.EventListeners.PreInsertEventListeners, | ||
new DataAnnotationsEventListener()); | ||
e.EventListeners.PreUpdateEventListeners = InsertFirst(e.EventListeners.PreUpdateEventListeners, | ||
new DataAnnotationsEventListener()); | ||
}); | ||
} | ||
return fluentConfig.BuildConfiguration(); | ||
} | ||
|
||
/// <summary> | ||
/// Loads configuration from properties dictionary and from external file if avaiable. | ||
/// </summary> | ||
/// <returns></returns> | ||
private Configuration LoadExternalConfiguration() | ||
{ | ||
var cfg = new Configuration(); | ||
if (properties != null && properties.Any()) | ||
{ | ||
cfg.AddProperties(properties); | ||
} | ||
if (!string.IsNullOrEmpty(configFile)) | ||
{ | ||
return cfg.Configure(configFile); | ||
} | ||
if (File.Exists(DefaultNHibernateConfigFileName)) | ||
{ | ||
return cfg.Configure(); | ||
} | ||
return cfg; | ||
} | ||
|
||
private static T[] InsertFirst<T>(T[] array, T item) | ||
{ | ||
if (array == null) | ||
{ | ||
return new[] {item}; | ||
} | ||
|
||
var items = new List<T> {item}; | ||
items.AddRange(array); | ||
return items.ToArray(); | ||
} | ||
|
||
private static string MakeLoadReadyAssemblyName(string assemblyName) | ||
{ | ||
return (assemblyName.IndexOf(".dll", StringComparison.OrdinalIgnoreCase) == -1) | ||
? assemblyName.Trim() + ".dll" | ||
: assemblyName.Trim(); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
Solutions/SharpArch.NHibernate/NullNHibernateConfigurationCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace SharpArch.NHibernate | ||
{ | ||
using System.Collections.Generic; | ||
using global::NHibernate.Cfg; | ||
|
||
/// <summary> | ||
/// Null Object for configuration cache. | ||
/// </summary> | ||
class NullNHibernateConfigurationCache : INHibernateConfigurationCache | ||
{ | ||
internal NullNHibernateConfigurationCache() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Instance. | ||
/// </summary> | ||
public static readonly INHibernateConfigurationCache Null = new NullNHibernateConfigurationCache(); | ||
|
||
public Configuration LoadConfiguration(string configKey, string configPath, IEnumerable<string> mappingAssemblies) | ||
{ | ||
return null; | ||
} | ||
|
||
public void SaveConfiguration(string configKey, Configuration config) | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.