Created
August 20, 2010 09:22
-
-
Save ArnisL/539954 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
namespace Interreg.Config{ | |
using System; | |
using System.IO; | |
using Domain; | |
using FluentNHibernate.Automapping; | |
using FluentNHibernate.Cfg; | |
using FluentNHibernate.Cfg.Db; | |
using FluentNHibernate.Conventions.Helpers; | |
using NHibernate; | |
using NHibernate.Context; | |
using NHibernate.Tool.hbm2ddl; | |
using Persistence; | |
using StructureMap; | |
public class NHibernateBootstrap{ | |
public static ISessionFactory SpinIt(IContainer container){ | |
var factory=SpinIt(); | |
container.Inject(factory); | |
return factory; | |
} | |
private static AutoPersistenceModel GetMappingConfiguration(){ | |
var storeCfg=new StoreConfiguration(); | |
var m=new AutoPersistenceModel(storeCfg); | |
m.AddEntityAssembly(typeof(Project).Assembly); | |
m.Conventions.Add(DefaultCascade.All()); | |
m.Conventions.Add(new EnumerationTypeConvention()); | |
return m; | |
} | |
public static ISessionFactory SpinIt(){ | |
var dbpath=Path.Combine(Environment.CurrentDirectory,"db.s3"); | |
var factory=Fluently.Configure() | |
.Database( | |
SQLiteConfiguration.Standard.UsingFile(dbpath).ConnectionString(string.Format( | |
"Data Source={0};Version=3;Pooling=True;Max Pool Size=1;",dbpath))) | |
.Mappings(m=>m.AutoMappings.Add(GetMappingConfiguration())) | |
.ExposeConfiguration( | |
cfg=>{ | |
if(!File.Exists(dbpath)) | |
new SchemaExport(cfg).Create(true,true); | |
cfg.SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass, | |
typeof(ThreadStaticSessionContext).AssemblyQualifiedName); | |
}) | |
.BuildSessionFactory(); | |
return factory; | |
} | |
public static ISessionFactory SpinItForTests(){ | |
var dbpath=Path.Combine(Environment.CurrentDirectory,"test_db.s3"); | |
var factory=Fluently.Configure() | |
.Database( | |
SQLiteConfiguration.Standard.UsingFile(dbpath).ConnectionString( | |
string.Format("Data Source={0};Version=3;",dbpath))) | |
.Mappings(m=>m.AutoMappings.Add(GetMappingConfiguration())) | |
.ExposeConfiguration( | |
cfg=>{ | |
new SchemaExport(cfg).Create(true,true); | |
cfg.SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass, | |
typeof(ThreadStaticSessionContext).AssemblyQualifiedName); | |
}) | |
.BuildSessionFactory(); | |
return factory; | |
} | |
} | |
public class StoreConfiguration:DefaultAutomappingConfiguration{ | |
public override bool ShouldMap(Type type){ | |
return type.BaseType!=typeof(Enumeration)&& | |
type.BaseType!=typeof(Exception); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment