This time I want to indroduce my approach on the Unit Of Work pattern (Unit of Work).
If you’re reading this and thinking EF or Nhibernate, then don’t waste your time. DbContext and ISession are already implementations of Unit of Work.
I use this aproach mostly with Micro ORM frameworks, like Dapper, Simple.Data, etc…
Are you still doing Ado.Net directly? In what year do you live in?
This is the base code. Now we have a few different options:
Add public properties of Connection and Transaction and let the outside world to manage them.
Add protected properties of Connection and Transaction and derive the class to a data access specific class.
Add to it the public interface of your favorite micro Orm library.
For option #1, I really don’t like it. It most certainly will lead to micro managing connection and transactions…
For option #2 and #3, it’s really up to you. You can even mix and match them.
I use option #3. Why? because is simpler, and I mostly just use Dapper on my projects. It’s a matter of personal preference.