In my last post we talked about letting our container manage our ISession lifetime. Immediatly after publishing this post, i’ve got a lot of reactions considering the management of transactions, more specificly about issueing a rollback when an exception has been thrown.
A simplistic approach would be to do this in every commandhandler:
But this approach seems rather cumbersome and not very flexible. What if we could automate this repeated code pattern more or less like what behaviors are doing in a WCF context ? We need some sort of AOP approach.
Meet AutoTx interceptor
Basicly this code takes the entire transaction idiom from our hands, it starts a transaction if not allready started and if the interceptor started the transaction it will commit/rollback (depending on exceptions thrown) the transaction.
Nothing really special going on, just not forget to opt-in for Lazy resolving and register the interceptor as transient, so that it’s lifetime gets bound to whom it is intercepting.