Imports FluentNHibernate.Cfg Imports FluentNHibernate.Cfg.Db Imports FluentNHibernate.Mapping Imports NHibernate Imports NHibernate.Tool.hbm2ddl Imports System.Data.SQLite Public Class SQLiteDatabaseScope(Of TClassFromMappingAssembly) Implements IDisposable Private Const CONNECTION_STRING As String = "Data Source=:memory:;Version=3;New=True;" Public Sub New() BuildConfiguration() End Sub Private m_Connection As SQLiteConnection Private m_SessionFactory As ISessionFactory Private Sub BuildConfiguration() m_SessionFactory = Fluently.Configure() _ .Database(GetDBConfig()) _ .Mappings(AddressOf GetMappings) _ .ExposeConfiguration(AddressOf BuildSchema) _ .BuildSessionFactory() End Sub Private Function GetDBConfig() As FluentNHibernate.Cfg.Db.IPersistenceConfigurer Return SQLiteConfiguration.Standard _ .ConnectionString(Function(cs As ConnectionStringBuilder) cs.Is(CONNECTION_STRING)) End Function Private Sub GetMappings(ByVal x As MappingConfiguration) x.FluentMappings _ .AddFromAssemblyOf(Of TClassFromMappingAssembly)() _ .ExportTo(".") End Sub Private Sub BuildSchema(ByVal Cfg As NHibernate.Cfg.Configuration) Dim SE As New SchemaExport(Cfg) SE.Execute(False, True, False, GetConnection, Console.Out) End Sub Private Function GetConnection() As System.Data.SQLite.SQLiteConnection If m_Connection Is Nothing Then m_Connection = New SQLiteConnection(CONNECTION_STRING) m_Connection.Open() End If Return m_Connection End Function Public Function OpenSession() As ISession Return m_SessionFactory.OpenSession(GetConnection) End Function Private disposedValue As Boolean = False ' To detect redundant calls ' IDisposable Protected Overridable Sub Dispose(ByVal disposing As Boolean) If Not Me.disposedValue Then If disposing Then ' TODO: free other state (managed objects). If m_Connection IsNot Nothing Then m_Connection.Close() m_Connection = Nothing End If ' TODO: free your own state (unmanaged objects). ' TODO: set large fields to null. End If Me.disposedValue = True End Sub #Region " IDisposable Support " ' This code added by Visual Basic to correctly implement the disposable pattern. Public Sub Dispose() Implements IDisposable.Dispose ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(True) GC.SuppressFinalize(Me) End Sub #End Region End Class