-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NHibernate infrastructure. related #1
- Loading branch information
1 parent
e780935
commit 3f9b5c8
Showing
5 changed files
with
238 additions
and
2 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/PetStore.Infrastructure.NHibernate/Database/IDatabaseSessionFactory.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,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using NHibernate; | ||
|
||
namespace PetStore.Infrastructure.NHibernate.Database | ||
{ | ||
/// <summary> | ||
/// NHibernate sessions manager | ||
/// </summary> | ||
public interface IDatabaseSessionFactory | ||
{ | ||
/// <summary> | ||
/// Open a NHibernate Session | ||
/// </summary> | ||
/// <returns>NHibernate Session.</returns> | ||
ISession Retrieve(); | ||
|
||
/// <summary> | ||
/// Opens an independent NHibernate Session. | ||
/// WARNING: Sessions opened independently must be manually closed! | ||
/// </summary> | ||
/// <returns>Session</returns> | ||
ISession OpenIndependentSession(); | ||
|
||
/// <summary> | ||
/// Closes the NHibernate Session. | ||
/// </summary> | ||
void Close(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/PetStore.Infrastructure.NHibernate/Database/UnitOfWork.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,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using NHibernate; | ||
using PetStore.Core.Infrastructure.InversionOfControl; | ||
using PetStore.Core.Infrastructure.UnitOfWork; | ||
|
||
namespace PetStore.Infrastructure.NHibernate.Database | ||
{ | ||
public class UnitOfWork : IUnitOfWork | ||
{ | ||
private static readonly Dictionary<int, ITransaction> Transactions = new Dictionary<int, ITransaction>(); | ||
private readonly object _sync = new object(); | ||
private readonly bool _isChild; | ||
private bool _commited = false; | ||
|
||
public UnitOfWork() | ||
{ | ||
lock (_sync) | ||
{ | ||
if (Transactions.ContainsKey(Thread.CurrentThread.ManagedThreadId)) | ||
{ | ||
_isChild = true; | ||
} | ||
else | ||
{ | ||
Transactions.Add(Thread.CurrentThread.ManagedThreadId, IoC.Resolve<IDatabaseSessionFactory>().Retrieve().BeginTransaction()); | ||
_isChild = false; | ||
} | ||
} | ||
} | ||
|
||
public void Commit() | ||
{ | ||
if (!_isChild) | ||
{ | ||
lock (_sync) | ||
{ | ||
Transactions[Thread.CurrentThread.ManagedThreadId].Commit(); | ||
_commited = true; | ||
} | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (!_isChild) | ||
{ | ||
lock (_sync) | ||
{ | ||
if (!_commited) | ||
{ | ||
Transactions[Thread.CurrentThread.ManagedThreadId].Rollback(); | ||
} | ||
Transactions[Thread.CurrentThread.ManagedThreadId].Dispose(); | ||
Transactions.Remove(Thread.CurrentThread.ManagedThreadId); | ||
} | ||
} | ||
} | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
src/PetStore.Infrastructure.NHibernate/Database/WebBasedDatabaseSessionFactory.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,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Web; | ||
using NHibernate; | ||
using NHibernate.Cfg; | ||
|
||
namespace PetStore.Infrastructure.NHibernate.Database | ||
{ | ||
/// <summary> | ||
/// Database session factory for WEB apps. | ||
/// Based on Request Context. | ||
/// </summary> | ||
public class WebBasedDatabaseSessionFactory : IDatabaseSessionFactory | ||
{ | ||
#region Fields | ||
|
||
private const string CurrentSessionKey = "nhibernate.current_session"; | ||
private static ISessionFactory _sessionFactory; | ||
private static object _sessionFactorySync = new object(); | ||
|
||
#endregion | ||
|
||
#region Properties | ||
|
||
public static ISessionFactory SessionFactory | ||
{ | ||
get | ||
{ | ||
if (_sessionFactory == null) | ||
{ | ||
lock (_sessionFactorySync) | ||
{ | ||
if (_sessionFactory == null) | ||
{ | ||
_sessionFactory = new Configuration().Configure().BuildSessionFactory(); | ||
} | ||
} | ||
|
||
} | ||
return _sessionFactory; | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
#region Constructors | ||
|
||
static WebBasedDatabaseSessionFactory() | ||
{ | ||
SetupProfiler(); | ||
} | ||
|
||
#endregion | ||
|
||
#region IDatabaseSessionFactory Members | ||
|
||
public ISession Retrieve() | ||
{ | ||
HttpContext context = HttpContext.Current; | ||
ISession currentSession = context.Items[CurrentSessionKey] as ISession; | ||
|
||
if (currentSession == null) | ||
{ | ||
currentSession = SessionFactory.OpenSession(); | ||
currentSession.FlushMode = FlushMode.Never; | ||
context.Items[CurrentSessionKey] = currentSession; | ||
} | ||
|
||
return currentSession; | ||
} | ||
|
||
public ISession OpenIndependentSession() | ||
{ | ||
ISession session = SessionFactory.OpenSession(); | ||
session.FlushMode = FlushMode.Never; | ||
return session; | ||
} | ||
|
||
public void Close() | ||
{ | ||
HttpContext context = HttpContext.Current; | ||
ISession currentSession = context.Items[CurrentSessionKey] as ISession; | ||
|
||
if (currentSession == null) | ||
{ | ||
return; | ||
} | ||
|
||
currentSession.Close(); | ||
context.Items.Remove(CurrentSessionKey); | ||
} | ||
|
||
#endregion | ||
|
||
#region Private Methods | ||
|
||
private static void SetupProfiler() | ||
{ | ||
#if DEBUG | ||
HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); | ||
#endif | ||
} | ||
|
||
#endregion | ||
} | ||
} |
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