Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnitOfWork vs Database connection

I currently have made a UnitOfWork implementation which wraps both the database connection and the transaction.

using (var uow = UnitOfWorkFactory.Create())
{
    // do db operations here through repositories

   uow.SaveChanges();
}

Rollback will be called if SaveChanges haven't been called before the uow gets disposed.

Is it a bad design choice to let the uow handle both the connection and the transaction?

Let's say that I got a ASP.Net MVC website where most of the actions are just fetching information from the database. Are there a performance penalty for creating/committing transactions that aren't actually not doing anything in the database?

like image 727
jgauffin Avatar asked May 25 '26 07:05

jgauffin


1 Answers

If you want to implement UoW then SaveChanges should be the only place where a connection and a transaction will be used. UoW will just collect all commands that must be executed and execute them in a transaction when SaveChanges is invoked.

like image 80
Ladislav Mrnka Avatar answered May 26 '26 20:05

Ladislav Mrnka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!