Last active
August 29, 2015 14:11
-
-
Save cottsak/026ec230930d6fcd62d6 to your computer and use it in GitHub Desktop.
Explicitly flushing NHibernate ISession at the end of every request
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
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(); | |
}); | |
} | |
} |
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
[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"); | |
} |
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
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