Skip to content

Commit

Permalink
NHibernate infrastructure. related #1
Browse files Browse the repository at this point in the history
  • Loading branch information
andreazevedo committed Aug 9, 2011
1 parent e780935 commit 3f9b5c8
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 2 deletions.
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 src/PetStore.Infrastructure.NHibernate/Database/UnitOfWork.cs
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);
}
}
}
}
}
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>2e687ae0-bfc4-44af-9b34-df40da1f2c3a</ProjectGuid>
<ProjectGuid>{F5595B81-C00D-452E-8C4E-67380224EE4E}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PetStore.Infrastructure.NHibernate</RootNamespace>
Expand All @@ -31,18 +31,45 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core">
<HintPath>..\..\libs\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="HibernatingRhinos.Profiler.Appender.v4.0">
<HintPath>..\..\libs\HibernatingRhinos.Profiler.Appender.v4.0.dll</HintPath>
</Reference>
<Reference Include="Iesi.Collections">
<HintPath>..\..\libs\Iesi.Collections.dll</HintPath>
</Reference>
<Reference Include="NHibernate">
<HintPath>..\..\libs\NHibernate.dll</HintPath>
</Reference>
<Reference Include="NHibernate.ByteCode.Castle">
<HintPath>..\..\libs\NHibernate.ByteCode.Castle.dll</HintPath>
</Reference>
<Reference Include="NHibernate.Caches.SysCache">
<HintPath>..\..\libs\NHibernate.Caches.SysCache.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="Database\IDatabaseSessionFactory.cs" />
<Compile Include="Database\UnitOfWork.cs" />
<Compile Include="Database\WebBasedDatabaseSessionFactory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PetStore.Core\PetStore.Core.csproj">
<Project>{FF79E5EE-F489-4239-B935-DA2C06848BFA}</Project>
<Name>PetStore.Core</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
6 changes: 6 additions & 0 deletions src/PetStore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PetStore.Core.Test", "Test\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PetStore.TestBase", "Test\PetStore.TestBase\PetStore.TestBase.csproj", "{BE801B36-49C9-4770-952C-76259F81F5FF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PetStore.Infrastructure.NHibernate", "PetStore.Infrastructure.NHibernate\PetStore.Infrastructure.NHibernate.csproj", "{F5595B81-C00D-452E-8C4E-67380224EE4E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -33,6 +35,10 @@ Global
{BE801B36-49C9-4770-952C-76259F81F5FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE801B36-49C9-4770-952C-76259F81F5FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE801B36-49C9-4770-952C-76259F81F5FF}.Release|Any CPU.Build.0 = Release|Any CPU
{F5595B81-C00D-452E-8C4E-67380224EE4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F5595B81-C00D-452E-8C4E-67380224EE4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5595B81-C00D-452E-8C4E-67380224EE4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5595B81-C00D-452E-8C4E-67380224EE4E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 3f9b5c8

Please sign in to comment.