Skip to content

Instantly share code, notes, and snippets.

@JakeGinnivan
Last active December 29, 2015 11:09
Show Gist options
  • Select an option

  • Save JakeGinnivan/7661360 to your computer and use it in GitHub Desktop.

Select an option

Save JakeGinnivan/7661360 to your computer and use it in GitHub Desktop.
public static class nHibernateExtensions
{
public static T Transaction<T>(this ISession session, Action<ISession, T> action)
{
// 3. Enters here
using (var tran = session.BeginTransaction()){
// 4. Calls into the lambda
return action(tran);
// 7. execution continues here
} // 8. Transaction is commited/disposed
// 9. this method returns the task from the action (which is still running)
}
}
// 1. Execution Starts
public async T DoStuff(){
// 2. Calls Transaction
//10. the task is now awaited
return await _session.Transaction(async session=>
{
// 5. Lambda executing
session.Get<Foo>();
// 6. We hit the await keyword, this causes this lambda to return
await SomeAsyncThingo();
// 11. The async operation returns, now you are playing with a disposed transaction
session.DoSomethingElse();
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment