Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?
like image 668
RKh Avatar asked May 12 '15 07:05

RKh


1 Answers

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.
}
like image 119
jgauffin Avatar answered Sep 29 '22 02:09

jgauffin