Who manages the transaction?


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 or rollback (depending on exceptions thrown) the transaction.

The registration

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.

2 thoughts on “Who manages the transaction?

    • Alexander, allthough i don’t believe a command can by handled in multiple ways, intercepting your send(icommand[] commands) commandbus could solve it. It would be using the same interceptor.

What do you think ?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s