Skip to content

Instantly share code, notes, and snippets.

@cottsak
Last active August 29, 2015 14:11
Show Gist options
  • Save cottsak/026ec230930d6fcd62d6 to your computer and use it in GitHub Desktop.
Save cottsak/026ec230930d6fcd62d6 to your computer and use it in GitHub Desktop.
Explicitly flushing NHibernate ISession at the end of every request
class WebRegistrationModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterControllers(ThisAssembly);
builder.RegisterModule(new AutofacWebTypesModule());
RegisterDatabaseComponents(builder);
}
private void RegisterDatabaseComponents(ContainerBuilder builder)
{
builder.Register(context => NhibernateConfig.CreateSessionFactory().OpenSession())
.As<ISession>()
.InstancePerRequest()
.OnRelease(session =>
{
if (session.IsDirty())
session.Flush(); // deletes won't work without explicit .Flush()
session.Dispose();
});
}
}
[HttpPost]
public ActionResult CreateIC(string code, string description)
{
_session.Save(new IntegratedCircuit { Code = code, Description = description });
AddPageAlert("IC added successfully.", AlertType.Success);
return RedirectToAction("Index");
}
public HttpResponseMessage Post(int id)
{
var ic = _session.Load<IntegratedCircuit>(id);
_session.Delete(ic);
return new HttpResponseMessage(HttpStatusCode.OK);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment