Skip to content
Browse files

setup NHibernate properly. App basically runs in Nancy now. Still nee…

…d to remove all WebAPI reminniscents
  • Loading branch information...
1 parent f59560e commit 768982934a2456e062c3b9eac5d01c033713659f @horsdal committed Mar 22, 2012
View
42 RestBucks/Bootstrapper.cs
@@ -1,13 +1,19 @@
namespace RestBucks
{
+ using System;
using System.ComponentModel;
using Bots;
using Castle.Facilities.TypedFactory;
using Castle.MicroKernel.Resolvers.SpecializedResolvers;
+ using Castle.Windsor;
using Castle.Windsor.Installer;
+ using NHibernate;
+ using NHibernate.Context;
+
+ using Nancy;
using Nancy.Bootstrappers.Windsor;
public class Bootstrapper : WindsorNancyBootstrapper
@@ -26,6 +32,42 @@ protected override void ApplicationStartup(Castle.Windsor.IWindsorContainer cont
base.ApplicationStartup(container, pipelines);
container.Resolve<Barista>();
+
+ pipelines.BeforeRequest += c => CreateSession(container);
+ pipelines.AfterRequest += c => CommitSession(container);
+ pipelines.OnError += (c, e) => RollbackSession(container);
}
+
+ private Response RollbackSession(IWindsorContainer container)
+ {
+ var sessionFactory = container.Resolve<ISessionFactory>();
+ var requestSession = sessionFactory.GetCurrentSession();
+ requestSession.Transaction.Rollback();
+ CurrentSessionContext.Unbind(sessionFactory);
+ requestSession.Dispose();
+
+ return null;
+ }
+
+ private Response CreateSession(IWindsorContainer container)
+ {
+ var sessionFactory = container.Resolve<ISessionFactory>();
+ var requestSession = sessionFactory.OpenSession();
+ CurrentSessionContext.Bind(requestSession);
+ requestSession.BeginTransaction();
+
+ return null;
+ }
+
+ private AfterPipeline CommitSession(IWindsorContainer container)
+ {
+ var sessionFactory = container.Resolve<ISessionFactory>();
+ var requestSession = sessionFactory.GetCurrentSession();
+ requestSession.Transaction.Commit();
+ CurrentSessionContext.Unbind(sessionFactory);
+ requestSession.Dispose();
+
+ return null;
+ }
}
}
View
3 RestBucks/Infrastructure/Installers/NHibernateInstaller.cs
@@ -10,6 +10,7 @@ namespace RestBucks.Infrastructure.Installers
using NHibernate;
using NHibernate.Cfg;
+ using NHibernate.Context;
using NHibernate.Dialect;
using RestBucks.Data;
@@ -46,7 +47,7 @@ public static Configuration CreateConfiguration(string connectionStringName = "R
db.LogFormatedSql = true;
db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
});
- configuration.CurrentSessionContext<LazySessionContext>();
+ configuration.CurrentSessionContext<CallSessionContext>();
configuration.CollectionTypeFactory<Net4CollectionTypeFactory>();
configuration.AddDeserializedMapping(Mapper.Generate(), "Restbucks");
return configuration;
View
2 RestBucks/Infrastructure/Linking/ResourceLinker.cs
@@ -5,7 +5,7 @@
using System.ComponentModel;
using System.Linq;
- public class ResourceLinker : IResourceLinker
+ public class ResourceLinker
{
private readonly Uri baseUri;
View
5 RestBucks/Infrastructure/RequestExtensions.cs
@@ -17,7 +17,10 @@ public static bool IsNotModified(this Request request, IVersionable versionable)
public static string BaseUri(this Request request)
{
- return string.Format("{0}://{1}/", request.Url.Scheme, request.Url.BasePath ?? "bogus");
+ var host = request.Url.Port == null
+ ? request.Url.HostName
+ : string.Format("{0}:{1}", request.Url.HostName, request.Url.Port);
+ return string.Format("{0}://{1}/", request.Url.Scheme, string.IsNullOrWhiteSpace(host) ? "bogus" : host);
}
}
View
1 RestBucks/RestBucks.csproj
@@ -171,7 +171,6 @@
<Compile Include="Infrastructure\Installers\NHibernateInstaller.cs" />
<Compile Include="Infrastructure\Installers\RepositoryInstaller.cs" />
<Compile Include="Infrastructure\Installers\WebApiInstaller.cs" />
- <Compile Include="Infrastructure\Linking\IResourceLinker.cs" />
<Compile Include="Infrastructure\Linking\ResourceLinker.cs" />
<Compile Include="Infrastructure\Router.cs" />
<Compile Include="Infrastructure\SessionManagement\ISessionFactoryProvider.cs" />

0 comments on commit 7689829

Please sign in to comment.
Something went wrong with that request. Please try again.