How to use Repository and Unit of Work Patterns with ADO.NET?

RKh picture RKh · May 12, 2015 · Viewed 9.4k times · Source

I am building ASP.NET MVC 5 application.

I read about Repository and Unit of Work (UoW) Patterns here.

These examples use Entity Framework which adds a high-level of abstraction itself.

I am using ADO.NET and not EF. I want to know:

  1. Whether Repository and UoW patterns makes any sense with ADO.NET?
  2. How will my Repositories and UoW look with ADO.NET? Any Samples?
  3. Can I add a separate class library for Repository or to make it a part of DAL?

Answer

jgauffin picture jgauffin · May 12, 2015

I've written a blog post which teaches you on how to write driver independent code and how to implement Uow/Repository pattern with plain ADO.NET.

It's a bit too long to include in this answer, but basically the IDbtransaction will represent your Unit oF Work (or be contained in one) and every repository will take the transaction or UoW in it's constructor.

To create a command using a IDbTransaction

using (var cmd = transaction.Connection.CreateCommand())
{
    cmd.Transaction = transaction;

    //do a CRUD operation here.
}