I’m looking for a good tutorial to configure AUTOFAC with NHibernate in a WinForm application injecting the ISession when a form is created and disposing the ISession on form close.
I found a lot of MVC and ASP.NET example but none using WinForm.
Can you point me in the right direction?
I would do something like this
Register your
ISessionasInstancePerLifetimeScopeand Autofac will dispose of it when its scope is disposed. In this example, I am using the “FormScope” tag so that if I accidentally try to resolve anISessionout of another scope (maybe the top-level container scope) Autofac will throw an exception.Your code will have to commit the transaction explicitly (probably when the user clicks Save or something), and it probably should rollback the transaction if the user clicks Cancel. Implicit rollback is not recommended.